龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > Sql Server开发 >

mssql sql高效关联子查询的update 批量更新

时间:2013-01-03 18:08来源:未知 作者:admin 点击:
分享到:
mssql sql高效关联子查询的update 批量更新 mssql sql高效关联子查询的update 批量更新 /* 使用带关联子查询的Update更新 --1.创建测试表 create TABLE Table1 ( a varchar(10), b varchar(10), c varchar(10), CONST
mssql sql高效关联子查询的update 批量更新

mssql sql高效关联子查询的update 批量更新
/*
使用带关联子查询的Update更新 
  --1.创建测试表  
 

create TABLE Table1 
  ( 
  a varchar(10), 
  b varchar(10), 
  c varchar(10), 
  CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
  ( 
  a ASC 
  ) 
  ) ON [PRIMARY] 

  create TABLE Table2 
  ( 
  a varchar(10), 
  c varchar(10), 
  CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED 
  ( 
  a ASC 
  ) 
  ) ON [PRIMARY] 
  GO 
  --2.创建测试数据 
  Insert into Table1 values('赵','asds',null) 
  Insert into Table1 values('钱','asds','100') 
  Insert into Table1 values('孙','asds','80') 
  Insert into Table1 values('李','asds',null) 

  Insert into Table2 values('赵','90') 
  Insert into Table2 values('钱','100') 
  Insert into Table2 values('孙','80') 
  Insert into Table2 values('李','95') 
  GO 
  select * from Table1 

  --3.通过Update方式更新 
  Update Table1 set c = (select c from Table2 where a = Table1.a) where c is null 
  GO 

  --4.显示更新后的结果 
  select * from Table1 
  GO 
  --5.删除测试表 
  drop TABLE Table1 
  drop TABLE Table2


精彩图集

赞助商链接