Q:如下情况:
create proc save_stopname 
(@bus_id int,@stop_name varchar(50),@req int)
as
begin
declare @temp int
set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
@temp=?--返回值是什么?
用if ...else来判断怎么写?是if @temp==0 begin...end else begin ... end吗?

解决方案 »

  1.   

    declare @sql varchar(30) 
    select  @sql=(select cols from  表A where id=condition)
    if((@sql is null) or @sql='')
    begin 
    print ' null'
    end 
    else
    begin 
    print 'not null '
    end 
      

  2.   

    create proc test_001 
    @bus_id int,
    @stop_name varchar(50),
    @req int
    as
    begin
    declare @temp int
    select @temp =stop_id from Chengdu_stop where stop_name=@stop_name
    if @temp>0
    select '存在' as aaa
    else
    select '不存在' as aaa
    end但是我不明白,为什么要传 @bus_id,@req 这两个参数,存储过程里面没有用到的地方啊
      

  3.   

    1楼不要or @sql=''要的话就不是null
    2楼:楼主只给出部分,不能用0来判断应用 is null 
    如果不用变量接收应用if exists select stop_id from Chengdu_stop .......
      

  4.   

    create proc save_stopname 
    (@bus_id int,@stop_name varchar(50),@req int)
    as
    begin
    declare @temp int
    set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
    --@temp=?--返回值是什么?
    if @temp=1
      begin
      --...
      end
    else
      begin
      --...
      end
      

  5.   

    得是得注意:
    set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
    这种写法如果查询结果不止一个,就会出错.
      

  6.   

    create proc save_stopname 
    (@bus_id int,@stop_name varchar(50),@req int)
    as
    begin
    declare @temp int
    select @temp=stop_id from Chengdu_stop where stop_name=@stop_nameif @temp is not null
    --有值
    else
    --无值
      

  7.   

    create proc save_stopname 
    (@bus_id int,@stop_name varchar(50),@req int)
    as
    begin
    declare @temp int
    set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name) if(@temp is null)
       begin
       end
     else 
       begin 
       end 
      

  8.   

    这个会有问题
    set @temp=(select stop_id from Chengdu_stop where stop_name=@stop_name)
    如果存在多条会出现这个错误
    服务器: 消息 512,级别 16,状态 1,行 2
    子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。为保险起见,还是加top 1
    set @temp=(select top 1 stop_id from Chengdu_stop where stop_name=@stop_name)另外,stop_id是否有空值??如果没有
    就通过判断@temp is null就可以知道了。如果有空值,那么就用isnull(stop_id,'000')处理下,在通过判断@temp is null就可以知道了
      

  9.   

    要@temp 干什么,画蛇添足了。proc save_stopname 
    (@bus_id int,@stop_name varchar(50),@req int)
    as
    beginselect stop_id from Chengdu_stop where stop_name=@stop_nameif stop_id is not null
        --有值
    else
        --无值
      

  10.   

    还是长虹nb;select stop_id from Chengdu_stop where stop_name=@stop_name if @@rowcount > 0  
        --有值 
    else 
        --无值 没试过!
      

  11.   

    @@rowcount这个个不挺好
      

  12.   


    if(@temp is null)
       begin
       end
     else 
       begin 
       end