在我的sql-server 2008数据库的触发器中,我需要检查一个变量是否为空.这段代码可以满足我的需要,但它可以用更少的行来完成,更具可读性吗?
DECLARE @string varchar
DECLARE @float float
DECLARE @bit bit
DECLARE @int int
Set @string=NULL -- Exactly one of these variables needs to be set
Set @float=NULL --
Set @bit=NULL --
Set @int=NULL --
IF( (@string is not null AND COALESCE(@float, @bit, @int) IS NULL)
OR (@float is not null AND COALESCE(@string, @bit, @int) IS NULL)
OR (@bit is not null AND COALESCE(@string, @float, @int) IS NULL)
OR (@int is not null AND COALESCE(@string, @float, @bit) IS NULL)
)
print ' ok'
ELSE
print ' not ok'
Run Code Online (Sandbox Code Playgroud)
SELECT CASE WHEN COUNT(c) =1 THEN 'Y' ELSE 'N' END
FROM
(VALUES (CAST(@string AS SQL_VARIANT)),(@float),(@bit),(@int)) T (c)
Run Code Online (Sandbox Code Playgroud)