我有一個動態構建查詢的存儲過程。與此查詢關聯的where子句基於用戶選擇的過濾器值。不管我做什麼,where子句似乎都沒有設置。SQL Server 2005 - 構建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?
謝謝你的幫助!
謝謝!
+1:我的情緒正好,尤其是支架... – 2010-06-06 19:25:37