【发布时间】:2014-05-13 13:54:45
【问题描述】:
我正在尝试在 SQL Server 2005 的 where 子句中添加一列,但问题是我必须为给定条件添加它,例如
where condition 1
and condition 2
if(@val !=null and @val = 'test')
begin
and condition 3
end
但是当我尝试这个 SQL Server 时,我会报错:
Incorrect syntax near the keyword 'and'. Severity 15 Procedure Name
Incorrect syntax near 'END'. Severity 15 Procedure Name
我做了一些研发,发现我应该改用case语句,然后我尝试了这个
where condition 1
and condition 2
CASE @val
WHEN 'test' THEN AND condition 3
END
但现在它给出了错误:
Incorrect syntax near the keyword 'CASE'. Severity 15 Procedure Name
谁能帮我解决这个问题?谢谢。
编辑
select *
from Tables with joins
where ac_jd_date_closed >= convert(VARCHAR(10),dateadd(dd, -@period, getdate()), 101)
and ac_jd_date_closed <= convert(VARCHAR(10),dateadd(dd, 0, getdate()), 101)
and lo_pc_last_name not in ('Shroder')
and lm_ap_pr_id NOT IN (4743, 2683)
我想在 spm David 时添加这个条件,否则这个条件不应该存在。
【问题讨论】:
-
CASE in WHERE, SQL Server 的可能重复项
标签: sql sql-server