if exists(select *from sysobjects where id=object_id('proc_fetch_all')) --事先刪除儲存過程
drop procedure proc_fetch_all
gocreate procedure proc_fetch_all
as--當 set nocount 為 on 時,不返回計數(表示受 transact-sql 語句影響的行數)。當 set nocount 為 off 時,返回計數。
set nocount on
declare @name varchar(20)
begin
declare db_cursor cursor scroll for select sname from student --建立游標
open db_cursor --開啟游標
fetch first from db_cursor into @name
select @name as sname
while @@fetch_status=0 --游標讀取下一條資料是否成功
begin
fetch next from db_cursor into @name
select @name as sname
endclose db_cursor --關閉游標
deallocate db_cursor --刪除游標
endset nocount off
go exec proc_fetch_all --執行儲存過程
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...
SQL儲存過程分頁與游標使用
儲存過程 create procedure p splitpage sql nvarchar 4000 要執行的 sql語句 page int 1,要顯示的頁碼 pagesize int,每頁的大小 pagecount int 0 out,總頁數 recordcount int 0 out 總記錄數...