Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy record and all related records

Status
Not open for further replies.

Page410

Technical User
Mar 9, 2001
106
0
0
US
I was wondering if there was any way to identify and/or copy all records related to a record being copied.

Let's say I have 5 tables A,B,C,D,E

My goal: When I copy record A1 to create record A2 I would like to identify all records in B,C,D,E (if they exist) and create records in those tables to relate to the new record A2.

I can write a routine for this but figured I would toss it out there before I started as I'm not one for re-inventing the wheel.

 
When you say "records being copied"

do you mean records being "selected"
or Records being "Inserted"

?

If it is on the "Insert" (SQL has no COPY - that is a file opp.. DOS Type) then YES you can..

The thing you need is called a "TRIGGER" and they can watch for things like "INSERT", "UPDATE" and "DELETE"..

These things are basicly the "EVENTS" that Sql can SEE and you can write code to handle these events..

e.g.
Code:
Create Trigger Tab1_Insert
on Tab1
For Insert 
as
Insert into TableB select * from inserted

would "fire" on the "Insert Event" and Copy all the Rows being inserted into Tab1 into TableB


HTH


Rob

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top