sql service 儲存過程,游標的使用

2021-07-11 04:50:29 字數 1272 閱讀 7806

1、建表

drop table dbo.users

gocreate table dbo.users (

id int not null ,

name varchar(32) null )go

alter table dbo.users add primary key (id)

go2、新增資料

--刪除儲存過程

if (exists (select * from sys.objects where name = 'insert_users'))

drop proc insert_users

go--建立儲存過程

create proc insert_users as

begin

declare @i int;

set @i = 0;

while @i <100

begin

set @i = @i + 1;

insert into dbo.users values(@i, cast(@i as varchar)+'name');

endend

--執行儲存過程

exec insert_users

3游標的使用

--刪除儲存過程

if (exists (select * from sys.objects where name = 'my_proc_cursor'))

drop proc my_proc_cursor

go--建立儲存過程

create proc my_proc_cursor as

begin

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

--定義游標

declare my_cursor cursor for select top 10 * from dbo.users;

--開啟游標

open my_cursor;

--首次填充資料

fetch next from my_cursor into @id, @name;

--假如檢索到了資料,才處理

while @@fetch_status = 0

begin

select @id, @name;

--再次填充資料,(相當於i=i+1的操作)

fetch next from my_cursor into @id, @name;

endend

--執行儲存過程

exec my_proc_cursor

改良版的SQL Service 通用儲存過程分頁

上次寫了通用儲存過程。感覺還是有很大的bug。就是條件不能引數畫化。這個bug可以說是致命的。但是我一直想在用什麼方法能解決這個東西。其實我只是想寫少量的 來做更多的事情。我想能不能傳集合給儲存過程但是好像這個是行不通沒辦法只能寫死。上 吧 if select count from sysobjec...

Sql Service 的job作業新建過程

第一步 開啟sql service 找到 sql server agent 下的 jobs 如圖 注 如果沒有找到,請檢視你安裝sql service 的版本 通過 select version檢視 第二步 單機jobs 右鍵new job 重要的標示 一定要打對勾 不然job不跑 name 就是j...

儲存過程系列之儲存過程sql查詢儲存過程的使用

1.查詢某個表被哪些儲存過程 以下簡稱 sp 使用到 select distinct object name id from syscomments where id in select object id from sys.objects where type p and text like ta...