开发者

SQL Server Procedure Parameter Null Case

I have a single int output parameter for a sproc in MSSQL 2008. The value can be null or it can be populated.

I am trying to write a clever single select statement that will 开发者_运维问答return one row: if the param value is populated, return that row if the param value is null, return the top 1 row where issoftdeleted=0

Thanks for all ideas.


SELECT
   TOP (CASE WHEN @param IS NULL THEN 2000000000 ELSE 1 END)
  ...
FROM
   Mytable
WHERE
   SomeID = @param
   OR 
   (@param IS NULL AND issoftdeleted = 0)
ORDER BY
   something --to make the top meaningful

If you expect one row when @param is set then use TOP (1)

Personally, I'd consider using an IF statement and 2 separate SELECTs...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜