深入SQL Cursor基本用法的詳細(xì)介紹
更新時間:2013年06月11日 11:01:51 作者:
本篇文章是對SQL Cursor的基本用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
由于這個游標(biāo) 執(zhí)行一下就相當(dāng)于SELECT一下 其效率不敢恭維也沒做深入研究。
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)的對象(跟據(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語句執(zhí)行過程... ...
FETCH NEXT FROM 游標(biāo)名稱 INTO 變量名1,變量名2,變量名3,...
END
CLOSE 游標(biāo)名稱
DEALLOCATE 游標(biāo)名稱 (刪除游標(biāo))
例子:
/*
功能:數(shù)據(jù)庫表格tbl_users數(shù)據(jù)
deptid userid username
1 100 a
1 101 b
2 102 c
要求用一個sql語句輸出下面結(jié)果
deptid username
1 ab
2 c
[要求用游標(biāo)實(shí)現(xiàn)設(shè)計: OK_008
時間: 2006-05
備注:無
*/
create table #Temp1(deptid int,userid int,username varchar(20)) --待測試的數(shù)據(jù)表
create table #Temp2(deptid int,username varchar(20)) --結(jié)果表
--先把一些待測試的數(shù)據(jù)插入到待測試表#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 語句執(zhí)行的最后游標(biāo)的狀態(tài)
/*
@@FETCH_STATUS =0 FETCH 語句成功
@@FETCH_STATUS =-1 FETCH 語句失敗或此行不在結(jié)果集中
@@FETCH_STATUS =-2 被提取的行不存在
*/
begin
--當(dāng)表#Temp2列deptid存在相同的數(shù)據(jù)時,就直接在列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 --測試結(jié)果
Drop table #Temp1,#Temp2
復(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)的對象(跟據(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語句執(zhí)行過程... ...
FETCH NEXT FROM 游標(biāo)名稱 INTO 變量名1,變量名2,變量名3,...
END
CLOSE 游標(biāo)名稱
DEALLOCATE 游標(biāo)名稱 (刪除游標(biāo))
復(fù)制代碼 代碼如下:
例子:
/*
功能:數(shù)據(jù)庫表格tbl_users數(shù)據(jù)
deptid userid username
1 100 a
1 101 b
2 102 c
要求用一個sql語句輸出下面結(jié)果
deptid username
1 ab
2 c
[要求用游標(biāo)實(shí)現(xiàn)設(shè)計: OK_008
時間: 2006-05
備注:無
*/
create table #Temp1(deptid int,userid int,username varchar(20)) --待測試的數(shù)據(jù)表
create table #Temp2(deptid int,username varchar(20)) --結(jié)果表
--先把一些待測試的數(shù)據(jù)插入到待測試表#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 語句執(zhí)行的最后游標(biāo)的狀態(tài)
/*
@@FETCH_STATUS =0 FETCH 語句成功
@@FETCH_STATUS =-1 FETCH 語句失敗或此行不在結(jié)果集中
@@FETCH_STATUS =-2 被提取的行不存在
*/
begin
--當(dāng)表#Temp2列deptid存在相同的數(shù)據(jù)時,就直接在列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 --測試結(jié)果
Drop table #Temp1,#Temp2
您可能感興趣的文章:
相關(guān)文章
SQL2000個人版 應(yīng)用程序正常初始化失敗0乘以C0000135失敗
應(yīng)用程序正常初始化(0*c0000135)失敗。是什么意思?2011-01-01
數(shù)據(jù)庫中identity字段不必是系統(tǒng)產(chǎn)生的唯一值 性能優(yōu)化方法(新招)
具有identity特性的字段,其值是系統(tǒng)產(chǎn)生的,自動增加的,所以,一般把這個用在一個表的主鍵上。2011-09-09
SQL Server中將查詢結(jié)果轉(zhuǎn)換為Json格式腳本分享
這篇文章主要介紹了SQL Server中將查詢結(jié)果轉(zhuǎn)換為Json格式腳本分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-02-02
SQL Server 2017無法連接到服務(wù)器的問題解決
本文主要介紹了SQL Server 2017無法連接到服務(wù)器的問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
SQL SERVER數(shù)據(jù)庫的作業(yè)的腳本及存儲過程
本站文章旨在為該問題提供解決思路及關(guān)鍵性代碼,并不能完成應(yīng)該由網(wǎng)友自己完成的所有工作,請網(wǎng)友在仔細(xì)看文章并理解思路的基礎(chǔ)上舉一反三、靈活運(yùn)用2015-10-10
SQL Server 中的數(shù)據(jù)類型隱式轉(zhuǎn)換問題
這篇文章主要介紹了SQL Server 中的數(shù)據(jù)類型隱式轉(zhuǎn)換問題,本文給大家介紹的非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
SQL SERVER 2012新增函數(shù)之字符串函數(shù)FORMAT詳解
這篇文章主要給大家介紹了關(guān)于SQL SERVER 2012新增函數(shù)之字符串函數(shù)FORMAT的相關(guān)資料,文中通過實(shí)例介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
我也有微信朋友圈了 Android實(shí)現(xiàn)
最近寫了一個簡單的朋友圈程序,包含了朋友圈的列表實(shí)現(xiàn),視頻的錄制、預(yù)覽與上傳,圖片可選擇拍照或者從相冊選取,從相冊選取可以一次選擇多張照片,并且限制照片的張數(shù),想擁有真正屬于自己的朋友圈嗎?快來圍觀2016-05-05

