我有以下四个参数的存储过程。
存储过程spTest
:
CREATE PROCEDURE spTest
@Name varchar(20) = '',
@Address varchar(100) = '',
@City varchar(50) = '',
@Pin varchar(50) = ''
AS
DECLARE @DynamicWhere varchar(max)
DECLARE @Query varchar(max)
/* Here I want to prepare a dynamic where clause for all possibilities */
SET @Query = 'SELECT * FROM Test_Table '+ @DynamicWhere +'';
EXECUTE(@Query);
GO
好吧,我正在准备这样:
IF @Name = '' AND @Address = '' AND @City = '' AND @Pin = ''
BEGIN
SET @DynamicWhere = '';
END
ELSE IF @Name != '' AND @Address = '' AND @City = '' AND @Pin = ''
BEGIN
SET @DynamicWhere = 'Name ='''+@Name+'''';
END
ELSE IF @Name != '' AND @Address != '' AND @City = '' AND @Pin = ''
BEGIN
SET @DynamicWhere = 'Name ='''+@Name+''' AND Address ='''+@Address+'''';
END
......
......
Many possibilities
这是正确的方法 OR 有没有更好的方法来准备动态WHERE
条款?
答案 0 :(得分:5)
它被称为catch-all查询,它基本上是这样的:
CREATE PROCEDURE spTest
@Name varchar(20) = '',
@Address varchar(100) = '',
@City varchar(50) = '',
@Pin varchar(50) = ''
AS
SELECT *
FROM Test_Table
WHERE (@Name = '' OR Name = @Name)
AND (@Address = '' OR Address = @Address)
AND (@City = '' OR City = @City)
AND (@Pin = '' OR Pin = @Pin);
GO
答案 1 :(得分:1)
在这种情况下你也可以使用ISNULL和NULLIF:
下面的代码应该工作:
CREATE PROCEDURE spTest @Name varchar(20) = '', @Address varchar(100) = '', @City varchar(50) = '', @Pin varchar(50) = '' AS SET @Name=NULLIF(@Name,'') SET @Address=NULLIF(@Address,'') SET @City=NULLIF(@City,'') SET @Pin=NULLIF(@Pin,'') SELECT * FROM Test_Table WHERE Name = ISNULL(@Name,Name) AND Address = ISNULL(@Address,Address) AND City = ISNULL(@City,City) AND Pin = ISNULL(@Pin,Pin) GO
答案 2 :(得分:1)
我更新了@Zohar的答案。定义空白是坏习惯,理想情况下定义为null,然后使用空白。所以查询将是
CREATE PROCEDURE spTest
@Name varchar(20) = null,
@Address varchar(100) = null,
@City varchar(50) = null,
@Pin varchar(50) = null
AS
SELECT *
FROM Test_Table
WHERE (Name = ISNULL(@Name,'') )
AND (Address = ISNULL(@Address,''))
AND (City = ISNULL(@City,''))
AND (Pin = ISNULL(@Pin,''));
GO
即使我喜欢@Biswa的答案,因为它使用当前版本的sql server,但是Sqlserver 2008R2没有这个功能。