码迷,mamicode.com
首页 > 数据库 > 详细

sql update小结

时间:2014-06-19 07:34:09      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:使用   strong   set      应用   sql   

以前update用的不少,但都是简单的单表操作,没有在意,最近查阅多表关联更新及更新top n,发现update还真灵活,记录如下(在mssqlserver2008r2下测试通过):

1单表操作 

update table1 set col1=val[,col2=val2...]

[where 条件表达式] 

2多表关联操作

1)update 表名 set 列名 from 表1,表2 where 条件,此法源表(table1)不能用as别名,长表名很麻烦,如下:

update table1 set col1=table2.col1

from table2  where table1.pkey=table2.pkey

2)使用别名表更新,简洁!

update t1 set col1=t2.col1

from table1 t1,table2 t2  where t1.pkey=t2.pkey

3)更新来自查询表

update t1 set col1=val

from (select * from table1 [where 条件表达式] )t1

应用:更新前n条

update t1 set col1=val

from (select top 10 * from table1 [where 条件表达式] order by 列n )t1

另外,查询可以是本库中的表,也可以是外库表,如dblink连接的表或openrowset等

4)使用cte

;with t as(select * from table1  [where 条件表达式])

update t set  col1=val

 

sql update小结,布布扣,bubuko.com

sql update小结

标签:使用   strong   set      应用   sql   

原文地址:http://www.cnblogs.com/pw33/p/3793900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!