sql關於游標和游標 儲存過程結合的乙個例子

2021-06-08 09:06:16 字數 4749 閱讀 3105

--關於游標的用法

--定義 transact-sql 伺服器游標的特性,例如游標的滾動行為和用於生成游標對其進行操作的結果集的查詢。

--declare cursor 接受基於 sql-92 標準的語法和使用一組 transact-sql 擴充套件的語法。

/*@@fetch_status

返回被 fetch 語句執行的最後游標的狀態,而不是任何當前被連線開啟的游標的狀態。

返回值 描述

0 fetch 語句成功。

-1 fetch 語句失敗或此行不在結果集中。

-2 被提取的行不存在。

*/select * from dep;

--這是定義乙個游標

declare @id int, @name varchar(20);

declare dep_cursor cursor for

select depid, depname from dep;

--這是開啟游標

open dep_cursor;

fetch  next from dep_cursor into @id, @name;

while(@@fetch_status = 0)

begin

--fetch next返回緊跟當前行之後的結果行,並且當前行遞增為結果行。如果 fetch next 為對游標的第一次提取操作,

--則返回結果集中的第一行。next 為預設的游標提取選項。

fetch  next from dep_cursor into @id, @name;

print @id;

print @name;

end;

--這是關閉游標

close dep_cursor;

--這是刪除游標

deallocate dep_cursor;

--這是乙個將游標應用到儲存過程的應用

--如果我們要返回乙個游標的返回值,那麼我們的游標的這個引數必須定義為 cursor varying output

create procedure t1_cursor(@dep_cursor cursor varying output)

asdeclare @id int, @name varchar(20);

set @dep_cursor =  cursor  for select depid,depname from dep;--這句**是個關鍵

open @dep_cursor;--這是開啟游標

drop procedure t1_cursor;

godeclare @t2_cursor cursor, @id int, @name varchar(20);

exec  t1_cursor @t2_cursor output;

fetch next from @t2_cursor into @id, @name;

while(@@fetch_status = 0)

begin

print @id;

print @name;

fetch next from @t2_cursor into @id, @name;

end;

close @t2_cursor;

go/*sqlser游標乙個小例子    問題描述:有以下2張表,

現在希望得到這樣的資料表a裡的o_saloary加上表b的a_salary是當前的總工資,並更新到表a。

你可以用檢視來,表連線下就可以。

現在我們考慮下讓sql自己來處理,游標就是很好的方法。

原理:游標就是把資料按照指定要求提取出相應的資料集,然後逐條進行資料處理。

1.1游標的概念

游標(cursor)它使使用者可逐行訪問由sql server返回的結果集。使用游標(cursor)的乙個主要的原因就是把集合操作轉換成單

個記錄處理方式。用sql語言從資料庫中檢索資料後,結果放在記憶體的一塊區域中,且結果往往是乙個含有多個記錄的集合。

游標機制允許使用者在sql server內逐行地訪問這些記錄,按照使用者自己的意願來顯示和處理這些記錄。

1.2 游標的優點

從游標定義可以得到游標的如下優點,這些優點使游標在實際應用中發揮了重要作用:

1)允許程式對由查詢語句select返回的行集合中的每一行執行相同或不同的操作,而不是對整個行集合執行同乙個操作。

2)提供對基於游標位置的表中的行進行刪除和更新的能力。

3)游標實際上作為面向集合的資料庫管理系統(rdbms)和面向行的程式設計之間的橋梁,使這兩種處理方式通過游標溝通起來。

1.3 游標的使用

講了這個多游標的優點,現在我們就親自來揭開游標的神秘的面紗。

使用游標的順序: 聲名游標、開啟游標、讀取資料、關閉游標、刪除游標。

宣告游標

最簡單游標宣告:declare 《游標名》cursor for其中select語句可以是簡單查詢,也可以是複雜的接連查詢和巢狀查詢

開啟游標

非常簡單,我們就開啟剛才我們宣告的游標mycursor

open mycursor      

讀取資料

fetch [ next | prior | first | last] from [ into @變數名 [,…] ]

引數說明:

next   取下一行的資料,並把下一行作為當前行(遞增)。由於開啟游標後,行指標是指向該游標第1行之前,所以第一次執行fetch next操作將取得游標集中的第1行資料。next為預設的游標提取選項。

into @變數名[,…] 把提取操作的列資料放到區域性變數中。

列表中的各個變數從左到右與游標結果集中的相應列相關聯。

各變數的資料型別必須與相應的結果列的資料型別匹配或是結果列資料型別所支援的隱性轉換。變數的數目必須與游標選擇列表中的列的數目一致。       

關閉游標

close mycursor             

刪除游標

deallocate mycursor 

給出具體的例子:

declare @id nvarchar(20) --定義變數來儲存id號

declare @a float                  --定義變數來儲存值

declare mycursor cursor for select * from tb_c   --為所獲得的資料集指定游標

open mycursor                   --開啟游標

fetch next from mycursor into @id,@a   --開始抓第一條資料

while(@@fetch_status=0)     --如果資料集裡一直有資料

begin

select tb_b.name,(tb_b.gz + @a) from tb_b where tb_b.id = @id   --開始做想做的事(什麼更新呀,刪除呀)

fetch next from mycursor into @id,@a     --跳到下一條資料

endclose mycursor        --關閉游標

deallocate mycursor --刪除游標

sql2005 判斷是否為 數字

sql 判斷是否為數字

sql語句中的isnumeric函式是用來判斷是否為數字

舉例:select isnumeric(0)

返回值為1

select isnumeric('易心部落格')

返回值為0

sql2005 update

update mis.a22 set mis.a22.名稱=dm_pdtq.dm from dm_pdtq where dm_pdtq.tqmc=mis.a22.名稱

說明,更新a22中的名稱欄位為dm_pdtq.dm

說明sqlserver2005分頁處理

select *   from (select row_number() over(order by bh) as rownum, *

from table1) as table1

where rownum> 0 and rownum<=50 (查詢第1頁到50頁)

說明:複製表(只複製結構,源表名:a 新錶名:b)

a:select * into b from a where 1<>1

b:select top 0 * into b from a

說明:刪除重覆記錄

delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

說明:幾個高階查詢運算詞

a: union 運算子

union 運算子通過組合其他兩個結果表(例如 table1 和 table2)並消去表中任何重複行而派生出乙個結果表。當 all 隨 union 一起使用時(即 union all),不消除重複行。兩種情況下,派生表的每一行不是來自 table1 就是來自 table2。

b: except 運算子

except 運算子通過包括所有在 table1 中但不在 table2 中的行並消除所有重複行而派生出乙個結果表。當 all 隨 except 一起使用時 (except all),不消除重複行。

c: intersect 運算子

intersect 運算子通過只包括 table1 和 table2 中都有的行並消除所有重複行而派生出乙個結果表。當 all 隨 intersect 一起使用時 (intersect all),不消除重複行。

注:使用運算詞的幾個查詢結果行必須是一致的

*/

SQL 儲存過程,游標

if exists select from sysobjects where id object id proc fetch all 事先刪除儲存過程 drop procedure proc fetch all gocreate procedure proc fetch all as 當 set n...

SQL儲存過程游標呼叫儲存過程示例

if object id proc temp p is not null drop procedure proc temp go create procedure proc temp with recompile as select from student goexecute proc temp ...

sql儲存過程 游標 迴圈表

游標例項 利用游標迴圈表 根據userid賦值 alter procedure cursor eg1 asbegin declare a int,error int declare temp varchar 50 臨時變數,用來儲存游標值 set a 1 set error 0 begin tran...