分批次更新同一个表中的相同字段,急!!!
时间:2011-12-04
来源:互联网
表名为:COMUST
表结构如下:
Pnumber name Bchno Pset Pact
1 HB500 7901 2404 2369
1 HC300 7901 560 578
1 water 7901 344 349
1 HB500 7902 2422 2479
1 HC300 7902 542 547
1 water 7902 339 372
Pnumber 为主键,我想同时更新Bchno 为 7901,name为HB500, Bchno 为 7902,name为HB500的Pact值,并且Bchno 为 7901,name为HB500这条记录的Pact值更新为2404,Bchno 为 7902,name为HB500这条记录的Pact值更新为2422,用SQ语句该怎么做呢?
请教各位高手,很急啊,万分感谢!!!
表结构如下:
Pnumber name Bchno Pset Pact
1 HB500 7901 2404 2369
1 HC300 7901 560 578
1 water 7901 344 349
1 HB500 7902 2422 2479
1 HC300 7902 542 547
1 water 7902 339 372
Pnumber 为主键,我想同时更新Bchno 为 7901,name为HB500, Bchno 为 7902,name为HB500的Pact值,并且Bchno 为 7901,name为HB500这条记录的Pact值更新为2404,Bchno 为 7902,name为HB500这条记录的Pact值更新为2422,用SQ语句该怎么做呢?
请教各位高手,很急啊,万分感谢!!!
作者: Nong_Ming_Shuai 发布时间: 2011-12-04
SQL code
update tb set pact=2404 where (bchno =7901 and name='HB500') update tb set pact=2422 where (bchno =7902 and name='HB500')
作者: ssp2009 发布时间: 2011-12-04
SQL code
update a set Pact=b.Pset from tb a, (select px=ROW_NUMBER()over(partition by Bchno order by GETDATE()),* from tb)b where a.Bchno=b.Bchno and a.Pnumber=b.Pnumber and b.px=1 select * from tb
作者: fredrickhu 发布时间: 2011-12-04
SQL code
---------------------------------------------------------------- -- Author :fredrickhu(小F,向高手学习) -- Date :2011-12-04 20:14:30 -- Version: -- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) -- Jul 9 2008 14:43:34 -- Copyright (c) 1988-2008 Microsoft Corporation -- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3) -- ---------------------------------------------------------------- --> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([Pnumber] int,[name] varchar(5),[Bchno] int,[Pset] int,[Pact] int) insert [tb] select 1,'HB500',7901,2404,2369 union all select 1,'HC300',7901,560,578 union all select 1,'water',7901,344,349 union all select 1,'HB500',7902,2422,2479 union all select 1,'HC300',7902,542,547 union all select 1,'water',7902,339,372 --------------开始查询-------------------------- update a set Pact=b.Pset from tb a, (select px=ROW_NUMBER()over(partition by Bchno order by GETDATE()),* from tb)b where a.Bchno=b.Bchno and a.Pnumber=b.Pnumber and a.name=b.name and b.px=1 select * from tb ----------------结果---------------------------- /* Pnumber name Bchno Pset Pact ----------- ----- ----------- ----------- ----------- 1 HB500 7901 2404 2404 1 HC300 7901 560 578 1 water 7901 344 349 1 HB500 7902 2422 2422 1 HC300 7902 542 547 1 water 7902 339 372 (6 行受影响) */
作者: fredrickhu 发布时间: 2011-12-04
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28