Avatar billede fredand Forsker
29. maj 2001 - 09:40 Der er 7 kommentarer og
2 løsninger

I need a trigger thats return the @@identity on Insert, Update, and Delete

Hola!

I have two tabels, table1 wich gets updated, tabel2 which I want to note which rows in tabel1 who gets changed.

So I need a trigger thats get fired when something happens in tabel1, and I need to return the primarykey from tabel1 on that row who gets change and store that value in tabel2.

So what I have tried is somthing like this:

Create trigger dbo.tr_change_on_tabel1
On table1
For
{
Insert | Delete | Update
}
With encryption
as
Declare id numeric
Select @@identity Into id

insert into tabel2 (tabel, id)
        values(\'tabel1\', id)

This last one would be great if it could be replaced by a stored_procedure as well.

It would be great if you could help me out with this.

Best regards

Fredrik
Avatar billede slash Nybegynder
29. maj 2001 - 09:46 #1
Create trigger dbo.tr_change_on_tabel1
On table1
For
{
Insert | Delete | Update
}
With encryption
as
Declare @id numeric
Select @@identity as @id from inserted i

insert into tabel2 (tabel, id)
        values(\'tabel1\', @id)
Avatar billede slash Nybegynder
29. maj 2001 - 09:53 #2
maybe:
Create trigger dbo.tr_change_on_tabel1
On table1
For
{
Insert | Delete | Update
}
With encryption
as
Declare @id numeric
Select @id = @@identity from inserted

insert into tabel2 (tabel, id)
        values(\'tabel1\', @id)
Avatar billede fredand Forsker
29. maj 2001 - 09:59 #3
Hola!

Do you think that this will work for delete and update as well or have I need to change this row:
Select @id = @@identity from inserted

Suppose that \"inserted\" aims to insert?

Regards Fredrik
Avatar billede slash Nybegynder
29. maj 2001 - 10:03 #4
yes, inserted is just the temp. table that the trigger creates when an insert,update, delete occurs (the changed data is placed in the temp. table!) - as far as I know :O)
Avatar billede slash Nybegynder
29. maj 2001 - 10:36 #5
does it work?
Avatar billede fredand Forsker
29. maj 2001 - 10:51 #6
Hola!

I will try it soon, just waiting for a colleague to finish a thing first. Hang on!

Regards Fredrik

Ps
Isn\'t the temp table called \"deleted\" when a delete occur?

Ps Use Svar instead of kommentar so I can give the points to you!
Avatar billede slash Nybegynder
29. maj 2001 - 10:55 #7
yes, when a delete occurs the temp. table is called deleted, which stores rows affected by a delete and update statement.
Avatar billede nolle_k Nybegynder
31. maj 2001 - 10:37 #8
deleted : Contains the deleted rows which mean the rows that are actually deleted and the rows that are updated! The rows before they are updated are in the deleted table.

Inserted : Contains the inserted rows which mean the rows that are actually inserted and the rows that are updated! The rows after they are updated are in the inserted table. So make a JOIN between the deleted and the inserted table to figure out which rows are updated.

Nolle_k
Avatar billede fredand Forsker
31. maj 2001 - 10:40 #9
Thanks for your help!

It\'s work fine now!!

Best regards

Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester