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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT in Master Detail tables

Status
Not open for further replies.

Ginka

Programmer
Mar 18, 2005
54
MX
How can I insert in 2 or more tables using a single query?
I wanna do this:
Table_1:
Id_Prod
Name

Table_2:
Id_Prod
Any_Other_Data

So if I need to fill it it should be like this
Table_1
Id_Prod = 1
Name = 'Something';

Table_2
Id_Prod = 1
Any_Other_Data = 'Any In Record # 1'
Id_Prod = 1
Any_Other_Data = 'Any In Record # 2'

 
You can only insert in 1 table at a time
wrap your inserts into a transaction to preserve data integrity

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
But how can I do that?


If the Id_Prod is autoincremental
I need to know which number the DB has assigned to the record and then save in the others tables with that Id_prod
 
Lookup @@IDENTITY in Books On Line

basically
insert into table1
select @id =@@IDENTITY

insert into table2
select @id, other fields

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top