获取SQL查询中满足WHERE子句条件的列名需求及对应代码
Hey there! Let's work through this together. First, I notice your original SQL has some syntax errors in the CASE statements—they're missing the ELSE clause and closing END keyword. Let's fix that first so we can clearly see which columns are involved, then pull out the ones you need.
修正后的语法正确版SQL
Declare @sdate datetime Declare @edate datetime set @sdate='2018-01-01 00:00:00' set @edate='2018-01-18 23:59:59' select * from Upload Where (CASE WHEN Step_1 >= @sdate and Step_1 <= @edate THEN 'Step_1' ELSE '' END) >= @sdate AND (CASE WHEN Step_2 >= @sdate and Step_2 <= @edate THEN 'Step_2' ELSE '' END) >= @sdate go
提取出的目标列名
Looking at the WHERE clause logic, the columns directly involved in your date range conditional checks are:
- Step_1
- Step_2
These are the only columns tied to your @sdate/@edate parameters, so they're the ones you'll want to use for your follow-up work.
额外小技巧:自动化提取列名(批量处理场景)
If you ever need to extract columns from queries programmatically instead of manually parsing, you can use SQL Server's system views like sys.dm_exec_describe_first_result_set, or use string functions to search for column name patterns (like your Step_ prefix). For general cases, relying on system views is far more reliable than manual string searches.
内容的提问来源于stack exchange,提问作者Rajat Sharma

