Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: INSERTED table

  1. #1
    Join Date
    Nov 2003
    Posts
    31

    Question Unanswered: INSERTED table

    I have a qn regarding the INSERTED table.

    Whenever a row is inserted into the table i understand that the INSERTED table also gets that particular row. But how long does that particular row stay thr? Till another new row is inserted into the table? which means that rows get overwritten whenever a row is inserted?

    Hope everyone understands what i am trying to say. Would be kind of you to reply.thanks!

    Gayathri

  2. #2
    Join Date
    Feb 2002
    Posts
    2,232
    Please provide an example of the sql.

  3. #3
    Join Date
    Nov 2003
    Posts
    31
    SELECT JobNumber
    from inserted
    where ServiceType = 'On-site' and ServiceStatus = 'NEW' and DateModified=(SELECT MAX(DateModified) from inserted)

    when i execute this statement alone i will only get one job number but when i put this into a trigger and channel the output into a cursor to copy it into a variable it selects some other job number as well...This is the whole code

    DECLARE job_number_cursor CURSOR FOR
    SELECT JobNumber
    from inserted
    where ServiceType = 'On-site' and ServiceStatus = 'NEW' and DateModified=(SELECT MAX(DateModified) from inserted)
    OPEN job_number_cursor
    FETCH NEXT FROM job_number_cursor INTO
    @job_number

    CLOSE job_number_cursor
    DEALLOCATE job_number_cursor

    I was hoping to get just one output since i thought the INSERTED table only contains the last inserted row.

  4. #4
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    I understand what you are asking...

    Basically you are saying,...

    When a table has a trigger on it the trigger has access to a table called inserted (assuming it is an insert trigger). How long does the inserted table with the insert record exist....

    In all honesty, I'm not sure, but I would say it would exist until the insert and the associated trigger (if there is one) has been completed....

    I'll look up some resources and see what I can find.

  5. #5
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    Um,... question,... when you use your cursor are you doing an update or insert into the table with the trigger on it??

  6. #6
    Join Date
    Nov 2003
    Posts
    31
    I am inserting

  7. #7
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    okie,... so lets think about that for a sec...

    you are in the middle of an insert, your trigger fires which opens a cursor which does an insert (loop to start and insert a new record into the inserted table)

    your cursor is still open when you do your second insert and it references the same inserted table.... which now has the new record in it...

    does that make sense????

  8. #8
    Join Date
    Feb 2002
    Posts
    2,232
    Check out your bol:

    The inserted table stores copies of the affected rows during INSERT and UPDATE statements. During an insert or update transaction, new rows are added simultaneously to both the inserted table and the trigger table. The rows in the inserted table are copies of the new rows in the trigger table.

  9. #9
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    and I assume they get cleaned out once the insert is complete.... including trigger execution....

  10. #10
    Join Date
    Nov 2003
    Posts
    31
    rnealejr ,I understand that INSERTED table stores copies of the rows inserted into the actual table....

    So does this mean that everytime an insert or update statement is executed a new inserted table is formed?

  11. #11
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    I think the table remains but the rows are removed after the action is completed.

    The reason I think this is because according to the BOL you can reference the deleted table when doing an insert and the inserted table when doing a delete but there are no rows contained in the tables...

    "When you set trigger conditions, use the inserted and deleted tables appropriately for the action that fired the trigger. Although referencing the deleted table while testing an INSERT, or the inserted table while testing a DELETE does not cause any errors, these trigger test tables do not contain any rows in these cases."

  12. #12
    Join Date
    Feb 2002
    Posts
    2,232
    You can have more than 1 record in the inserted table and the table is only accessible to the trigger - so the table exists as long as the trigger runs for a particular sql statement.

  13. #13
    Join Date
    Feb 2002
    Posts
    2,232
    So does this mean that everytime an insert or update statement is executed a new inserted table is formed?
    These tables are created/stored in memory. From what I remember, I believe the scope of these virtual tables are for the life of the trigger. It would not make sense that ss would keep a table in memory any longer than needed.

  14. #14
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    It's highly possible that they continue to exist after their usefulness has gone, after all we are talking a microsoft product and they have done stranger things in the past.

    Also the amount of memory you are talking about is minimal so the effect of keeping the table alive in memory is unlikely to cause any real problems.

    In fact, it is likely that the over head involved in creating the tables each time if more detrimental then kepeing them in memory especially when you consider that you are likely to do multiple updates/inserts/deletes on any given table at a time rather then constant swap around tables ...

  15. #15
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    The inserted and deleted tables exist only within the scope of the trigger execution. Updates use both the inserted and deleted tables because they effectively insert new modified copies of the records and then delete the old ones.

    gayamantra, the inserted table does not exist as a distinct and persistent object. Keep in mind that it has the same record format as whatever datatable was the subject of the operation.

    New virtual tables of inserted and deleted records must be created (in memory only) for each operation, otherwise multiple users accessing the datatable would end up with their inserted/deleted records intermingling.

    I would guess that there is little additional overhead in creating these virtual tables on the fly, because they may be incidental to the database server's operations anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •