我正在尝试将触发器保存在存储过程中,因为删除表时将删除触发器。
我遇到错误,无法执行。
CREATE PROCEDURE EC5Trigger2
AS
CREATE TRIGGER trVendorProductsPST
ON VendorProducts
FOR INSERT
AS
BEGIN
SET NOCOUNT ON
DECLARE @SKU VARCHAR(64)
SELECT @SKU = I.SKU FROM inserted AS I
INSERT INTO ProductStockTransactions (SKU, stockingCode)
VALUES (@SKU, 'A')
END
RETURN 0
答案 0 :(得分:1)
这是使用动态SQL在存储过程内创建触发器的方法,这是唯一的方法。
CREATE PROCEDURE EC5Trigger2
AS
BEGIN
SET NOCOUNT ON;
DECLARE @TriggerCode NVARCHAR(max);
SET @TriggerCode = 'CREATE TRIGGER trVendorProductsPST
ON VendorProducts
FOR INSERT
AS
BEGIN
SET NOCOUNT ON
DECLARE @SKU VARCHAR(64)
SELECT @SKU = I.SKU from INSERTED as I
INSERT INTO ProductStockTransactions (SKU, stockingCode)
VALUES (@SKU, ''A'')
END';
EXEC(@TriggerCode);
RETURN 0;
END