Sql server 2012 向where子句sql添加参数

Sql server 2012 向where子句sql添加参数,sql-server-2012,Sql Server 2012,我是SQL的初学者,我正在尝试运行以下SP DECLARE @stringStatus varchar(100) --Check for status value IF @Status is NULL BEGIN set @stringStatus = '' END ELSE BEGIN set @stringStatus = ' and ps.Status = ' + CAST(@Status as varchar) END select * from Projects

我是SQL的初学者,我正在尝试运行以下SP

DECLARE @stringStatus varchar(100)

--Check for status value
IF @Status is NULL
BEGIN
    set @stringStatus = ''
END
ELSE 
BEGIN
    set @stringStatus = ' and ps.Status = ' + CAST(@Status as varchar)
END

select * from Projects p
    join projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) + @stringStatus
上述操作的目的是在@Status为NULL时获取所有行,并在@Status已分配参数时过滤行。 @类别varchar和@Status int在参数中

当@Status为NULL时,这可以正常工作,也就是说,我获取所有记录。但是,如果我传递一个参数,比如@Status=2,那么即使有一些记录可用,执行也不会返回任何行。

首先,我如何得到我想要的结果?第二,有没有更好的方法在没有if条件块的情况下实现这一点?
实际上,你的结果是

select * from something where ps.Category ='some string, containing and ps.Status= inside' 
所以空行集是预期的结果。 希望状态是数字,而不是字符串

select * from Projects p
    join projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) and
     (@Status is NULL OR ps.Status = @Status)
好的,下面是对不信任的测试:-

declare @projects table 
(
    pid int,
    name nvarchar(20),
    category int
);

declare @projectstatus table
(
    pid int,
    Category int,
    status int
);

insert into @projects values
(1,'Project 1', 1),(2,'Project 2',1),(3,'Project 3',1),(4,'Project 4',1),(5,'Project 5',1);

insert into @projectstatus values
(1,1,1),(2,1,2),(3,1,3),(4,1,2),(5,1,NULL);


declare @Category int =null;
declare @Status int;

--first of all, do not understand, what is the logic with category
--category in one table should be the same, than in other table or specified?
--ok, you said with category everything is ok, do not test category, test status 

--test with null
set @Status=null


select * from @Projects p
    join @projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) and
     (@Status is NULL OR ps.Status = @Status)

--test with existing status     
set @Status=1
select * from @Projects p
    join @projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) and
     (@Status is NULL OR ps.Status = @Status)

--test with not existing status     
set @Status=10
select * from @Projects p
    join @projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) and
     (@Status is NULL OR ps.Status = @Status)

您可以按以下方式简单地设置条件,以获得所需的结果

--Check for status value
IF @Status is NULL
  BEGIN
    select * from Projects p
    join projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) 
  END
ELSE 
  BEGIN
    select * from Projects p
    join projectstatus ps on p.pid = ps.pid
    where ps.Category = isnull(@Category, p.Category) + @stringStatus
  END

谢谢

@Status为NULL或ps。Status=@Status将始终返回完整列表。它永远不会过滤。不要同意。例如@Status=1,将返回状态为1的行,而不显示状态为2的行。我甚至可以举一些临时表格的例子