Calling Stored Procedures in Triggers
Calling Stored Procedures in Triggers
Here is an example of something that works just fine: CREATE TABLE [dbo].[A_Test]( [aValue] [int] NOT NULL, [aSum] [int] NULL) CREATE PROCEDURE [dbo].[aTestProc] AS DECLARE @Sum int SELECT @Sum=sum(aValue) from A_Test UPDATE A_Test SET aSum=@Sum CREATE TRIGGER [dbo].[UpdateSum] ON [dbo].[AA_Test] AFTER INSERT AS BEGIN EXEC aTestProc END INSERT INTO A_Test(aValue) VALUES(1) INSERT INTO A_Test(aValue) VALUES(2) INSERT INTO A_Test(aValue) VALUES(3) SELECT * FROM A_Test This gives: aValue aSum ------ ---16 26 36 As it should. INSERT INTO A_Test(aValue) VALUES(4) SELECT * FROM A_Test Would then give the records as above with 4|10 added at the bottom and the aSum column will all say 10.
CREATE TRIGGER InsertPublication ON Publications AFTER INSERT AS BEGIN UPDATE Books SET NumPublications = NumPublications + 1
1200',16,1);
delete from Employee_Test where Emp_ID=@emp_id; COMMIT; insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestam p) values(@emp_id,@emp_name,@emp_sal,'Deleted -Instead Of Delete Trigger.',getdate()); PRINT 'Record Deleted -- Instead Of Delete Trigger.' end END GO
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE TRIGGER TestInsert ON dbo.Customer AFTER INSERT AS DECLARE @Max_Num int SELECT @Max_Num=MAX(Customer_Number) from dbo.Customer DECLARE @Actions varchar(200);
DECLARE @Action_Date datetime; set @Action='Inserted Record -- After Insert Trigger.'; set @Action_Date=getdate(); BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. EXEC InsertCustomers UPDATE dbo.Customer SET Actions = @Actions, Action_Date=@Action_Date WHERE Customer_Number = @Max_Num; -- Insert statements for trigger here END