create procedure ストアドプロシージャ名
--引数宣言
as
declare
--変数宣言
begin
--処理内容
end
create procedure ストアドプロシージャ名
--引数宣言
as
declare
--変数宣言
begin try
--トランザクションの開始
begin transaction
-- 例外が発生する可能性がある処理
--トランザクションをコミット
commit transaction
end try
-- 例外処理
begin catch
-- トランザクションをロールバック
rollback transaction
return error_number()
end catch
return 0
create procedure sp_get_suryo
@id numeric,
@c numeric output
as
begin
select @c=sum(数量) from 売上データ where 商品ID=@id
end
declare @suryo numeric
exec sp_get_suryo 1,@suryo output
select @sury
declare @suryo numeric
exec sp_get_suryo @id=1,@c=@suryo output
select @suryo
create procedure sp_retest
@value numeric
as
begin
return @value*2;
end
declare @ret numeric
exec @ret=sp_retest @value=1
select @ret
create procedure sp_vartest
as
declare
@i numeric,
@j varchar(20)
begin
set @i=1
select @j=顧客名 from 顧客マスタ where 顧客ID=1
end
exec sp_vartest
declare @n numeric
set @n=1
if (@n=1)
print 'n = 1'
else if(@n=2)
print 'n = 2'
else
print 'n != 1 or 2'
declare @i numeric
set @i=0
while (@i <= 10)
begin
set @i = @i + 1
if(@i=2) continue
if(@i=5) break
select @i
end
declare @i numeric
set @i=0
label:
if(@i < 10)
begin
set @i=@i+1
select @i
goto label
end
create procedure sp_get_sum_oroshi
@total numeric output
as
declare cur CURSOR local for select isnull(卸単価,0) from 商品マスタ
declare @price numeric
begin
set @total = 0
open cur
fetch next from cur into @price
while @@fetch_status=0
begin
set @total=@total+@price;
fetch next from cur into @price
end
close cur
deallocate cur
end
declare @total numeric
exec sp_get_sum_oroshi @total output
select @total