473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Insert into two related tables

Hey there, I'd like to insert into two tables simultaneously, where the value of the first column in the first table is the same as the value of the second column in the second table. Here's an example of what I'm trying to achieve:

Table 1
pri_ID, some_data
10,words
11,something

Table 2
sec_ID, pri_ID, other_data
1,10,morewords
2,11,somethingelse

Both pri_ID and sec_ID are auto increments, and some_data/more_data are inserted from php. The whole statement will be wrapped in ADOdb functions.
Expand|Select|Wrap|Line Numbers
  1. $DB->BeginTrans();
  2. $DB->Execute("SQL STATEMENT GOES HERE");
  3. $DB->CommitTrans();
  4.  
Please let me know if I need to give any more info. Thanks for your time!
Feb 28 '11 #1
8 7092
Rabbit
12,516 Expert Mod 8TB
You can't do it simultaneously. You insert into the primary key table and then insert into the foreign key table.
Feb 28 '11 #2
Just to make sure: two separate statements, with the second one calling the primary key of the first? Is there an optimal way to do that during the php function that you know of?
Feb 28 '11 #3
ck9663
2,878 Expert 2GB
Yes, two separate statements. It is recommended to do it all in sql server for you to keep the consistency of the relationship.

Enclose your insert statements in a transaction so that if the other insert fails, you can rollback the previous one. You also need to consider to make sure your sequence of auto-number revert back to the value before the fail.

Now, to make sure that you insert only those record that the first statement just inserted, use this clause. Either that or you create an "update-able" view to handle it.

Happy Coding!!!

~~ CK
Feb 28 '11 #4
I'm not quite sure what you mean by "do it all in sql server" - I am doing it all in php, using ADOdb as my go-between.

The statements are wrapped in a transaction - my first post makes that clear. I even gave the code of the transaction it will be wrapped in.

So, to make this a single action, is this what you mean?

Expand|Select|Wrap|Line Numbers
  1. DECLARE @dummytablevar(pri_ID int);
  2. INSERT INTO table1(some_data)
  3.     OUTPUT INSERTED.pri_ID
  4.         INTO @dummytablevar
  5. VALUES ('somethinghere');
  6. INSERT table2(pri_ID,other_data)
  7. VALUES (INSERTED.pri_ID,'somethingelse');
Mar 1 '11 #5
ck9663
2,878 Expert 2GB
What you gave is a block of command,not transaction. I am talking about this

Your last post tells me you're on the right track. The reason am recommending you wrap in transaction so that when there's an error on your second insert, you can rollback everything. Otherwise, you'll have data on your first table, but not on the second table. Also, identity property cannot be reused immediately. You need a couple of steps to reuse it and it has to be properly handled. Otherwise you'll have missing identity values and worse the tables being not sync.

Good Luck!!!

~~ CK
Mar 1 '11 #6
Okay, I think I'm getting closer here. I'm using adoDB to talk to SQL server - here's what I have so far.

Expand|Select|Wrap|Line Numbers
  1. $sql = "INSERT INTO table1(some_data) VALUES ("somethinghere")";
  2. $db->StartTrans();
  3. $db->_Execute($sql);
  4. $newID = $db->Insert_ID();
  5. $sql2 = "INSERT INTO table2(table1_ID,other_data) VALUES ($newID,"somethingelse")
  6. $db->_Execute($sql2);
  7. $db->CompleteTrans();
  8.  
Now, from what I understand, our older version of ADOdb (3.9x) doesn't handle Insert_ID the same way as @@IDENTIFY would (it doesn't work at all during a wrapped transaction), but I'm not sure quite how to work @@IDENTIFY or some variation into the script. Any ideas here?
Mar 1 '11 #7
ck9663
2,878 Expert 2GB
Your sql variable doesn't need to have only one statement. It can contain multiple statements. As long as you split it with semi colon inside the quote. Not that semicolon outside. So your two insert statement can be in one variable as long as it can accommodate the string.

What I am trying to recommend is to create a stored proc that you can pass those values, let your stored procedure handle the insert, the transaction processing and the @@IDENTITY. The problem with merely using @@IDENTITY is you have to consider if there are multiple session trying to update your table.

Good Luck!!!

~~ CK
Mar 1 '11 #8
The semicolon outside is the endline for php.

I discovered a different method that works beautifully when I put it into the SQL editor directly, but it's not functional when executing through php.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO table1(some_data)
  2. VALUES ('some notes');
  3. INSERT INTO table2(Pri_ID,other_data)
  4. VALUES (IDENT_CURRENT('table1'),'something else')
  5.  
I'll head over to the php forums for the rest of this, then. I think my issues are stemming from ADOdb at this point. Thanks for all your help, ck!
Mar 1 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
1
by: Christa Waggett | last post by:
Hi, I'm not a programmer but would appreciate some help with the following. I've been looking at various sites but cannot find the information I require. I have a table of strata plans and if we...
4
by: Patrick | last post by:
I have a Windows Forms DataGrid bound to a Table within a DataSet. The Table is related to others within the DataSet. The DataGrid allows the user to surf through related tables. I don't want...
3
by: kashifahmed.bse.mg | last post by:
Hello All, I would like to insert dynamically created related tables by using datatable collections in ado.net. So can anyone figure out which procedure is usefull to do this task. Example will...
0
by: Nedim | last post by:
i created a typed dataset with related tables, but i dont know how to display related values, for example, i have a tables Person and City, which are related like Person.CityId = City.Id (they are...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
5
by: upwardgazing | last post by:
I'm using Access 2003 (Access 2000 file format) and I have two tables related one-to-many called tblTempEncounter and tblTempEncounterDetails. I need to move a record from the first table with it's...
0
by: PreethiParkavi | last post by:
Hi, I am having two tables : Offer and OfferItems.Both tables having Identity fields as primary keys Offer_ID and OfferItem_ID respectively. the field "OI_OfferID" in OfferItem table relates...
3
by: simple simon | last post by:
How would I insert into multiple related tables using a table valued parameter? Is there any way to do this without using a WHILE loop? I know how to insert from a table valued parameter into one...
3
by: southpawjoe | last post by:
Hi, I would like to insert data into two related tables in a SQL Server 2008 database. I created the following stored procedure but it has a problem. When I add a record, the State table gets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.