t-sql 遊標,t-sql

來源:互聯網
上載者:User

t-sql 遊標,t-sql

Ø 遊標

     遊標可以對一個select的結果集進行處理,或是不需要全部處理,就會返回一個對記錄集進行處理之後的結果。

工作上的執行個體create PROCEDURE [dbo].[WeekTotalPowerBingTuByWeek]       @DevName varchar(2000),       @starttime varchar(200)ASBEGINSET NOCOUNT ON;--Set @DevName='電錶1(DTL1110321471),dianbiao$DTL1110321471;電錶2(DTL1110321470),dianbiao$DTL1110321470;'Declare @pointID varchar(300)Declare @pointName varchar(300)Create table #tempTable(  pointName varchar(200),  pointValue float)create table #tempTables(   pointID varchar(2000),   pointName varchar(2000)    )declare @temp table(col varchar(2000))--set @str=replace(@str,'|',',')while(len(@DevName)>0)begininsert into @tempselect left(@DevName,charindex(';',@DevName)-1)set @DevName=stuff(@DevName,1,charindex(';',@DevName),'')enddeclare @name varchar(2000)declare @value varchar(2000)declare cur cursor for  select * from @tempopen cur--開啟遊標fetch next from cur into @name while @@FETCH_STATUS=0  --判斷是否成功擷取資料 begin declare @str1 varchar(2000) declare @str2 varchar(2000) select @str1=left(@name,charindex(',',@name)-1) select @str2=substring(@name,charindex(',',@name)+1,LEN(@name))  insert into #tempTables values (@str2,@str1) fetch next from cur into @name endclose cur --關閉遊標deallocate curDeclare @starttimes varchar(200)Declare @endtime varchar(200)select @starttimes=dateadd(day,-datepart(weekday,@starttime)+1,@starttime)--開始時間select @endtime=dateadd(day,7-datepart(weekday,@starttime)+2,@starttime)--結束時間Declare Cur cursor forselect pointID,pointName from #tempTablesopen Curfetch next from cur into @pointID,@pointName while @@FETCH_STATUS=0  --判斷是否成功擷取資料 begincreate table #temp(   inValue float)declare @inValue floatDeclare @sql varchar(8000)Set @sql='insert into #temp select round(SUM(incrementValue),2) as incrementValue  from TAB_DAY_'+@pointID+'  where  GetValueTime > '''+@starttimes+''' and GetValueTime < '''+@endtime+''''exec(@sql)select @inValue=inValue from #tempinsert into #tempTable(pointName,pointValue) values (@pointName,@inValue)drop table #temp   fetch next from cur into @pointID,@pointName endclose cur --關閉遊標deallocate curselect * from #tempTable            drop table #tempTable            drop table #tempTablesEND

1、遊標實際上是一種能從多條資料記錄的結果集中每次提取一條記錄的機制。遊標可以完成:

          # 允許定位到結果集中的特定行

          # 從結果集的當前位置檢索一行或多行資料

          # 支援對結果集中當前位置的進行修改

     由於遊標是將記錄集進行一條條的操作,所以這樣給伺服器增加負擔,一般在操作複雜的結果集的情況下,才使用遊標。SQL Server 2005有三種遊標:T-SQL遊標、API遊標、用戶端資料指標。

 

     2、遊標的基本操作

          遊標的基本操作有定義遊標、開啟遊標、迴圈讀取遊標、關閉遊標、刪除遊標。

     A、 定義遊標

declare cursor_name --遊標名稱cursor [local | global] --全域、局部[forward only | scroll] --遊標滾動方式[read_only | scroll_locks | optimistic] --讀取方式for select_statements --查詢語句[for update | of column_name ...] --修改欄位

     參數:

     forward only | scroll:前一個參數,遊標只能向後移動;後一個參數,遊標可以隨意移動

     read_only:唯讀遊標

     scroll_locks:遊標鎖定,遊標在讀取時,資料庫會將該記錄鎖定,以便遊標完成對記錄的操作

     optimistic:該參數不會鎖定遊標;此時,如果記錄被讀入遊標後,對遊標進行更新或刪除不會超過

 

     B、 開啟遊標

          open cursor_name;

          遊標開啟後,可以使用全域變數@@cursor_rows顯示讀取記錄條數

 

     C、 檢索遊標

          fetch cursor_name;

          檢索方式如下:

             fetch first; 讀取第一行

             fetch next; 讀取下一行

             fetch prior; 讀取上一行

             fetch last; 讀取最後一行

             fetch absolute n; 讀取某一行

                如果n為正整數,則讀取第n條記錄

                如果n為負數,則倒數提取第n條記錄

                如果n為,則不讀取任何記錄

             fetch pelative n

                如果n為正整數,則讀取上次讀取記錄之後第n條記錄

                如果n為負數,則讀取上次讀取記錄之前第n條記錄

                如果n為,則讀取上次讀取的記錄

 

     D、 關閉遊標

          close cursor_name;

 

     E、 刪除遊標

          deallocate cursor_name;

 

     3、遊標操作樣本

--建立一個遊標declare cursor_stu cursor scroll forselect id, name, age from student;--開啟遊標open cursor_stu;--儲存讀取的值declare @id int,@name nvarchar(20),@age varchar(20);--讀取第一條記錄fetch first from cursor_stu into @id, @name, @age;--迴圈讀取遊標記錄print '讀取的資料如下:';--全域變數while (@@fetch_status = 0)beginprint '編號:' + convert(char(5), @id) + ', 名稱:' + @name + ', 類型:' + @age;--繼續讀取下一條記錄fetch next from cursor_stu into @id, @name, @age;end--關閉遊標close area_cursor;--刪除遊標--deallocate area_cursor;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.