遊標(cursor)是系統爲用戶開設的一個數據緩衝區,存放SQL語句的執行結果。每個遊標區都有一個名字。用戶可以用SQL語句逐一從遊標中獲取記錄,並賦給主變量,交由主語言進一步處理。
2. 遊標種類
MS SQL SERVER 支持三種類型的遊標:Transact_SQL 遊標,API 服務器遊標和客戶遊標。
(1) Transact_SQL 遊標
Transact_SQL 遊標是由DECLARE CURSOR 語法定義、主要用在Transact_SQL 腳本、
存儲過程和
觸發器中。Transact_SQL
遊標主要用在服務器上,由從
客戶端發送給服務器的Transact_SQL 語句或是
批處理、存儲過程、觸發器中的Transact_SQL
進行管理。 Transact_SQL 遊標不支持提取數據塊或多行數據。
(2) API遊標
API 遊標支持在OLE DB, ODBC 以及DB_library 中使用遊標函數,主要用在服務器上。每一次客戶端應用程序調用API 遊標函數,MS SQL SEVER 的OLE DB 提供者、ODBC驅動器或DB_library 的動態鏈接庫(DLL) 都會將這些客戶請求傳送給服務器以對API遊標進行處理。
(3) 客戶遊標
客戶遊標主要是當在客戶機上緩存結果集時才使用。在客戶遊標中,有一個缺省的結果集被用來在客戶機上緩存整個結果集。客戶遊標僅支持
靜態遊標而非
動態遊標。由於服務器遊標並不支持所有的
Transact-SQL 語句或批處理,所以客戶遊標常常僅被用作服務器遊標的輔助。因爲在一般情況下,服務器遊標能支持絕大多數的遊標操作。由於API
遊標和Transact-SQL 遊標使用在服務器端,所以被稱爲服務器遊標,也被稱爲
後臺遊標,而客戶端遊標被稱爲前臺遊標。在本章中我們主要講述服務器(後臺)遊標
遊標使用示例(1)
create proc cursorTest
@_id int=0,
@_name varchar(50)=''
as
--創建遊標
declare @cursor cursor
--設定遊標欲操作的
數據集
set @cursor=cursor for
select _id,_name from users
--打開遊標
open @cursor
--移動遊標指向到第一條數據,提取第一條數據存放在變量中
fetch next from @cursor into @_id,@_name
--如果上一次操作成功則繼續循環
while(
@@fetch_status=0)
begin
--操作提出的數據
print @_name
--繼續提下一行
fetch next from @cursor into @_id,@_name
end
--關閉遊標
close @cursor
--刪除遊標
deallocate @curso
遊標使用示例(2)
CREATE proc [dbo].[As_Proc_GetPlanDetail]
(
@ids varchar(max)--
)
as
create table #temp
(
dept varchar(100),
sorttitle varchar(200),
title varchar(200),
spec varchar(50),
model varchar(50),
budget decimal(18,2),
amount int,
sgamount int,
htamount int,
ysamount int,
fenfamount int
)
declare @did varchar(32)
declare @sorttitle varchar(200)
declare @title varchar(200)
declare @spec varchar(50)
declare @model varchar(50)
declare @budget decimal(18,2)
declare @amount int
declare @sgamount int
declare @htamount int
declare @ysamount int--驗收數量
declare @fenfamount int--分發數量
declare cursor1 cursor for
select rowid,dbo.clip(ypdept,':',1) from As_year_plan where Charindex(rowid,@ids,0)>0 union select rowid,dbo.clip(department,':',1) from As_assets_requisition where Charindex(rowid,@ids,0)>0 -----------------------------------------------控制計劃
declare @rowid varchar(32)
declare @dept varchar(100)
open cursor1
fetch next from cursor1 into @rowid,@dept
while @@fetch_status=0
begin
declare cursor2 cursor for select a.rowid, b.sorttitle,a.title,a.spec,a.model,a.budget,a.amount from As_assts_planDetai a left join As_assetsSort b on a.sortId=b.rowid where planID=@rowid
open cursor2
fetch next from cursor2 into @did, @sorttitle, @title,@spec, @model ,@budget ,@amount
while @@fetch_status=0
begin
select @sgamount= isnull(sum(a.amount),0) from As_assets_buyDetail a inner join As_assets_buy b on a.buyId=b.rowid where a.planId=@did and b.state=1
--print @did
select @htamount= isnull(sum(a.amount),0) from As_contract_detail a inner join As_stock_contract b on a.contId=b.rowid where b.state=1 and a.bydetId in(
select c.rowid from As_assets_buyDetail c inner join As_assets_buy d on c.buyId=d.rowid where c.planId=@did and d.state=1)
---驗收數量
select @ysamount=count(1) from As_AssetsInfo a inner join As_contract_detail b on a. contractId=b.rowid where b.bydetId in(select rowid from As_assets_buyDetail where planId=@did)
--分發數量
select @fenfamount=count(1) from As_AssetsInfo a inner join As_contract_detail b on a. contractId=b.rowid where b.bydetId in(select rowid from As_assets_buyDetail where planId=@did) and a.drawState=1
insert into #temp(dept, sorttitle, title , spec, model , budget ,amount,sgamount,htamount, ysamount,fenfamount) values(@dept,@sorttitle, @title , @spec, @model , @budget ,@amount,@sgamount,@htamount,@ysamount,@fenfamount)
fetch next from cursor2 into @did, @sorttitle, @title,@spec, @model ,@budget ,@amount
end
close cursor2 --關閉遊標
deallocate cursor2
fetch next from cursor1 into @rowid,@dept
end
close cursor1 --關閉遊標
deallocate cursor1 --釋放遊標
--查詢臨時表
select * from #temp
遊標詳細說明
RS.OPEN SQL,CONN,A,B
參數A爲設定遊標的類型,其取值爲:
0 僅向前遊標,只能向前瀏覽記錄,不支持
分頁、Recordset、BookMark
1 鍵集遊標,其他用戶對記錄所做的修改將反映到記錄集中,但其他用戶增加或刪除記錄不會反映到記錄集中。支持分頁、Recordset、BookMark
2 動態遊標功能最強,但耗資源也最多。用戶對記錄所做的修改,增加或刪除記錄都將反映到記錄集中。支持全功能瀏覽。
3 靜態遊標,只是數據的一個
快照,用戶對記錄所做的修改,增加或刪除記錄都不會反映到記錄集中。支持向前或向後移動
參數B爲記錄集的鎖定類型,其取值爲:
1 鎖定類型,默認的,只讀,不能作任何修改
2 當編輯時立即鎖定記錄,最安全的方式
3 只有在調用Update方法時才鎖定記錄集,而在此前的其他操作仍可對當前記錄進行更改、插入和刪除等
4 當編輯時記錄不會被鎖定,而更改、插入和刪除是在批處理方式下完成的
打開數據記錄集方法其實不止一種,但是我們用的最多的就是
rs.open sql,1,1的方法,可是後面的數字參數很多人不解其意,下面我們來介紹一下。
其實open方法後面有多個參數
CursorType LockType CommandType
比如 rs.open sql,1,1
也可以寫成
rs.cursorType = 1
rs.LockType = 1
rs.open sql
其中CursorType代表從一個表或者一個SQL查詢結果返回的記錄。
這個參數有四個值分別是:
adOpenForwardOnly 表示只允許在記錄集內的記錄間往前移動。這個是
缺省值。
adOpenKeyset 反映由其它用戶所做的對記錄的改變或者刪除動作,但並不反映由其它用戶做作的添加新記錄的動作。
adOpenDynamic 反映由其它用戶所做的對記錄的改變或者刪除動作,包括添加的新記錄
adOpenStatic 不反映其它用戶對記錄所做的修改,添加,刪除動作。
這四個值VBSCRIPT預定義位
adOpenForwardOnly = 0
adOpenKeyset = 1
adOpenDynamic = 2
adOpenStatic = 3
lockType 表示當打開記錄集時,數據提供者用於鎖定數據庫的類型:
adLockReadOnly 數據不能改變,這是缺省值!
adLockPessimistic 數據提供者在開始編輯數據的時候鎖定記錄
adLockOptimistic 僅當調用update方法時,數據提供者鎖定記錄
adLockBatchOptimistic 用於批處理修改
他們的
常量值定義分別是:
adLockReadOnly = 1
adLockPessimistic = 2
adLockOptimistic = 3
adLockBatchOptimistic = 4
rs.open sql,conn,1,1 讀取記錄 select
rs.open sql,conn,1,3 只更新記錄最好 update
rs.open sql,conn,2,3 插入和刪除最好 insert delete