(1)execute @sta=my_status ''
(2)execute @sta=my_status
(3)declare @temp
execute @sta=my_status @temp    
(4)declare @temp
execute @sta=my_status @name=@temp
------------------------
这四种情况得到的@sta值均为-155

解决方案 »

  1.   

    create proc my_status
                (@name char(8)=NULL)
    as
              if @name is NULL--**********************************
                   return 55
              if not exists
                        (select * from 学生表 where 姓名=@name)
                    return -155
    return 0
      

  2.   

    NULL值判断是用 is null or is not null
      

  3.   

    将“姓名”字段赋一个默认值(不太常用的值),如“0”
    这样 if @name='0'   return 55
      

  4.   

    将“姓名”字段赋一个默认值(不太常用的值),如“0”
    这样 if @name='0'   return 55
      

  5.   

    空 (NULL) 值表示数值未知。空值不同于空白或零值。没有两个相等的空值。比较两个空值或将空值与任何其它数值相比均返回未知,这是因为每个空值均为未知。
      比较空值时必须小心。比较行为取决于选项 SET ANSI_NULLS 的设置。当 SET ANSI_NULLS 为 ON 时,如果比较中有一个或多个表达式为 NULL,则既不输出 TRUE 也不输出 FALSE,而是输出UNKNOWN。这是因为,未知值不能与其它任何值进行逻辑比较。这种情况发生在一个表达式与 NULL 单词进行比较,或者两个表达式相比,而其中一个表达式取值为 NULL 时。例如,当 ANSI_NULLS 为 ON 时,以下比较总是输出 UNKNOWN:ytd_sales > NULL 只要变量包含 NULL 值,下列比较也输出 UNKNOWN:ytd_sales > @MyVariable*** 建议使用 IS NULL 或 IS NOT NULL 子句测试 NULL 值 ***
      

  6.   

    同意,null 的判断用 is 或者 is not 不要用 =
      

  7.   

    create proc my_status
                (@name varchar(8)='')
    as
              if @name=''
                   return 55
              if not exists
                        (select * from 学生表 where 姓名=@name)
                    return -155
    return0
      

  8.   

    if isnull(@name,'')=''
        return 55
    这样不管@name是null还是''都可以正确判断。
      

  9.   

    --把存儲過程修改如下就可以了.
    create proc my_status
                (@name char(8)=NULL)
    as
              --if @name=NULL
              if @name is NULL
                   return 55
              if not exists
                        (select * from 学生表 where 姓名=@name)
                    return -155
    return0
      

  10.   

    if  exists(select 1 from sysobjects where id=object_id('學生表') and objectproperty(id,'isTable')=1)
    drop table 學生表
    gocreate table 學生表(學號 varchar(10) not null primary key,姓名 varchar(10) null)
    goif  exists(select 1 from sysobjects where id=object_id('my_status') and objectproperty(id,'isprocedure')=1)
    drop procedure my_status
    gocreate proc my_status
                (@name char(8)=NULL)
    as
              if @name is NULL
                   return 55
              if not exists
                        (select * from 學生表 where 姓名=@name)
                    return -155
    return 0
    goinsert into 學生表 select '200204001','李濤'
    goselect * from 學生表
    /*
    學號             姓名
    200204001        李濤*/
    --調用實例A
    declare @sta int
    execute @sta=my_status '李濤'    -----(1)
    if @sta=55
             print '沒有给名字'
    else
             if @sta=-155
                      print '没找到'
    select @sta
    go
    /*
    (No  column name)
    0
    */--調用實例B
    declare @sta int
    execute @sta=my_status '葛文卿'
    if @sta=55
             print '沒有给名字'
    else
             if @sta=-155
                      print '没找到'
    select @sta
    go
    /*
    (No  column name)
    -155
    */--調用實例C
    declare @sta int
    execute @sta=my_status 
    if @sta=55
             print '沒有给名字'
    else
             if @sta=-155
                      print '没找到'
    select @sta
    go
    /*
    (No  column name)
    55
    */--調用實例D
    declare @sta int
    declare @temp varchar(10)
    execute @sta=my_status @name=@temp
    if @sta=55
             print '沒有给名字'
    else
             if @sta=-155
                      print '没找到'
    select @sta
    go
    /*
    (No  column name)
    55
    */