You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取SQL查询中满足WHERE子句条件的列名需求及对应代码

提取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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:31:41