1、
例如表:
id       name       sex       tel                  qq       time
1       李先生       null  15088888888      null      2013-4-10
更新数据   null   男     150666666666     123456      null
得到结果  
id       name       sex       tel                  qq       time
1       李先生       男    15088888888      123456      2013-4-10------------------------------------------------------------------------------------------------2、
例如表:
id       name       sex       tel                  qq       time
1       李先生       null  15088888888      null      2013-4-10
更新数据   null   男     111111111     123456      null得到结果  
id       name       sex       tel                  qq       time
1       李先生       男    111111111       123456      2013-4-10

解决方案 »

  1.   

    第二个例子中tel不为空,为啥更新了?另外你要更新的数据有一些标识吗?如id和需要更新的相同?
      

  2.   

    update table set ziduan=xxxx where ziduan is null
      

  3.   

    update a
    set a.字段1= case when a.字段1 is null then b.字段1 else a.字段1 end ,把其他字段写上
    from tb a inner join 用于更新的表,假设为tb2 b on a.id= b.id
      

  4.   

    先问个问题,比如你这个例子:
    2、
    例如表:
    id       name       sex       tel                  qq       time
    1       李先生       null  15088888888      null      2013-4-10
    更新数据 在表中这里是否会有name和上面的对应?或者说这行的id和上面那个是一样的  null   男     111111111     123456      null
      

  5.   


    -------------------------------------------------------------------------------------
    表名([text].[dbo].[case])
    例如表:
    id       name       sex       tel            qq       time
    1       李先生     null   15088888888      null      2013-4-10
    UPDATE [text].[dbo].[case]
       SET [name] ='',[sex] = '男',[tel] = '150666666666',[qq] = '123456',[time] = ''
     WHERE id=1
    得到结果:id       name       sex       tel            qq       time
    1       李先生       男    15088888888      123456      2013-4-10
      

  6.   

    那太好办了:1、备份源表:
    2、
    select id,name,max(sex)sex,max(tel)tel,max(qq)qq,max(time)time into #t
    from tb
    group by id,name
    3、truncate tale tb
    4、insert into tb select * from #t
      

  7.   

    create table myuser
    (
    id int primary key,
    name nvarchar(20),
    sex nvarchar(20),
    tel nvarchar(20),
    qq nvarchar(20),
    time datetime
    )go
    insert into myuser values(1,'李先生',null,'15088888888',null,'2013-4-10')go
    select *  from myuser
    select *  from myuser1
    go
    --修改不是空值的数据
    --begin transactionupdate a
    set a.name= case when a.name is null then b.name else a.name end,
    a.sex= case when a.sex is null then b.sex else a.sex end,
    a.tel= case when a.tel is null then b.tel else a.tel end,
    a.qq= case when a.qq is null then b.qq else a.qq end,
    a.time= case when a.time is null then b.time else a.time end
    from myuser a inner join myuser1 b on a.id= b.id--rollback transaction
    select *  from myuser
    select *  from myuser1
    --只有不是空就更新
    --begin transaction
    update a
    set a.name= case when b.name is not null then b.name else a.name end,
    a.sex= case when b.sex is not null then b.sex else a.sex end,
    a.tel= case when b.tel is not null then b.tel else a.tel end,
    a.qq= case when b.qq is not null then b.qq else a.qq end,
    a.time= case when b.time is not null then b.time else a.time end
    from myuser a inner join myuser1 b on a.id= b.id 
      

  8.   

    select * from test
    id          name                                               addr
    ----------- -------------------------------------------------- ----------
    1           a,b,c,d                                            NULL
    2           NULL                                               234declare @name nvarchar(10);
    declare @addr nvarchar(10);
    set @name = 'wokaoa';
    set @addr = 'xiamen';
    update test set name=ISNULL(name,@name),addr=ISNULL(addr,@addr);select * from test
    id          name                                               addr
    ----------- -------------------------------------------------- ----------
    1           a,b,c,d                                            xiamen
    2           wokao                                              234
      

  9.   

    你的意思是,同一表中,某条记录的某个字段可能是空,但不知道哪个字段是NULL,你想要的结果有2个:
    (1)只更新字段是NULL的,非空的不更新。
    (2)字段是NULL要更新,有的字段比如TEL,虽然不为空,但是也要更新了。
    是这样吗?
      

  10.   


    上一个是对的哦,敢问这种情况怎么实现的
    表名([text].[dbo].[case])
    例如表:
    id       name       sex       tel            qq       time
    1       李先生     null   15088888888      null      2013-4-10
    UPDATE [text].[dbo].[case]
    SET [name] ='',[sex] = '男',[tel] = '1111111',[qq] = '123456',[time] = ''
    WHERE id=1
    得到结果:id       name       sex       tel            qq       time
    1       李先生       男    1111111      123456      2013-4-10 
      

  11.   

    呵呵,那第一个问题,你用5楼DBA_HUANGZJ的方法是可以的,只是所有的字段需要都列出来,这个方法我刚才试了,可以的呢,但是我写不出来。第二个问题,可以先用第一个问题的方法更新了空值字段,再用3楼的方法更新,这样是否满足你的要求?其他再简单的方法我目前没有,不知道dba_huangzj是否有好方法,我也想学习学习。
      

  12.   

    update table set ziduan=xxxx where ziduan is null