在存储过程中如何判断是否有符合条件的记录?
比如:select @regFrom=regfrom from users where username=@uid
字段regFrom可以为空的.
经常测试,不存在@uid时,@regFrom是为NULL,
当存在@uid,且regFrom字段是NULL时,结果@regFrom也是为空。
请问这两个结果应该如何区分?
比如:select @regFrom=regfrom from users where username=@uid
字段regFrom可以为空的.
经常测试,不存在@uid时,@regFrom是为NULL,
当存在@uid,且regFrom字段是NULL时,结果@regFrom也是为空。
请问这两个结果应该如何区分?
经常测试,不存在@uid时,@regFrom是为NULL,
当存在@uid,且regFrom字段是NULL时,结果@regFrom也是为空。
请问这两个结果应该如何区分?
----------------
不存在@uid时,@regfrom应该是没返回数据,
而存在@uid,@regFrom返回了数据,只不过值为NULL,当然不同了。
if @@rowcount=0 --不存在@uid
else
insert into users values(1,'aaa','asdf')
insert into users values(2,'bbb',null)
goselect regfrom from users where username='bbb'
--有结果,但值为null
/*
regfrom
----------
NULL
*/select regfrom from users where username='ccc'
--无结果
/*
regfrom
----------
*/
增加一个判断吧。
--或者自己判断
if not exists(select regfrom from users where username=@uid)
print '不存在该username'
else
begin
select @regFrom=regfrom from users where username=@uid
--继续你的操作
end
if exists(select 1 from users where username=@uid)
如果@regFrom 是 '' 表示数据为null