五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

深入SQL Cursor基本用法的詳細(xì)介紹

發(fā)布日期:2022-01-02 10:39 | 文章來(lái)源:CSDN

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

table1結(jié)構(gòu)如下
id int
name varchar(50) declare @id int
declare @name varchar(50)
declare cursor1 cursor for --定義游標(biāo)cursor1
select * from table1 --使用游標(biāo)的對(duì)象(跟據(jù)需要填入select文)
open cursor1 --打開游標(biāo) fetch next from cursor1 into @id,@name --將游標(biāo)向下移1行,獲取的數(shù)據(jù)放入之前定義的變量@id,@name中 while @@fetch_status=0 --判斷是否成功獲取數(shù)據(jù)
begin
update table1 set name=name+'1'
where id=@id --進(jìn)行相應(yīng)處理(跟據(jù)需要填入SQL文) fetch next from cursor1 into @id,@name --將游標(biāo)向下移1行
end close cursor1 --關(guān)閉游標(biāo)
deallocate cursor1

游標(biāo)一般格式:
DECLARE 游標(biāo)名稱 CURSOR FOR SELECT 字段1,字段2,字段3,... FROM 表名 WHERE ...
OPEN 游標(biāo)名稱
FETCH NEXT FROM 游標(biāo)名稱 INTO 變量名1,變量名2,變量名3,...
WHILE @@FETCH_STATUS=0
BEGIN
SQL語(yǔ)句執(zhí)行過(guò)程... ...
FETCH NEXT FROM 游標(biāo)名稱 INTO 變量名1,變量名2,變量名3,...
END
CLOSE 游標(biāo)名稱
DEALLOCATE 游標(biāo)名稱 (刪除游標(biāo))
復(fù)制代碼 代碼如下:

例子:
/*
功能:數(shù)據(jù)庫(kù)表格tbl_users數(shù)據(jù)
deptid userid username
1 100 a
1 101 b
2 102 c
要求用一個(gè)sql語(yǔ)句輸出下面結(jié)果
deptid username
1 ab
2 c
[要求用游標(biāo)實(shí)現(xiàn)設(shè)計(jì): OK_008
時(shí)間: 2006-05
備注:無(wú)
*/
create table #Temp1(deptid int,userid int,username varchar(20)) --待測(cè)試的數(shù)據(jù)表
create table #Temp2(deptid int,username varchar(20)) --結(jié)果表
--先把一些待測(cè)試的數(shù)據(jù)插入到待測(cè)試表#Temp1中
insert into #Temp1
select 1,100,'a' union all
select 1,101,'b' union all
select 1,131,'d' union all
select 1,201,'f' union all
select 2,302,'c' union all
select 2,202,'a' union all
select 2,221,'e' union all
select 3,102,'y' union all
select 3,302,'e' union all
select 3,121,'t'
--
declare @deptid int,@username varchar(20)
--定義游標(biāo)
declare Select_cursor cursor for
select deptid,username from #Temp1
open Select_cursor
fetch next from Select_cursor into @deptid,@username --提取操作的列數(shù)據(jù)放到局部變量中
while @@fetch_status=0 --返回被 FETCH 語(yǔ)句執(zhí)行的最后游標(biāo)的狀態(tài)
/*
@@FETCH_STATUS =0 FETCH 語(yǔ)句成功
@@FETCH_STATUS =-1 FETCH 語(yǔ)句失敗或此行不在結(jié)果集中
@@FETCH_STATUS =-2 被提取的行不存在
*/
begin
--當(dāng)表#Temp2列deptid存在相同的數(shù)據(jù)時(shí),就直接在列username上追加@username值
if(exists(select * from #Temp2 where deptid=@deptid ))
update #Temp2 set username=username +@username where deptid=@deptid
else
--插入新數(shù)據(jù)
insert into #Temp2 select @deptid,@username
fetch next from Select_cursor into @deptid,@username
end
close Select_cursor
deallocate Select_cursor
select * from #Temp2 --測(cè)試結(jié)果
Drop table #Temp1,#Temp2

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部