if exists ( select *
from sys.objects
where object_id = object_id(n'[dbo].[payee_del]')
and type in ( n'p', n'pc' ) )
begin
drop procedure [payee_del]
endgo
create proc [dbo].[payee_del]
@payee_id int ,
@rp int output
as set @rp = 0
if not exists ( select payee_id
from payee
where payee_id = @payee_id )
begin
set @rp = 11
return
end
if exists ( select top 1
mab01id
from dbo.mab01_buyorder
where mab01payeeid = @payee_id )
begin
set @rp = 374
return
end
delete from payeeaccount
where payee_id = @payee_id
declare @payee_accountid int
declare cursor1 cursor for
select payeeaccount_id from dbo.payeeaccount pa where pa.payee_id=@payee_id
open cursor1
fetch next from cursor1 into @payee_accountid
while @@fetch_status = 0
begin
print @payee_accountid
delete from dbo.payee_tvmedia
where payeeaccount_id = @payee_accountid
fetch next from cursor1 into @payee_accountid
endclose cursor1
deallocate cursor1
delete payee
where payee_id = @payee_id
go
簡記游標用法
第一步 宣告游標 關鍵字 declare 游標名 關鍵字 cursor 關鍵字 for 記錄集例如 declare cur name cursor for select from tt 第二步開啟游標 open cur name 第四步提取資料 fetch next from cur name in...
SQL游標使用
使用迴圈控制 declare a int set a 0 while a 10 begin print a set a a 1 end 獲取系統時間 select getdate exec sp addlinkedserver mailoudb exec sp addlinkedserver dbl...
sql 游標 初次使用
use brow go declare browser cursor cursor for select browsertype from tbbrowser 定義游標 open browser cursor 開啟游標 declare browsertype nvarchar 10 儲存迴圈出來的值...