• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 940
  • Last Modified:

MySQL insert multiple table at same time

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
tangteng78
Asked:
tangteng78
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now