0% found this document useful (0 votes)
46 views3 pages

Calling Stored Procedures in Triggers

The document provides examples of different types of SQL triggers: 1. An AFTER INSERT trigger on a Publications table that updates a Books table to increment the NumPublications column where the ISBN matches. 2. An INSTEAD OF DELETE trigger on an Employee_Test table that checks if the salary is over 1200, raises an error if so, and otherwise deletes the record and logs it in an audit table. 3. An AFTER INSERT trigger on a Customer table that gets the max Customer_Number, sets action details variables, and updates the inserted record with the action details.

Uploaded by

Antonio Gonzaga
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Calling Stored Procedures in Triggers

The document provides examples of different types of SQL triggers: 1. An AFTER INSERT trigger on a Publications table that updates a Books table to increment the NumPublications column where the ISBN matches. 2. An INSTEAD OF DELETE trigger on an Employee_Test table that checks if the salary is over 1200, raises an error if so, and otherwise deletes the record and logs it in an audit table. 3. An AFTER INSERT trigger on a Customer table that gets the max Customer_Number, sets action details variables, and updates the inserted record with the action details.

Uploaded by

Antonio Gonzaga
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

I have had no trouble calling a stored procedure from a trigger before.

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

FROM Books, inserted WHERE Books.ISBN = inserted.ISBN END


CREATE TRIGGER trgInsteadOfDelete ON [dbo].[Employee_Test] INSTEAD OF DELETE AS declare @emp_id int; declare @emp_name varchar(100); declare @emp_sal int; select @emp_id=d.Emp_ID from deleted d; select @emp_name=d.Emp_Name from deleted d; select @emp_sal=d.Emp_Sal from deleted d; BEGIN if(@emp_sal>1200) begin RAISERROR('Cannot delete where salary > ROLLBACK; end else begin

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

You might also like