Hi
How can we define StoredProcedure with Trigger in Sql Server ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Nov 14, 2011, 7:18 PM EST
As per my understading on your need, We can create a trigger inside the SP. We need to use Dynamic SQL as like the below one.
USE tempdb; SELECT ProductNumber, ListPrice, Color INTO Product FROM AdventureWorks2008.Production.Product GO CREATE PROC sprocCreateDynamicTrigger AS BEGIN DECLARE @SQL nvarchar(max)= 'CREATE TRIGGER trgProduct on Product for INSERT AS DECLARE @InsProd varchar(32) SELECT @insProd = ''TRIGGER: '' + ProductNumber FROM inserted PRINT @InsProd' EXEC sp_executeSQL @SQL END GO -- Execute stored procedure to create trigger EXEC sprocCreateDynamicTrigger GO INSERT Product VALUES ('Alpha Romeo 2011', 40000, 'Blue') GO -- TRIGGER: Alpha Romeo 2011 DROP PROC sprocCreateDynamicTrigger DROP TABLE tempdb.dbo.Product SourceDhaval PatelRank #1,023 · 1.9K reputationPosted Nov 14, 2011, 7:23 PM EST
Please Refer below link.
Stored Procudure with Trigger
This post help you than mark as "Accepted Answer"
Datta KharadRank #2,135 · 668 reputationPosted Nov 14, 2011, 7:31 PM EST
CREATE OR REPLACE my_procedure
(p_variable1 IN my_table.column1%type,
p_variable2 IN my_table.column2%type,
p_out OUT number)
AS
BEGIN
p_out := p_variable1 + p_variable2;
END;
/
CREATE OR REPLACE TRIGGER my_test_trigger
BEFORE INSERT ON my_table
DECLARE
v_out number;
BEGIN
my_procedure(:new.column1, :new.column2, v_out);
:new.column3 := v_out;
END;
/
AartiRank #2,383 · 594 reputationPosted Nov 15, 2011, 2:23 AM EST
Refer this Syntax:
May be it will help you
create Trigger Trigger_name
After INSERT ON my_table
DECLARE
v_out number;
BEGIN
my_updateproc(:new.column1, :new.column2, v_out);
:new.column3 := v_out;
END;
Thanks.