Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 15th, 2003, 03:31 PM
Authorized User
 
Join Date: Jun 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default trigger

Hi all,

I have tbl1 and tbl2 they have the same number of columns.

I'd like to write trigger that every time I insert a new row into the tbl1 it will trigger and insert same row to tbl2.

Ex:

if I insert into tbl1
col1 col2
---- ----
ad bc

then the trigger will insert into tbl2
col1 col2
---- ----
ad bc

I can't think of the way to do. Please anyone who has been there gives me a tip. Thanks.
 
Old October 15th, 2003, 03:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

The 'inserted' pseudo table lists the rows which have been inserted into a table. Thus, code in the trigger along the lines of:
Code:
INSERT INTO tbl2 (col1, col2)
    SELECT col1, col2 FROM inserted
would do the trick, but for a complication when rows are updated.

The 'deleted' pseudo table lists those rows which have been removed. Updates to a table are implemented as a delete and add, so updated rows are in both tables, and if you only care about new rows, you'll want to modify that INSERT so it only selects rows which are not in 'deleted'.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 16th, 2003, 02:44 AM
Authorized User
 
Join Date: May 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi khautinh,
 Try this:

  create or replace trigger triggername
  BEFORE UPDATE on tb1
  FOR EACH ROW
  begin
    insert into tb2 values(:old.col1,:old.col2);
  end;



Deepesh Jain
VB,VBA & .NET Specialist
Wiley Support Team
 
Old October 16th, 2003, 06:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote: create or replace trigger triggername
BEFORE UPDATE on tb1
FOR EACH ROW
begin
    insert into tb2 values(:old.col1,:old.col2);
end;
Are you sure you are posting T/SQL - i.e. SQL Server syntax?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 16th, 2003, 10:27 AM
Authorized User
 
Join Date: Jun 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for all helps, but it didn't work out well. The trigger doesn't know (the id of the row) which I have updated, deleted or inserted on tlb1 so it can do same thing on tbl2. This is so tricky.
Ex: if I insert, delect or update a certain row with the id 34 in tbl1, the trigger doesnt' know to look for the id 34 in tbl2 to do the same.
 
Old October 16th, 2003, 11:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

The 'inserted' and 'deleted' pseudo tables contain all of the columns in all of the rows being inserted and/or deleted, so all of the data being inserted/updated/deleted is available for the trigger to inspect.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trigger Help monika.vasvani SQL Language 2 March 1st, 2007 06:59 AM
Trigger arshad mahmood C++ Programming 4 June 24th, 2004 07:10 AM
Trigger ! minhtri Pro VB Databases 2 June 23rd, 2004 02:27 AM
Trigger arshad mahmood SQL Language 2 May 12th, 2004 05:16 AM
Using instead of trigger dmr999 SQL Server 2000 1 November 29th, 2003 02:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.