Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to reference the current row?

Status
Not open for further replies.

Dan777

Programmer
Jul 22, 2000
34
0
0
US
I have a table that has 2 columns in it.&nbsp;&nbsp;One of the columns is updated by the UI.&nbsp;&nbsp;The other column is a calulated update based on the other column.<br><br>What I want to do is put a trigger on the table so that when the row is updated or inserted the calculated column is updated automatically.<br><br>What I tried doing is:<br><br>CREATE TRIGGER MyTrigger<br>ON MyTable<br>FOR UPDATE<br>AS<br> UPDATE MyTable<br> SET MyTotalField = MyQtyField * MyRateField<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM MyTable<br><br>How can I make this trigger fire for just the current row?<br>
 
You have to refere to then virtual &quot;inserted&quot; table.<br>Like this:<br><br>You have to refer to the virtual inserted table.<br>Like this:<br><br>CREATE TRIGGER MyTrigger<br>ON MyTable<br>FOR UPDATE<br>AS<br>UPDATE MyTable<br>SET MyTotalField = MyQtyField * MyRateField<br>FROM MyTable, inserted<br>WHERE MyTable.TheIdColumn = inserted.TheIdColumn<br><br><br>With A UPDATE trigger you have the new value in the virtual<br>&quot;inserted&quot; table and the old value in the virtual <br>&quot;deleted&quot; table.<br>If its an INSERT that triggers the event, only the &quot;inserted&quot; table exists, and if its a DELETE that triggers an event only the &quot;deleted&quot; table exsists.<br><br>//Martin<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top