Wednesday, 12 March 2014

update multiple rows Trigger


/****** Object:  Trigger [dbo].[AfterUpdate]    Script Date: 03/12/2014 15:09:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [dbo].[AfterUpdate]
   ON  [dbo].[Employee]
   AFTER Update
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

declare @Id int ,@name Varchar(50),@salary int,@Address Varchar(50)

 DECLARE cur_emp CURSOR
STATIC FOR
SELECT i.EmpID,i.EmpName,i.Salary from Inserted i;
OPEN cur_emp
IF @@CURSOR_ROWS > 0
 BEGIN
 FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
 WHILE @@Fetch_status = 0
 BEGIN
INSERT INTO Employee_Audit(EmpID,EmpName,Salary,Address) VALUES(@Id,@name,@salary,'test')
 FETCH NEXT FROM cur_emp INTO @Id,@name,@salary
 END
END
CLOSE cur_emp
DEALLOCATE cur_emp
SET NOCOUNT OFF

    -- Insert statements for trigger here

END

No comments:

Post a Comment