Click here to Skip to main content
16,014,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to every one. could I have a answer about a problem. i have created a trigger FOR update, using it i want to list the columns that has been updated but on update stored procedure I update all columns so I want to get just the columns that contain different data after update
For examples GET COLUMNS THAT CONTAINS DIFFERENT DATA FROM INSERTED COMPARE TO DELETED (On TRIGGER)
thanks a lot
Posted
Comments
Sandeep Mewara 4-May-11 4:35am    
Ok so? Tried anything? Google?

Hi friend,

I have found a solution using After Update Trigger.
Just assume am having a table Employee with the following columns(Eid,Name,Salary)
Eid is primary key.. so It can't be changed.

My trigger will show what are all the columns are updated.
I have used 2 System virtual tables(inserted and deleted).
As I know what are all the columns are existed in my table,to create a trigger for my table, I just use column names directly.

Code:-

SQL
Alter trigger Tr_UpdateEmp
On Employee
After Update
As
Begin
declare @Eid int,@name varchar(20),@Salary money,@Eid1 int,@name1 varchar(20),@Salary1 money,@reuslt varchar(20),@reuslt1 varchar(20),@reuslt2 varchar(20)
Set @Eid=0
set @name=''
set @Salary=0
Set @Eid1=0
set @name1=''
set @Salary1=0
set @reuslt=''
set @reuslt1=''
set @reuslt2=''


select @Eid=Eid,@name=name,@Salary=Salary from Inserted
Select @Eid1=Eid,@name1=name,@Salary1=Salary from Deleted
select @reuslt1=case when @name<>@name1 Then 'Name' End
select @reuslt2=case when @Salary<>@Salary1 Then 'Salary' End

IF @reuslt1 <>'' and @reuslt2<>''
Begin
Select Name,Salary from Employee where Eid=@Eid
End
Else IF @reuslt1 ='' and @reuslt2<>''
Begin
Select Salary from Employee where Eid=@Eid
END
Else IF @reuslt1 <>'' and @reuslt2=''
Begin
Select Name from Employee where Eid=@Eid
END
END




--Please keep me in touch.

Aravinth.G
SQL Developer,
Kadamba Technologies Pvt.Ltd,
Chennai
Email:- [Deleted] // Don't mention your mail id here
 
Share this answer
 
v2
Comments
dajask 5-May-11 5:30am    
may be an other explain could help u to help me. here is the script that I'm using
WHILE @ColumnID <= @LastColumnID
BEGIN
SET @col = COL_NAME(@ObjectID, @ColumnID)
SET @sql1 = N'select [' + @col + '] FROM #tmp1'
SET @sql2 = N'select [' + @col + '] FROM #tmp2'
-- EXEC sp_executesql @sql1
-- EXEC sp_executesql @sql2
it is sure that @sql1 and @sql2 returns just one row, so i have a cell as result from @sql1 and a cell from @sql2, columns type can be bit or varchar or whatever but it's sure too that type of both column is same. I just need to compare Values in both of them
 
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