在下面的示例中,有没有办法避免错误空或全文谓词?
DECLARE @SearchText VARCHAR(1000);
SET @SearchText = ''
SELECT * FROM myTable
WHERE
/*
SOME CONTITIONS
AND
*/
(
@SearchText = ''
OR
(
@SearchText <> ''
AND
CONTAINS((myField1, myField2), @SearchText)
)
)
我可能会这样做,但我想避免重复代码:
DECLARE @SearchText VARCHAR(1000);
SET @SearchText = ''
IF @SearchText = ''
BEGIN
SELECT * FROM myTable
WHERE
/*
SOME CONTITIONS
*/
END
ELSE
BEGIN
SELECT * FROM myTable
WHERE
/*
SOME CONTITIONS
AND
*/
(
@SearchText = ''
OR
(
@SearchText <> ''
AND
CONTAINS((myField1, myField2), @SearchText)
)
)
END
[编辑]
我找到了答案here
所以解决方法是将@SearchText设置为'""'
而不是将其留空。