游标实例
--项目实例
--1
declare @pk_user int
declare @organizeid int
declare Type_Cursor CURSOR forward_only FOR
select top 1000000 pk_user,organizeid from EAP.dbo.zh_user
where dr=0 and username in (
select cardno from EAP.dbo.zh_card where dr=0 and cardno in
(
select username from EAP.dbo.zh_user zu,EAP.dbo.am_organize ao
where topid in (select organizeid from EAP.dbo.zh_user where username =@cardno)
and zu.dr=0 and ao.organizeid=zu.organizeid)
and cardclass in (2,3)
)
open Type_Cursor
fetch next from Type_Cursor into @pk_user,@organizeid
while(@@fetch_status=0)
begin
select @pk_user,@organizeid
exec ProCLearByUserID @pk_user,@organizeid
fetch next from Type_Cursor into @pk_user,@organizeid
end
CLOSE Type_Cursor
deallocate Type_Cursor
--2
declare cur_MyCur1 cursor for select companyname from dbo.ViewAgentRelationUser where Topid is not null group by companyname
open cur_MyCur1
fetch cur_MyCur1 into @CorpName
while (@@FETCH_STATUS=0)
begin
declare cur_MyCur2 cursor for select Topid from ViewAgentRelationUser
where CompanyName=@CorpName and Topid is not null
open cur_MyCur2
fetch cur_MyCur2 into @Topid
set @v_Topid='-1'
while (@@FETCH_STATUS=0)
begin
set @v_Topid=@v_Topid+','+@Topid
fetch cur_MyCur2 into @Topid
END
close cur_MyCur2
deallocate cur_MyCur2
--自己测试小实例
declare cur cursor
read_only
for select username from BackUser where userId <=10
declare @username varchar(20)
open cur
fetch next from cur into @username
while(@@FETCH_STATUS =0)
begin
print @username
fetch next from cur into @username
end
close cur
deallocate cur
go
-----------------------
declare abc scroll cursor
for
select username from BackUser where userId <=10
declare @MyCursor cursor
set @Mycursor = abc
declare @MyCursor2 cursor
set @MyCursor2 = cursor local scroll FOR
select username from BackUser where userId <=10
----------------------1
declare contact_cursor cursor
for select * from BackUser where userId <=10
open contact_cursor
fetch next from contact_cursor
while @@FETCH_STATUS=0
begin
fetch next from contact_cursor
end
close contact_cursor
deallocate contact_cursor
-------------------------------2
declare @username varchar(50),@companyName varchar(50)
declare contact_cursor cursor for
select username,companyName from backuser where userId<=10
open contact_cursor
fetch next from contact_cursor into @username,@companyName
while @@FETCH_STATUS =0
begin
print 'UserName:'+@username+' CompanyName:'+@companyName
fetch next from contact_cursor into @username,@companyName
end
close contact_cursor
deallocate contact_cursor
go
------------------------------3
declare contact_cursor scroll cursor for
select username,companyName from backuser where userId<=10
open contact_cursor
fetch first from contact_cursor
fetch last from contact_cursor
fetch absolute 2 from contact_cursor
fetch relative -2 from contact_cursor
close contact_cursor
deallocate contact_cursor
go
--========================================= 使用循环游标
先遍历店铺类型,再遍历交易类型,
实现每个店铺每个交易类型遍历一次。
ALTER procedure [dbo].[USP_RTAS_T_GetTransactionSummaryReport]
as
DECLARE @Store_No VARCHAR(20)
DECLARE @StoreName VARCHAR(50)
declare @TRANSACTION_TYPE_ID INT
declare @TRANSACTION_TYPE_Name varchar(50)
DELETE FROM RTAS_T_GetTransactionSummaryReport
declare curStore cursor read_only for
SELECT Store_No,StoreName FROM dbo.Store
declare curType cursor read_only for
SELECT TRANSACTION_TYPE_ID,TRANSACTION_TYPE_Name FROM dbo.TransactionType WHERE TRANSACTION_TYPE_ID NOT IN (2,10)
open curType
fetch next from curType into @TRANSACTION_TYPE_ID,@TRANSACTION_TYPE_Name
while(@@FETCH_STATUS =0)
begin
OPEN curStore
fetch next from curStore into @Store_No,@StoreName
while(@@FETCH_STATUS =0)
BEGIN
INSERT INTO RTAS_T_GetTransactionSummaryReport(Store_No,StoreName,TRANSACTION_TYPE_ID,TRANSACTION_TYPE_Name)
SELECT @Store_No,@StoreName,@TRANSACTION_TYPE_ID,@TRANSACTION_TYPE_Name
fetch next from curStore into @Store_No,@StoreName
END
close curStore
fetch next from curType into @TRANSACTION_TYPE_ID,@TRANSACTION_TYPE_Name
END
close curType
deallocate curStore
deallocate curType