在存储过程中如何判断是否有符合条件的记录?
比如:select @regFrom=regfrom from users where username=@uid
字段regFrom可以为空的.
经常测试,不存在@uid时,@regFrom是为NULL,
当存在@uid,且regFrom字段是NULL时,结果@regFrom也是为空。
请问这两个结果应该如何区分?

解决方案 »

  1.   

    字段regFrom可以为空的.
    经常测试,不存在@uid时,@regFrom是为NULL,
    当存在@uid,且regFrom字段是NULL时,结果@regFrom也是为空。
    请问这两个结果应该如何区分?
    ----------------
    不存在@uid时,@regfrom应该是没返回数据,
    而存在@uid,@regFrom返回了数据,只不过值为NULL,当然不同了。
      

  2.   

    select @regFrom=regfrom from users where username=@uid
    if @@rowcount=0 --不存在@uid
    else
      

  3.   

    create table users(id int,username varchar(10),regfrom  varchar(10))
    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
    ----------
    */
      

  4.   

    select count(1) from users where username=@uid 
    增加一个判断吧。
      

  5.   


    --或者自己判断
    if not exists(select regfrom from users where username=@uid)
           print '不存在该username'
    else
    begin
           select @regFrom=regfrom from users where username=@uid
           --继续你的操作
    end
      

  6.   

     用exists判断是否存在记录
    if exists(select 1 from users where username=@uid)
      

  7.   

    select @regFrom=isnull(regfrom,'') from users where username=@uid如果@regFrom 是 null 表示没有数据
    如果@regFrom 是 '' 表示数据为null