在表内行复制
  
   ID    事件ID    地点            时间       人数
    1      3      fake street    3:00pm    30
   2       4       null          5:00pm    null
  现在想把@FromID = 1的信息复制到@ToID = 2上,只有当字段为null时才复制,事件ID不变  输出结果为  ID    事件ID    地点            时间       人数
    1      3      fake street   3:00pm    30
   2       4      fake street   5:00pm     30 请问是否为null怎么判断,这个存储过程怎么写?
 谢谢!

解决方案 »

  1.   

    难道是每一个判断一下is null,然后select,然后插入?
    我有很多列,每个都要进行上述三个操作?
      

  2.   


    declare @FromID int,@ToID int
    SET @FromID= 1
    SET @ToID = 2
    INSERT INTO table1(ID ,事件ID,地点,时间,人数 ) 
    select ID,事件ID,地点,时间,人数  
    from table 
    where   (ID is null 
    or   事件ID is null
    or    地点 is null
    or     时间  is null
    or    人数 isnull) and ID=@FromID AND ID =@ToID
      

  3.   


     
    update table1
    set 事件ID=isnull(a.事件ID,b.事件ID)
    ,地点=isnull(a.地点,b.地点)
    ,时间=isnull(a.时间,b.时间)
    ,人数=isnull(a.人数,b.人数)   
    from table1 a,(select * from table1) b
    where a.事件ID=1
    and b.事件ID=2
      

  4.   


     oh,my God, 我查了一下数据库,不仅仅是null,还有一些empty string,怎么判断呀?
     多谢,多谢!
      

  5.   


    update table1
    set 事件ID=isnull((case when a.事件ID ='' then null else a.事件ID end),b.事件ID)
    ,地点=isnull((case when a.地点 ='' then null else a.地点 end),b.地点)
    ,时间=isnull((case when a.时间 ='' then null else a.时间 end),b.时间)
    ,人数=isnull((case when a.人数 ='' then null else a.人数 end),b.人数)   
    from table1 a,(select * from table1) b
    where a.事件ID=1
    and b.事件ID=2自己再看一下吧!!
      

  6.   

    我觉得ID应该不会是null,否则更新也没用,
    而且直接连接自己不需要(select * from table1) b吧?update table1
    set 地点=isnull((case when a.地点 ='' then null else a.地点 end),b.地点)
    ,时间=isnull((case when a.时间 ='' then null else a.时间 end),b.时间)
    ,人数=isnull((case when a.人数 ='' then null else a.人数 end),b.人数)   
    from table1 a,table1 b
    where a.事件ID=1
    and b.事件ID=2