Home » SQL & PL/SQL » SQL & PL/SQL » Insert values from different tables in a single statement
icon5.gif  Insert values from different tables in a single statement [message #669908] Wed, 23 May 2018 22:36 Go to next message
caiz
Messages: 2
Registered: May 2018
Junior Member
I would like to insert these values into a table. Student_ID, Student_Name are in the STUDENT table. Course_Code, Course_Name are in the COURSE table. Grade is in the GRADE table.

I have tried to put this in a single statement using:
INSERT INTO student (student_no, student_name, course_code, course_name, grade)
VALUES ('S-1478','Carol Wang', 'P1', 'Programming 1',70);

and have tried separating them:

INSERT INTO student (student_no, student_name)
VALUES ('S-7895','John Little');
INSERT INTO course (course_code, course_name)
VALUES ('ML', 'Machine Learning');

Both, with no luck
Re: Insert values from different tables in a single statement [message #669910 is a reply to message #669908] Wed, 23 May 2018 23:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

The statement will be an INSERT SELECT (or insert subquery) not an INSERT VALUES.
You have to first create the SELECT part which will return you the rows you want to then insert.

[Edit: add link]

[Updated on: Thu, 24 May 2018 00:25]

Report message to a moderator

Re: Insert values from different tables in a single statement [message #669913 is a reply to message #669910] Thu, 24 May 2018 02:45 Go to previous messageGo to next message
caiz
Messages: 2
Registered: May 2018
Junior Member
Hi Michael, would it still be INSERT SELECT because none of my tables have been populated yet?
Re: Insert values from different tables in a single statement [message #669914 is a reply to message #669913] Thu, 24 May 2018 03:19 Go to previous messageGo to next message
John Watson
Messages: 8922
Registered: January 2010
Location: Global Village
Senior Member
This does not make much sense. You had better provide the CREATE TABLE statements for your STUDENT, COURSE, and GRADE tables, and the the INSERT statements that will populate them. As far as you can.
Re: Insert values from different tables in a single statement [message #669915 is a reply to message #669913] Thu, 24 May 2018 03:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It is not clear what is the input and what is the result you want from it.
What does your image show?

Do what John said: provide CREATE TABLE and INSERT statements for the origin state then explain where you want to go from this state.

Re: Insert values from different tables in a single statement [message #669916 is a reply to message #669915] Thu, 24 May 2018 04:33 Go to previous message
Michel Cadot
Messages: 68624
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Unless your question is not "Insert values from different tables in a single statement" but "Insert values INTO different tables in a single statement".
In this case, have a look at multi-table insert.

Previous Topic: connect by prior performance issue
Next Topic: Find substr after nth occurance of spce
Goto Forum:
  


Current Time: Thu Mar 28 06:02:29 CDT 2024