Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to update multiple rows using trigger in sql server. please give an example at your earliest.
Posted
Comments
virusstorm 28-Apr-15 15:31pm    
Can you describe a little bit about what you are trying to accomplish?
RTK The Limited Edition 28-Apr-15 16:12pm    
create table employee(empid [varchar](10),empname [varchar](50),salary [decimal](18, 2))

create table employeeHistory(EffectDate [datetime],empid [varchar](10),empname [varchar](50),salary [decimal](18, 2))

create trigger UpdateEmployee on employee
after update AS
begin
declare @empid as [varchar](10)
declare @empname as [varchar](50)
declare @salary as [decimal](18, 2)

select @empid =d.empid from deleted d
select @empname =d.empname from deleted d
select @salary =d.salary from deleted d

Insert into employeeHistory([EffectDate],[empid],[empname],[salary]) values
(getdate(),@empid, @empname,@salary)
end

Now I explain it, I have 2 table employee and employeeHistory as well as a trigger on update of employee table. The employeeHistory table contains the updated record (i.e. when we update employee table then updated data will store in employee table and the deleted data will store in employeeHistory table. If I update single row then it's returns currect output. when I update multiple row at a time then it's fired only one that is 1st row. Actually it's update employee table but trigger is not work properly. that is trigger fired only for 1st row.

update employee set salary =11000.00 where salary =10000.00

Example: emp001 xxxx 10000.00
emp002 yyyy 12000.00
emp003 zzzz 10000.00
emp004 aaaa 15000.00
emp005 bbbb 12000.00
emp006 cccc 12000.00
emp007 pppp 10000.00



Now suggest me what is the how to solve it.
RTK The Limited Edition 29-Apr-15 13:42pm    
Thank you for the solution. Another problem is arise that is :

if I Add new column at employee table which contains default value like
create table employee(empid [varchar](10),empname [varchar](50),salary [decimal](18, 2),status [varchar] (100)(default ''))

create table employeeHistory(EffectDate [datetime],empid [varchar](10),empname [varchar](50),salary [decimal](18, 2),status [varchar] (100)(default ''))

create trigger UpdateEmployee on employee
after update AS
begin
declare @empid as [varchar](10)
declare @empname as [varchar](50)
declare @salary as [decimal](18, 2)
declare @status as [varchar](100)

select @empid =d.empid from deleted d
select @empname =d.empname from deleted d
select @salary =d.salary from deleted d


if update(status)
select @status=i.statusfrom deleted i
else
begin
select @status=i.status from deleted i
update [employee]
set status='' where empid=@empid
end

Insert into employeeHistory([EffectDate],[empid],[empname],[salary],[status])
SELECT getdate(), empid, empname, salary,status
FROM deleted
end



Example: emp001 xxxx 10000.00 paid
emp002 yyyy 12000.00 paid
emp003 zzzz 10000.00 paid
emp004 aaaa 15000.00 paid
emp005 bbbb 12000.00 paid
emp006 cccc 12000.00 paid
emp007 pppp 10000.00 paid

update employee set salary =11000.00 where salary =10000.00
after update first updated row set its default value but rest rows don't set it in employee table. Please give me a solution at your earliest.

1 solution

You are only grabbing the first record from the deleted table. You should be doing something like this:
SQL
create trigger UpdateEmployee on employee
after update AS
begin
	Insert into employeeHistory([EffectDate],[empid],[empname],[salary])
	SELECT getdate(), empid, empname, salary
	FROM deleted
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900