1

抱歉创建一个新线程。我无法在上一个帖子中正确地提出问题

我在 SQL Server 2008 中有一个如下所示的表 -

Id      Status
--------------
1        A
2        I
3        NULL

@Status我有一个作为参数的存储过程。

If the @Status is "-1", I need to retrieve records 1, 2. 
If the @Status is "A", I need to retrieve only record 1.
If the @Status is "I", I need to retrieve only record 2.

我希望能够在SELECT不使用IF ELSE. SELECT 语句中有很多连接和东西,我不想在 ELSE 条件中重复同样的事情。

我尝试了以下但得到不正确的数据 -

SELECT * FROM Person WHERE Status = @Status OR @Status IS NOT NULL

select * from Person
where (@Status is not null and Status = @Status) or
      (@Status is null and Status in ('A', 'P')) 
4

1 回答 1

2

您可以使用 OR:

SELECT *
FROM Person
WHERE Status = @Status
OR (@Status = '-1' AND Status IS NOT NULL)

当然,您不应该SELECT *在生产代码中使用。

于 2012-04-25T19:57:23.637 回答