开发者

SQL Server Procedure Parameter Null Case

开发者 https://www.devze.com 2023-03-22 05:28 出处:网络
I have a single int output parameter for a sproc in MSSQL 2008. The value can be null or it can be populated.

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

精彩评论

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

关注公众号