Monday, October 12, 2015

SQL - Inserted & Deleted Tables in Trigger

'Inserted' & 'Deleted' tables are temporary table created by Sql
These tables have same structure as Main table
Their scope is in triggers
These can be used for validation or other calculation or as required in triggers


























Example :
ALTER TRIGGER [dbo].[Reminder]
ON [dbo].[Filings]
AFTER UPDATE
AS

declare @FilingId as int
declare @FilingSId as int
declare @IsOnTime as bit

SELECT @FilingId = i.FilingId FROM INSERTED i
SELECT @FilingSId = i.filingstatus FROM INSERTED i
SELECT @IsOnTime = case when getdate() > A.DueDate then 0 elseend from vw_Companies A  inner join  INSERTED i on A.crno = i.crno

IF ( UPDATE (filingstatus) and @FilingSId =12)
BEGIN
update   Filings set isontime = @IsOnTime where filingid = (select i.FilingId FROM INSERTED i)
END;

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...