Write record to two tables

Write record to two tables

Post by Prayer S. Solank » Sun, 12 Jan 2003 02:26:20



Howdy!  Need help on writing a trigger.  I need to write a
trigger so that when a record is updated/inserted in one
table, that the contents of that record are also inserted
in another table.  Any ideas?

 
 
 

Write record to two tables

Post by Anith Se » Sun, 12 Jan 2003 02:37:45


There are lot of details & examples in SQL Server Books Online.
Normally you can write something along the lines of:

CREATE TRIGGER trg ON tbl
FOR INSERT, UPDATE
AS
INSERT anothertbl (col1, col2, ..coln)
SELECT *
  FROM inserted
 INNER JOIN tbl
    ON inserted.key = tbl.key
GO

--
- Anith
(Please respond only to newsgroups)

 
 
 

Write record to two tables

Post by Tony Rogerso » Sun, 12 Jan 2003 02:35:25


Inside the trigger write...

insert youraudittable (.... )
    select  ....
    from inserted

The inserted and deleted tables contain the rows just
updated/inserted/deleted.

--
Tony Rogerson
SQL Server MVP
http://www.sqlserverfaq.com



Quote:> Howdy!  Need help on writing a trigger.  I need to write a
> trigger so that when a record is updated/inserted in one
> table, that the contents of that record are also inserted
> in another table.  Any ideas?

 
 
 

Write record to two tables

Post by Prayer S. Solank » Sun, 12 Jan 2003 04:19:47


This inserts ALL the rows from INSERTED into
YOURAUDITTABLE whenever there is an insert or update on
INSERTED.  I just want one record inserted into
YOURAUDITTABLE when a record is inserted or updated in
INSERTED.  Is there something else I can try?

>-----Original Message-----
>Inside the trigger write...

>insert youraudittable (.... )
>    select  ....
>    from inserted

>The inserted and deleted tables contain the rows just
>updated/inserted/deleted.

>--
>Tony Rogerson
>SQL Server MVP
>http://www.sqlserverfaq.com


message

>> Howdy!  Need help on writing a trigger.  I need to
write a
>> trigger so that when a record is updated/inserted in one
>> table, that the contents of that record are also
inserted
>> in another table.  Any ideas?

>.

 
 
 

Write record to two tables

Post by Aaron Bertrand [MVP » Sun, 12 Jan 2003 04:45:02


As Anith's example points out, you need to join the inserted table to the
main table somehow, e.g. if you have a key on the main table.

--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/

Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.



> This inserts ALL the rows from INSERTED into
> YOURAUDITTABLE whenever there is an insert or update on
> INSERTED.  I just want one record inserted into
> YOURAUDITTABLE when a record is inserted or updated in
> INSERTED.  Is there something else I can try?

> >-----Original Message-----
> >Inside the trigger write...

> >insert youraudittable (.... )
> >    select  ....
> >    from inserted

> >The inserted and deleted tables contain the rows just
> >updated/inserted/deleted.

> >--
> >Tony Rogerson
> >SQL Server MVP
> >http://www.sqlserverfaq.com


> message

> >> Howdy!  Need help on writing a trigger.  I need to
> write a
> >> trigger so that when a record is updated/inserted in one
> >> table, that the contents of that record are also
> inserted
> >> in another table.  Any ideas?

> >.

 
 
 

Write record to two tables

Post by Prayer Solank » Sun, 12 Jan 2003 05:31:09


Ah, I understand now.  It worked as it should when you do
the join.  Thank you!