當我執行該程式時,我填寫了備用零件(機械車間)的 ID、零件數量和案例編號。
我試圖在某處添加一個輸出,如果我選擇的零件數量 >= 庫存零件數量,則觸發,并輸出一條訊息“重新訂購零件”
該腳本有效,但它并沒有完成我需要它做的所有事情。
resid = reserve part id
antal = number of parts
sagsnummer = case number
Reservedele = spare parts
CREATE PROCEDURE sp_opret_forbrug
@resid int,
@antal int,
@sagsnummer int
AS
BEGIN
INSERT INTO dbo.forbrug (resid, antal, sagsnummer)
VALUES (@resid, @antal, @sagsnummer)
UPDATE [dbo].[Reservedele]
SET antal = Reservedele.antal - @antal
WHERE reservedele.resid = @resid;
END
--exec sp_opret_forbrug
--@resid = '49',
--@antal = '2',
--@sagsnummer ='11'
--drop procedure sp_opret_forbrug

uj5u.com熱心網友回復:
你可以使用拋出錯誤 THROW
CREATE OR ALTER PROCEDURE sp_opret_forbrug
@resid int,
@antal int,
@sagsnummer int
AS
SET XACT_ABORT ON;
BEGIN TRAN;
IF EXISTS (SELECT 1
FROM [dbo].[Reservedele]
WHERE reservedele.resid = @resid
AND Reservedele.antal < @antal)
THROW 50000, 'Not enough stock, reorder', 0;
INSERT INTO dbo.forbrug (resid, antal, sagsnummer)
VALUES (@resid, @antal, @sagsnummer)
UPDATE [dbo].[Reservedele]
SET antal = Reservedele.antal - @antal
WHERE reservedele.resid = @resid;
COMMIT;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379317.html
標籤:sql sql-server 查询语句 存储过程
上一篇:在觸發器中獲取“無效的列名”
