sql server 語句 迴圈 游標 臨時表

2022-01-13 03:14:45 字數 2053 閱讀 9684

create table #tmp (i_sarinfor varchar(10))  -- 建立乙個臨時表

insert into #tmp

select a.i_sailorinfoid from

cert_sailorletter a

left join

crew_sailorinfo b

on a.i_sailorinfoid = b.i_sailorinfoid

left join

com_lettername c

on a.nvc_letternamecode = c.nvc_letternamecode

left join

com_position d

on b.nvc_positioncode = d.nvc_positioncode

left join

com_codec e

on e.i_codemid = '606'

and a.nvc_countrycode = e.nvc_code

left join

com_codec f

on f.i_codemid = '201'

and b.nvc_sailorstatecode = f.nvc_code

left join

com_codec g

on g.i_codemid = '308'

and a.nvc_locationcode = g.nvc_code

left join

com_position h

on a.nvc_suitpositioncode = h.nvc_positioncode

left join

fpd_client i

on a.i_clientid = i.i_clientid

left join

pfd_sailorhistory j

on b.i_sailorinfoid = j.i_sailorinfoid

and j.vc_isend = '1'

left join com_scanfile

on a.i_sailorletterid = com_scanfile.i_sailorletterid and com_scanfile.img_file is not null

where

1 = 1

and a.nvc_locationcode = '00002'

and b.nvc_sailorstatecode = '00001'

group by a.i_sailorinfoid --以上是查詢出來的資料放在臨時表中

declare @sql varchar(2000) --定義變數 大家懂

declare @i_sailorinfoid varchar(2000)

declare @bmzje varchar(2000)

declare bmcursor cursor for

select i_sarinfor from #tmp

open bmcursor

fetch next from bmcursor into @i_sailorinfoid ---這樣你就能迴圈取到每一行的i_sailorinforid值

while @@fetch_status=0

begin

set @sql='update cert_sailorletter set nvc_locationcode=''00004'' where nvc_locationcode=''00002'' and i_sailorinfoid='+@i_sailorinfoid+'' --要迴圈的sql 語句

exec(@sql) --執行

fetch next from bmcursor into @i_sailorinfoid

end

close bmcursor

deallocate bmcursor

sqlserver通過游標迴圈查詢

declare id int declare tempcursor cursor for select id from hrmresource where status in 0,1,2,3 order by id 建立游標tempcursor,並定義游標所指向的集合 open tempcursor...

sqlserver游標使用和迴圈

游標說簡單點都是設定乙個資料表的行指標,然後使用迴圈等運算元據 游標主要是用來完成複雜的業務邏輯 比如sqlserver中樹型查詢,比如如下業務點 資料表編號 名稱 父編號 1 中國 0 2 上海市 1 3 虹口區 2 4 楊浦區 2 顯示結果 中國 上海市 虹口區 中國 上海市 楊浦區 類似這樣的...

SQL SERVER迴圈遍歷(普通迴圈和游標迴圈)

自 1 首先需要乙個測試表資料student 2 普通迴圈 1 迴圈5次來修改學生表資訊 迴圈遍歷修改記錄 declare i int set i 0 while i 5 begin update student set demo i 5 where uid i set i i 1 end 檢視結果...