利用带关联子查询Update语句更新数据

 Update是T-sql中再简单不过的语句了,update table set column=expression  [where condition],我们都会用到。但update的用法不仅于此,真正在开发的时候,灵活恰当地使用update可以达到事半功倍的效果。

      假定有表Table1(a,b,c)和Table2(a,c),现在Table1中有些记录字段c为null,要根据字段a在Table2中查找,取出字段a相等的字段c的值来更新Table1。一种常规的思路,通过游标遍历Table1中字段c为null的所有记录,在循环体内查找Table2并进行更新,即用游标Cursor的形式。测试sql语句如下:

 

  1.  --1.创建测试表
  2.     create TABLE Table1
  3.     (
  4.         a varchar(10),
  5.         b varchar(10),
  6.         c varchar(10),
  7.         CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
  8.         (
  9.             a ASC
  10.         )
  11.     ) ON [PRIMARY]
  12.     create TABLE Table2
  13.     (
  14.         a varchar(10),
  15.         c varchar(10),
  16.         CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
  17.         (
  18.             a ASC
  19.         )
  20.     ) ON [PRIMARY]
  21.     GO
  22.     --2.创建测试数据
  23.     Insert into Table1 values('赵','asds',null)
  24.     Insert into Table1 values('钱','asds','100')
  25.     Insert into Table1 values('孙','asds','80')
  26.     Insert into Table1 values('李','asds',null)
  27.     Insert into Table2 values('赵','90')
  28.     Insert into Table2 values('钱','100')
  29.     Insert into Table2 values('孙','80')
  30.     Insert into Table2 values('李','95')
  31.     GO
  32.     select * from Table1
  33.     --3.通过游标方式更新
  34.     declare @name varchar(10)
  35.     declare @score varchar(10)
  36.     declare mycursor cursor for select a from Table1 where c is null
  37.     open mycursor
  38.     fetch next from mycursor into @name
  39.     while(@@fetch_status = 0)
  40.     BEGIN
  41.         select @score=c from Table2 where a=@name
  42.         update Table1 set c = @score where a = @name
  43.         fetch next from mycursor into @name    
  44.     END
  45.     close mycursor
  46.     deallocate mycursor
  47.     GO
  48.     --4.显示更新后的结果
  49.     select * from Table1
  50.     GO
  51.     --5.删除测试表
  52.     drop TABLE Table1
  53.     drop TABLE Table2
 虽然用游标可以实现,但代码看起来很复杂,其实用Update根据子关联来更新只要一条语句就可以搞定了,测试代码如下:


  1.  --1.创建测试表
  2.     create TABLE Table1
  3.     (
  4.         a varchar(10),
  5.         b varchar(10),
  6.         c varchar(10),
  7.         CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
  8.         (
  9.             a ASC
  10.         )
  11.     ) ON [PRIMARY]
  12.     create TABLE Table2
  13.     (
  14.         a varchar(10),
  15.         c varchar(10),
  16.         CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
  17.         (
  18.             a ASC
  19.         )
  20.     ) ON [PRIMARY]
  21.     GO
  22.     --2.创建测试数据
  23.     Insert into Table1 values('赵','asds',null)
  24.     Insert into Table1 values('钱','asds','100')
  25.     Insert into Table1 values('孙','asds','80')
  26.     Insert into Table1 values('李','asds',null)
  27.     Insert into Table2 values('赵','90')
  28.     Insert into Table2 values('钱','100')
  29.     Insert into Table2 values('孙','80')
  30.     Insert into Table2 values('李','95')
  31.     GO
  32.     select * from Table1
  33.     --3.通过Update方式更新
  34.     Update Table1 set c = (select c from Table2 where a = Table1.a) where c is null
  35.     GO
  36.     --4.显示更新后的结果
  37.     select * from Table1
  38.     GO
  39.     --5.删除测试表
  40.     drop TABLE Table1
  41.     drop TABLE Table2


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值