【发布时间】:2010-06-06 18:24:11
【问题描述】:
我有一个动态构建查询的存储过程。与此查询关联的 where 子句基于用户选择的过滤器值。不管我做什么,where 子句似乎都没有设置。
-- Dynamically build the WHERE clause based on the filters
DECLARE @whereClause as nvarchar(1024)
IF (@hasSpouse > -1)
BEGIN
IF (@hasSpouse = 0)
SET @whereClause='p.[HasSpouse]=0'
ELSE
SET @whereClause='(p.[HasSpouse]=1 OR p.[HasSpouse] IS NULL)'
END
-- Dynamically add the next filter if necessary
IF (@isVegan > -1)
BEGIN
IF (LEN(@whereClause) > 0)
BEGIN
SET @whereClause = @whereClause + ' AND '
END
IF (@isVegan = 0)
SET @whereClause = @whereClause + 'c.[IsVegan]=0'
ELSE
SET @whereClause = @whereClause + '(c.[IsVegan]=1 OR c.[IsVegan] IS NULL)'
END
PRINT @whereClause
@whereClause 从不打印任何内容。反过来,LEN(@whereClause) 始终为 NULL。 @isVegan 和@hasSpouse 值被传递到存储过程中。这些值是我所期望的。
我做错了什么?为什么永远不会设置 @whereClause?
感谢您的帮助!
谢谢!
【问题讨论】:
标签: sql sql-server tsql