需求是这样的:    我在写一个存储过程的时候,需要判断一个表的多个字段是否为空
    如表的字段: field1,field2,field3    需要先判断这3个字段是否为空
    如果field1为空,则将要修改的值写到field1
    如果field1不为空,而field2为空,则写到field2
    即,从左至右,哪个最先为空,就先写到哪个字段里面大虾们帮帮忙,小弟在线等哦

解决方案 »

  1.   

    declare @s varchar(20)
    set @s=?update
      tb
    set
      field1=case when field1 is null then @s else field1 end,
      field2=case when field1 is not null and field2 is null then @s else field2 end,
      field3=case when field1 is not null and field2 is not null and field3 is null then @s else field3 end
      

  2.   

    create proc u(@s varchar(10))
    as
    if (select field1  from tb ) is null
    begin
    udate tb
    set field1=@s 
    return 
    end
    else if (select field2  from tb ) is null
    begin
    udate tb
    set field2=@s 
    return 
    end
    else 
    begin
    udate tb
    set field3=@s 
    return 
    end