利用帶關(guān)聯(lián)子查詢Update語句更新數(shù)據(jù)的方法
Update是T-sql中再簡單不過的語句了,update table set column=expression [where condition],我們都會用到。但update的用法不僅于此,真正在開發(fā)的時(shí)候,靈活恰當(dāng)?shù)厥褂胾pdate可以達(dá)到事半功倍的效果。
假定有表Table1(a,b,c)和Table2(a,c),現(xiàn)在Table1中有些記錄字段c為null,要根據(jù)字段a在Table2中查找,取出字段a相等的字段c的值來更新Table1。一種常規(guī)的思路,通過游標(biāo)遍歷Table1中字段c為null的所有記錄,在循環(huán)體內(nèi)查找Table2并進(jìn)行更新,即用游標(biāo)Cursor的形式。測試sql語句如下:
--1.創(chuàng)建測試表
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.創(chuàng)建測試數(shù)據(jù)
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.通過游標(biāo)方式更新
declare @name varchar(10)
declare @score varchar(10)
declare mycursor cursor for select a from Table1 where c is null
open mycursor
fetch next from mycursor into @name
while(@@fetch_status = 0)
BEGIN
select @score=c from Table2 where a=@name
update Table1 set c = @score where a = @name
fetch next from mycursor into @name
END
close mycursor
deallocate mycursor
GO
--4.顯示更新后的結(jié)果
select * from Table1
GO
--5.刪除測試表
drop TABLE Table1
drop TABLE Table2
雖然用游標(biāo)可以實(shí)現(xiàn),但代碼看起來很復(fù)雜,其實(shí)用Update根據(jù)子關(guān)聯(lián)來更新只要一條語句就可以搞定了,測試代碼如下:
--1.創(chuàng)建測試表
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.創(chuàng)建測試數(shù)據(jù)
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.顯示更新后的結(jié)果
select * from Table1
GO
--5.刪除測試表
drop TABLE Table1
drop TABLE Table2
參考資料:也許是被忽略的update語句,update 子查詢
相關(guān)文章
海量數(shù)據(jù)庫的查詢優(yōu)化及分頁算法方案集合2/2
海量數(shù)據(jù)庫的查詢優(yōu)化及分頁算法方案集合2/2...2007-03-03
Navicat?premium?for?mac?12的安裝破解圖文教程
Navicat Premium是一款數(shù)據(jù)庫管理工具,將此工具連接數(shù)據(jù)庫,你可以從中看到各種數(shù)據(jù)庫的詳細(xì)信息,這篇文章主要介紹了Mac下Navicat?premium?for?mac?12的安裝破解過程,需要的朋友可以參考下2024-01-01
最近關(guān)于Navicat到期的完美解決辦法(親測有效)
這篇文章主要介紹了最近關(guān)于Navicat到期的完美解決辦法(親測有效),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
Django項(xiàng)目優(yōu)化數(shù)據(jù)庫操作總結(jié)
這篇文章主要介紹了Django項(xiàng)目中優(yōu)化數(shù)據(jù)庫操作總結(jié),有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家共進(jìn)步,早日升職加薪2021-09-09
在PostgreSQL中使用日期類型時(shí)一些需要注意的地方
這篇文章主要介紹了在PostgreSQL中使用日期類型時(shí)一些需要注意的地方,包括時(shí)間戳和日期轉(zhuǎn)換等方面,需要的朋友可以參考下2015-04-04

