2

我有一个存储过程,我想根据传入的参数查询生产表或“正在进行的工作”表。我可以编写两个单独的存储过程,但我认为这值得一试。

类似于:

create procedure getUserDetails
    @userID int,
    @prod varchar(5)
as 
    begin
        select * from
            if (@prod = 'true')
            Begin
                userprod_table
            else
                userwip_table
            end 
    where something = 'something'
END

这是可能吗?我不想写 2 个几乎相同的 SP:-/

4

2 回答 2

1

为什么不使用if(@prod = 'true')如下的简单语句:

if (@prod = 'true')
begin
    select * from userprod_table where something = 'something'
end  else
begin
    select * from userwip_table where something = 'something'
end
于 2013-03-11T14:46:20.687 回答
0

您可以使用 CTE,以免重复您的主要查询

with usertable as
(
    select * from userprod_table where 1 = @flag
    union
    select * from userwip_table where 0 = @flag
)
select ... from usertable ...
于 2013-03-11T14:57:51.937 回答