0

The problem is I have inserted a new row in a table through procedure which have a AFTER INSERT trigger on it, what I want to do is to update only this new inserted row though trigger, is there any way to do this?

steave
  • 203
  • 1
  • 6
  • 9
  • possible duplicate of [SQL Insert trigger to update INSERTED table values](http://stackoverflow.com/questions/1837475/sql-insert-trigger-to-update-inserted-table-values) – TomTom Mar 07 '14 at 06:11

1 Answers1

0

Yes.

Have you tried reading the documentation? I mean, at least once.

THe part where it tells you the trigger has a virtual table (inserted) with all the rows it processes (may be more than one actually, an insert can cover more than one row) and you can then use standard SQL on this (i.e. to find the updated rows in the real table and then update them)?

Your question is the textbook example of a simple standard tutorial trigger chapter 1.

A little google fu - "tsql trigger update inserted table".

Item 1: http://technet.microsoft.com/en-us/library/ms191300.aspx, named "Use the inserted and deleted Tables"

You could also just use search here instead of asking.

SQL Insert trigger to update INSERTED table values

Community
  • 1
  • 1
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • Great answer! In addition of my words, When you update rows(vales), the work-flow of SQLServer is firstly delete the original rows(values), put them in the virtual table named `"DELETED"`, then put the new rows(values) in `"INSERTED"`(another virtual table). Permanent modification will not be made to disk until the transaction committed. – vicd Mar 07 '14 at 06:18
  • But what if my column values are not making the row unique, is there something in SQL which will tell me that this particular row is inserted. – steave Mar 07 '14 at 06:23
  • 2
    @steave This is called "design error". All rows should have a primary key which - per definition - is unique. – TomTom Mar 07 '14 at 06:26
  • Actually the column of the row i want to update is the primary key. – steave Mar 07 '14 at 06:31
  • @steave actually it is even worse than a design error - it is a total design clusterf****. Primary keys, by definition of the relational model, are not allowed to change. Ups. – TomTom Mar 07 '14 at 08:06