Wednesday, 8 November 2023

Triggers...(imp)

 Create Triggers on Table

> the main use to if i deleted or inserted new record on table then customer ask old data which one deleted

to prevent from this type of issues i am creating the histroy schema with same table then.

1.creating the table on history schema same as main table

2.create trigger on main table.


-- ==============================================

-- Create dml trigger template Azure SQL Database 

-- ==============================================

-- Drop the dml trigger if it already exists



CREATE TRIGGER [dbo].[tr_Returntoemp_Delete] ON [dbo].[tbl_ReturnToEmployeeDetails]

   INSTEAD OF DELETE

AS 

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

    -- Insert statements for trigger here

INSERT INTO [History].[tbl_ReturnToEmployeeDetails]

SELECT GETDATE(), RT.ID,RT.ManagerID,RT.EmployeeID,RT.ActionerID,RT.AppraisalTypeID,

RT.Description,RT.ModifiedBy,RT.ModifiedOn,RT.IsActive

FROM [tbl_ReturnToEmployeeDetails] as RT

INNER JOIN DELETED ON RT.ID=Deleted.ID


DELETE FROM tbl_ReturnToEmployeeDetails WHERE ID IN(SELECT ID FROM Deleted)  



END

GO

ALTER TABLE [dbo].[tbl_ReturnToEmployeeDetails] ENABLE TRIGGER tr_Returntoemp_Delete

GO

-- ==============================================

-- ==============================================

No comments:

Post a Comment

7 Common mistakes in Dot Net — You can avoid

  There are many common mistakes made during .NET (ASP.NET, .NET Core) development, which affect performance, security, and code… Code Crack...