我有以下要求。当存在FirstName时,当LastName存在时,我应该返回唯一的condtionFirstName;当First和email都存在时,我应该返回唯一的condtionlastName;我应该返回condtionFirstName + condtionEmail。
基本上无论存在哪个值,我都应返回该条件,如果1表示1条件,如果1和2则表示条件1和2,如果1和3则表示条件1和3,如果只有3则表示条件3。
请帮助我理解这个逻辑。
DECLARE @FirstName NVARCHAR(100)
DECLARE @LastName NVARCHAR(100)
DECLARE @Email NVARCHAR(200)
DECLARE @condtionFirstName NVARCHAR(200)
DECLARE @condtionlastName NVARCHAR(200)
DECLARE @condtionEmail NVARCHAR(200)
SET @FirstName = 'JOhn'
SET @LastName = 'David'
SET @Email = 'john.david@abc.com'
SET @condtionFirstName = ' AND FirstName = ' + '''' + @FirstName + ''''
SET @condtionlastName = ' AND LastName = ' + '''' + @LastName + ''''
SET @condtionEmail = ' AND Email = ' + '''' + @Email + ''''
答案 0 :(得分:3)
这就是我长期以来所说的“ the kitchen sink”-您想要一个可以支持搜索条件任意组合的查询。一些想法:
sp_executesql
,即使它们并非最终都包含在动态SQL语句中。这就像声明局部变量而不使用它。示例:
DECLARE @FirstName nvarchar(100),
@LastName nvarchar(100),
@Email nvarchar(320),
@conditions nvarchar(max) = N'';
SET @FirstName = N'John';
SET @LastName = N'David';
SET @Email = N'john.david@abc.com';
SET @conditions += CASE WHEN @FirstName IS NOT NULL THEN
N' AND FirstName = @FirstName' ELSE N'' END
+ CASE WHEN @LastName IS NOT NULL THEN
N' AND LastName = @LastName' ELSE N'' END
+ CASE WHEN @Email IS NOT NULL THEN
N' AND Email = @Email' ELSE N'' END;
DECLARE @sql nvarchar(max) = N'SELECT ... FROM dbo.table
WHERE 1 = 1' + @conditions;
PRINT @sql; -- try this when populating or not populating each of
-- @FirstName, @LastName, @Email
EXEC sys.sp_executesql @sql,
N'@FirstName nvarchar(100), @LastName nvarchar(100), @Email nvarchar(320)',
@FirstName, @LastName, @Email;
更多详细信息: