一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫 - Sql Server - 多表關(guān)聯(lián)同時(shí)更新多條不同的記錄方法分享

多表關(guān)聯(lián)同時(shí)更新多條不同的記錄方法分享

2019-12-10 14:42MSSQL教程網(wǎng) Sql Server

因?yàn)轫?xiàng)目要求實(shí)現(xiàn)一次性同時(shí)更新多條不同的記錄的需求,和同事討論了一個(gè)比較不錯(cuò)的方案,這里供大家參考下

以下為測(cè)試?yán)印?nbsp;
1.首先創(chuàng)建兩張臨時(shí)表并錄入測(cè)試數(shù)據(jù): 

復(fù)制代碼代碼如下:


create table #temptest1 

id int, 
name1 varchar(50), 
age int 

create table #temptest2 

id int, 
name1 varchar(50), 
age int 

 

查詢出此時(shí)的表數(shù)據(jù)為:

#temptest1                 #temptest2

多表關(guān)聯(lián)同時(shí)更新多條不同的記錄方法分享    多表關(guān)聯(lián)同時(shí)更新多條不同的記錄方法分享

 

2.現(xiàn)在要將#temptest2中的年齡更新到相應(yīng)的#temptest1中的年齡。

其實(shí)就是讓[表1]中ID為1的年齡改成19,同時(shí)ID為2的年齡改成20。

當(dāng)然這里的要求是只用一句SQL,不能用循環(huán)。

結(jié)果如下:

多表關(guān)聯(lián)同時(shí)更新多條不同的記錄方法分享

 

實(shí)現(xiàn)方法如下:

Update t1 

Set t1 .age = t2.age

From  #temptest1 t1

Join #temptest2 t2

On  t1.id = t2.id

 

(補(bǔ)充)Sql Server 2008 Merge命令寫法:

merge into #temptest1 t1 
using(select age,id from #temptest2) t2
on t1.id = t2.id
when matched then
update set t1.age = t2.age

 

是不是挺有趣的Sql。

如何一次性更新多條不同值的記錄
標(biāo)題可能沒說清楚,假設(shè)有這樣兩張表: 

復(fù)制代碼代碼如下:


create table testA( 
id number, 
eng varchar2(3), 
chi varchar2(3) 

create table testB( 
id number, 
eng varchar2(3), 
chi varchar2(3), 
anythingother varchar2(1) 


現(xiàn)有記錄 
testA: 
ID ENG CHI 
=============== 
1 a 一 
2 b 二 
3 c 三 
testB: 
ID ENG CHI ANY.... 
================= 
1 d 四 
2 e 五 
3 f 六 
我想把testB中的記錄的ENG,CHI字段更新到testA中去,以ID來對(duì)應(yīng)。 

CODE: 

SQL> set autot on 
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where exists (select 1 from tb where ta.a=tb.a); 
已更新4行。 
已用時(shí)間: 00: 00: 00.01 
執(zhí)行計(jì)劃 
---------------------------------------------------------- 
Plan hash value: 1137212925 
-------------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
-------------------------------------------------------------------------------- 
| 0 | UPDATE STATEMENT | | 5 | 165 | 20 (30)| 00:00:01 | 
| 1 | UPDATE | TA | | | | | 
|* 2 | HASH JOIN SEMI | | 5 | 165 | 5 (20)| 00:00:01 | 
| 3 | TABLE ACCESS FULL | TA | 5 | 100 | 2 (0)| 00:00:01 | 
| 4 | VIEW | VW_SQ_1 | 4 | 52 | 2 (0)| 00:00:01 | 
| 5 | TABLE ACCESS FULL| TB | 4 | 52 | 2 (0)| 00:00:01 | 
|* 6 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 | 
-------------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - access("TA"."A"="ITEM_1") 
6 - filter("TB"."A"=:B1) 
Note 
----- 
- dynamic sampling used for this statement (level=2) 
統(tǒng)計(jì)信息 
---------------------------------------------------------- 
0 recursive calls 
4 db block gets 
23 consistent gets 
0 physical reads 
1004 redo size 
840 bytes sent via SQL*Net to client 
856 bytes received via SQL*Net from client 
3 SQL*Net roundtrips to/from client 
1 sorts (memory) 
0 sorts (disk) 
4 rows processed 
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where ta.a= (select tb.a from tb where ta.a=tb.a); 
已更新4行。 
已用時(shí)間: 00: 00: 00.00 
執(zhí)行計(jì)劃 
---------------------------------------------------------- 
Plan hash value: 3571861550 
---------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
---------------------------------------------------------------------------- 
| 0 | UPDATE STATEMENT | | 1 | 20 | 7 (15)| 00:00:01 | 
| 1 | UPDATE | TA | | | | | 
|* 2 | FILTER | | | | | | 
| 3 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 | 
|* 4 | TABLE ACCESS FULL| TB | 1 | 13 | 2 (0)| 00:00:01 | 
|* 5 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 | 
---------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("TA"."A"= (SELECT "TB"."A" FROM "TB" "TB" WHERE 
"TB"."A"=:B1)) 
4 - filter("TB"."A"=:B1) 
5 - filter("TB"."A"=:B1) 
Note 
----- 
- dynamic sampling used for this statement (level=2) 
統(tǒng)計(jì)信息 
---------------------------------------------------------- 
11 recursive calls 
1 db block gets 
53 consistent gets 
0 physical reads 
588 redo size 
840 bytes sent via SQL*Net to client 
858 bytes received via SQL*Net from client 
3 SQL*Net roundtrips to/from client 
1 sorts (memory) 
0 sorts (disk) 
4 rows processed 



如果 create unique index tb_a_uidx on tb(a); 

[Copy to clipboard] [ - ] 

CODE: 

SQL> update (select ta.b tab1 ,tb.b tbb from ta,tb where ta.a=tb.a) set tab1=tbb; 
已更新4行。 
已用時(shí)間: 00: 00: 00.01 
執(zhí)行計(jì)劃 
---------------------------------------------------------- 
Plan hash value: 1761655026 
---------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
---------------------------------------------------------------------------- 
| 0 | UPDATE STATEMENT | | 4 | 184 | 5 (20)| 00:00:01 | 
| 1 | UPDATE | TA | | | | | 
|* 2 | HASH JOIN | | 4 | 184 | 5 (20)| 00:00:01 | 
| 3 | TABLE ACCESS FULL| TB | 4 | 104 | 2 (0)| 00:00:01 | 
| 4 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 | 
---------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - access("TA"."A"="TB"."A") 
Note 
----- 
- dynamic sampling used for this statement (level=2) 
統(tǒng)計(jì)信息 
---------------------------------------------------------- 
8 recursive calls 
4 db block gets 
17 consistent gets 
0 physical reads 
1004 redo size 
840 bytes sent via SQL*Net to client 
827 bytes received via SQL*Net from client 
3 SQL*Net roundtrips to/from client 
3 sorts (memory) 
0 sorts (disk) 
4 rows processed

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2021国产麻豆剧传媒剧情最新 | 国产在视频线精品视频 | 美女逼逼软件 | 国产精品原创永久在线观看 | 色呦呦在线免费观看 | 国产成人小视频在线观看 | 好大好硬好紧太深了受不了 | 国产va免费精品高清在线 | 欧美大b| 3x免费高清视频 | 国产在线激情视频 | 国产成人精品午夜在线播放 | 国产精品第1页在线播放 | 亚洲 综合 欧美在线视频 | 五月天色小说 | 亚洲婷婷在线视频 | 四虎永久视频 | 亚洲成人福利网站 | 荡女人人爱全文免费阅读 | 9re视频这里只有精品 | 免费看片黄 | 女教师的一级毛片 | 精品99在线观看 | 五月一区二区久久综合天堂 | 天天天综合网 | 好男人资源免费播放在线观看 | 男女性潮高片无遮挡禁18 | 我的绝色岳每雯雯 | 女人被男人躁得好爽免费视频 | 日本在线一区二区 | 500第一精品 | 天堂资源wwww在线看 | 北岛玲在线播放 | 欧美深夜在线 | 成人国产网站v片免费观看 成人国产精品视频 | 无人区在线观看免费国语完整版 | 亚洲国内精品 | 小浪妇奶真大水多 | 911精品国产亚洲日本美国韩国 | 三级aa久久 | 日本一区二区三区久久精品 |