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,
- create trigger My_trig
- on
- test_
- after update
- as
- begin
- if(update(name))
- begin
- declare @Previous varchar(max);
- declare @Updated varchar(max);
- set @Previous=(select name from deleted);
- set @Updated=(select name from inserted);
- print 'Data Has Been Updated'+'Previous values Was '+ @Previous+' And New Value is '+@Updated;
- end
- else
- begin
- print 'Table is not updated';
- end
- 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
- update test_ set test_.name='Pankaj'
- where test_.id=1;
After executon of above query the following output will generate.