Home [SQL] Stored Procedure 預存程序的建立
Post
Cancel

[SQL] Stored Procedure 預存程序的建立

語法

1
2
3
4
5
6
7
8
create procedure 預存程序名稱
    --宣告參數 (輸入參數、輸出參數)
as
declare
    --宣告變數
begin
    --處理內容
end

範例

1
2
3
4
5
6
7
8
9
10
--製作統計所指定商品ID的販賣數量的預存程序
create procedure uspGetSum
    @id int, --要傳入的參數
    @sum int output --輸出參數(要加上output)
as
begin
    select  @sum = sum(數量)
    from 販賣資料
    where 商品ID = @id
end

執行方法

1
2
3
4
5
6
7
--執行
declare @@result int --宣告變數
exec uspGetSum 1, @result output --執行sp (輸出參數要加上output)
select @result --顯示結果

--執行結果
--6

刪除預存程序要用drop (Drop Procedure) drop procedure procedure_name

[SQL] Stored Procedure 預存程序的建立與刪除 [SQL] Stored Procedure 預存程序的執行方法 MSDN - 建立預存程序

This post is licensed under CC BY 4.0 by the author.