Showing posts with label updated. Show all posts
Showing posts with label updated. Show all posts

Wednesday, March 7, 2012

Retrieve record just updated

I want to add an update trigger to keep a LastDateModified column up to date. I also want to make the trigger as generic as possible so I can cut&paste into others, the column has the same name throughtout the system. How can I retrieve the record I just updated so I can change the date? Also, if my update trigger updates a record, will this cause a loop?

In the context of a TRIGGER, there are two virtual tables, known as inserted and deleted.

To update the [LastDateModified] column, your TRIGGER action would be something like this:

Code Snippet

UPDATE MyTable

SET LastDateModified = getdate()

FROM inserted i

JOIN MyTable m
ON m.PKColumn = i.PKColumn

And no, the TRIGGER does not cause a loop.

|||The trigger "should not" cause a loop, unless recursive triggers are on in the database.|||This works perfectly except for one thing, the "architect" created several table without keys. Now, I get to go back and fix the mistakes of someone that thought they were "Super SQL Designer".

Saturday, February 25, 2012

Retrieve last inserted or updated record

Hi

I have an application which get any change from database using sql dependency. When a record is inserted or updated it will fire an event and my application get that event and perform required operation.

On the event handler I am usin select ID,Name from my [table];

this will return all record from database.

I just want to get the record which is inserted or updated.

Can u help me in that.

Take care

Bye

You need no event to get the same. It can be easily done like:-

Dim sql As String = "INSERT INTO myTable (col2,col3) VALUES (bb,cc); SELECT * FROM jobs WHERE col1 = @.@.IDENTITY"

@.@.Identity carries the identity value of the last inserted record. Above statement can be executed with ExecuteReader method.

Hope this helps.

|||

Similarly code can be written for update.

|||

In the query you can use

SELECT@.ID=IDENT_CURRENT('TableName') for inserting

|||

Plz click "Mark as Answer" on the post that helped you.