Use Update() in SQL Server

Update() is used in trigger to check or ensure whether the data in a table or a view is updated or not. Let us take a table named test_ table. This table contains the following data:
 
 
 
Now, we create a trigger in which we will use Update() to check out the modification in data, if any, and generate a message. Code for the trigger is,  
  1. create trigger My_trig  
  2. on  
  3. test_  
  4. after update  
  5. as  
  6. begin  
  7. if(update(name))  
  8. begin  
  9. declare @Previous varchar(max);  
  10. declare @Updated varchar(max);  
  11. set @Previous=(select name from deleted);  
  12. set @Updated=(select name from inserted);  
  13. print 'Data Has Been Updated'+'Previous values Was '+ @Previous+' And New Value is '+@Updated;  
  14. end  
  15. else  
  16. begin  
  17. print 'Table is not updated';  
  18. end  
  19. end  
In the above code, we create a trigger that invokes after update operation. If the name column is modified then it shows the previous data along with the new inserted data, otherwise, it shows the message that "Table is not updated". Let's now, try to update some data.

Query 
  1. update test_ set test_.name='Pankaj'  
  2. where test_.id=1;  
After executon of above query the following output will generate.
 
 
Ebook Download
View all
SQL Jobs
Read by 0 people
Download Now!
Learn
View all