?
Solved

MySQL insert multiple table at same time

Posted on 2009-02-22
2
Medium Priority
?
937 Views
Last Modified: 2012-05-06
Say, i want to insert data into 2 tables...table1 and table2 at the same time.
Any idea what's the SQL syntax to use?

table1
- table1_id
- info1
- info2

table2
- table2_id
- table1_id -> the id comes from table1
- info3
- info4
0
Comment
Question by:tangteng78
  • 2
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 23708859
you cannot insert "at the same time".
you have to insert into table1, and then into table2:
INSERT INTO table1 (info1, info2) VALUES('info1', 'info2');
INSERT INTO table2 (table1_id, info3, info4) VALUES(last_insert_id(), 'info3', 'info4');

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 23708866
the above is MySQL syntax. as you posted in both MySQL and MSSQL zones, please specify...

mssql version:
INSERT INTO table1 (info1, info2) VALUES('info1', 'info2');
INSERT INTO table2 (table1_id, info3, info4) SELECT scope_identity(), 'info3', 'info4';

Open in new window

0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question