我有一个存储过程,我需要检查以查看:
if @variable is null
Then this
Else that
在这种情况下,最好使用IF / ELSE或CASE还是其他什么?
谢谢!
答案 0 :(得分:3)
选项1:
SELECT CASE WHEN @variable IS NULL THEN 'IT IS NULL' ELSE 'NOT NULL' END
选项2:
SELECT COALESCE(@variable,'IT IS NULL')
选项3:
SELECT ISNULL(@variable,'IT IS NULL')
所有内容都将附带Select Statement
选项4:
IF @variable IS NULL
BEGIN
PRINT 'IT IS NULL'
END
ELSE
PRINT 'IT IS NOT NULL'
希望图片清晰
答案 1 :(得分:3)
如果此和 是SQL查询,我宁愿使用IF
语句。
示例:
if @status=0
select * from #t where id=2
else
select * from #t where names='nameHere'
但如果它们是值,请使用CASE
:
SELECT (CASE WHEN @variable is null
THEN 'Nothing'
ELSE 'Hello!'
END) Status
FROM tableName