[英]SQL Server stored procedure dynamic SQL in WHERE clause
I have written a stored procedure that accepts one parameter. 我编写了一个接受一个参数的存储过程。
Request_ID int = 0
Later, in my WHERE
clause, I need to accomplish the following. 稍后,在我的WHERE
子句中,我需要完成以下工作。
I don't know how to describe it, but you can see what I'm trying to do with this completely made up pseudo-code: 我不知道如何描述它,但是您可以看到我正在尝试使用完全由伪代码组成的代码:
WHERE isNull(a.Target_Department, '') <> ''
AND isNull(a.Resolved_Date, '') = ''
AND isNull(c.Request_Archived, '') <> 'Y'
AND IF @Request_ID = 0
THEN Request_ID > 0
ELSE Request_ID = @Request_ID
END
Basically, if a specific request_ID
is supplied, I want the WHERE
clause to narrow to only that specific Request_ID
. 基本上,如果提供了特定的request_ID
,我希望WHERE
子句缩小到仅该特定的Request_ID
。 Otherwise, I want all of them. 否则,我想要所有人。
Any ideas on how to accomplish this? 关于如何做到这一点的任何想法?
Try this --> 试试这个->
WHERE
(@Request_ID IS NULL OR Request_ID=@RequestID)
OR 要么
WHERE
(@Request_ID=0 OR Request_ID=@RequestID)
and isnull(nullif(@RequestId,0),RequestId)=RequestId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.