利用帶關聯子查詢Update語句更新資料

2022-04-01 08:33:08 字數 4128 閱讀 6952

update是t-sql中再簡單不過的語句了,update table set column=expression  [where condition],我們都會用到。但update的用法不僅於此,真正在開發的時候,靈活恰當地使用update可以達到事半功倍的效果。

假定有表table1(a,b,c)和table2(a,c),現在table1中有些記錄欄位c為null,要根據欄位a在table2中查詢,取出欄位a相等的字段c的值來更新table1。一種常規的思路,通過游標遍歷table1中欄位c為null的所有記錄,在迴圈體內查詢table2並進行更新,即用游標cursor的形式。測試sql語句如下:

使用游標遍歷方式更新

--1.建立測試表

create

table

table1(a 

varchar(10

),b 

varchar(10

),c 

varchar(10

),constraint

[pk_table1

]primary

keyclustered(a 

asc)

) on

[primary

]create

table

table2(a 

varchar(10

),c 

varchar(10

),constraint

[pk_table2

]primary

keyclustered(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.通過游標方式更新

declare

@name

varchar(10

)declare

@score

varchar(10

)declare

mycursor 

cursor

forselect

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 

wherea=

@name

update

table1 

setc 

=@score

wherea =

@name

fetch

next

from

mycursor 

into

@name

endclose

mycursor

deallocate

mycursor

go--

4.顯示更新後的結果

select

*from

table1

go--

5.刪除測試表

drop

table

table1

drop

table

table2

雖然用游標可以實現,但**看起來很複雜,其實用update根據子關聯來更新只要一條語句就可以搞定了,測試**如下:

使用帶關聯子查詢的update更新

--1.建立測試表

create

table

table1(a 

varchar(10

),b 

varchar(10

),c 

varchar(10

),constraint

[pk_table1

]primary

keyclustered(a 

asc)

) on

[primary

]create

table

table2(a 

varchar(10

),c 

varchar(10

),constraint

[pk_table2

]primary

keyclustered(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 

setc =(

select

c from

table2 

wherea =

table1.a) 

where

c is

null

go--

4.顯示更新後的結果

select

*from

table1

go--

5.刪除測試表

drop

table

table1

drop

table

table2

利用帶關聯子查詢Update語句更新資料

update是t sql中再簡單不過的語句了,update table set column expression where condition 我們都會用到。但update的用法不僅於此,真正在開發的時候,靈活恰當地使用update可以達到事半功倍的效果。假定有表table1 a,b,c 和ta...

利用帶關聯子查詢Update語句更新資料

update是 t sql 中再簡單不過的語句了,update table set column expression where condition 我們都會用到。但 update 的用法不僅於此,真正在開發的時候,靈活恰當地使用 update 可以達到事半功倍的效果。假定有表table1 a,b...

mysql關聯子查詢 MySQL 關聯子查詢

mysql 關聯子查詢 關聯子查詢是指乙個包含對錶的引用的子查詢,該錶也顯示在外部查詢中。通俗一點來講,就是子查詢引用到了主查詢的資料資料。以乙個實際的例子來理解關聯子查詢 article 文章表 aidtitlecontentuid 文章1文章1正文內容.文章2文章2正文內容.文章3文章3正文內容...