Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ashok_gos
Starting Member
2 Posts |
Posted - 2008-04-07 : 02:44:16
|
| Hi, can anyone help me? I want to know whether is it possible to insert records simultaneously in both master & detail tables?For elaboration, say there is a master table contains (orderid,orderdate,amount) and details table contains (orderid, productid,qty,price). 1 record of master table associated with n records of details table. Can it be possible to insert both the 1 record at master table with n records in details table in a single sql statement? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-07 : 03:50:26
|
[code]-- Prepare sample dataDECLARE @Master TABLE (MasterID INT IDENTITY(1, 1), Name VARCHAR(20))DECLARE @Slave TABLE (SlaveID INT IDENTITY(1, 1), MasterID INT, Name VARCHAR(20))-- Insert first dummy recordINSERT @Master ( Name )SELECT 'Master 1'-- Insert second dummy recordINSERT @Master ( Name )OUTPUT inserted.MasterID, 'Slave 1'INTO @SlaveSELECT 'Master 2'-- Show resultsSELECT *FROM @MasterSELECT *FROM @Slave[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2008-04-07 : 07:13:18
|
quote: Originally posted by ashok_gos Hi, can anyone help me? I want to know whether is it possible to insert records simultaneously in both master & detail tables?For elaboration, say there is a master table contains (orderid,orderdate,amount) and details table contains (orderid, productid,qty,price). 1 record of master table associated with n records of details table. Can it be possible to insert both the 1 record at master table with n records in details table in a single sql statement?
Inserting records in multiple table with a single sql statement is not possible in MS SQL Server. This feature is in Oracle9i.Thanks,Mahesh |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-07 : 07:40:35
|
I think I proved you wrong 3 hours, 22 minutes and 52 seconds earlier. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|