Skip to content
Advertisement

Create trigger to insert a column value into other column of same table SQL Server 2005

How to create trigger to insert a value in a column same as value of another column of same table of a newly inserted row in the table.

Suppose I have table like below

I want columnB value to be inserted into ColumnA as soon as row gets inserted into table or columnB value gets updated . But it should not be vice versa ie insert columnA value into columnB

.Below code handles INSERT only , please help me how to handle both Insertion and Updation of table ie when columnB value gets inserted or updated.

This works fine if values inserted like below

ColumnB value gets inserted into columnA

But null value gets updated in both if some other application inserts value in columnA

Now records are

The correct recordset should be like below

How to solve this problem.

Advertisement

Answer

Try this trigger (it will copy the values from ColumnB to ColumnA when are inserted values into ColumnB or when are updated the values from ColumbB):

I used this table:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement