How do I write INSERT statement if I get the values of colA from TableX, colB from TableY and colC from TableZ?
For example:
INSERT INTO TableA (colA, colB, colC) VALUES (?,?,?)
Any ideas if it is possible?
How do I write INSERT statement if I get the values of colA from TableX, colB from TableY and colC from TableZ?
For example:
INSERT INTO TableA (colA, colB, colC) VALUES (?,?,?)
Any ideas if it is possible?
INSERT INTO TableA(colA, colB, colC)
SELECT TableX.valA, TableY.valB, TableZ.valC
FROM TableX
INNER JOIN TableY ON :......
INNER JOIN TableZ ON ........
Of course, TableX, TableY and TAbleZ might also be related in some other way (not INNER JOIN).
If you cannot find any relation between the tables AT ALL, you could also do three separate
SELECT @value1 = valA FROM TableX WHERE ......
SELECT @value2 = valB FROM TableY WHERE ......
SELECT @value3 = valC FROM TableZ WHERE ......
and then an insert like this:
INSERT INTO TableA(colA, colB, colC)
VALUES(@value1, @value2, @value3)
That's the ultimate last resort, you can can't express everything in a single SELECT statement.
Marc
Insert into TableA (ColA, ColB, ColC) . . .
Must be the column names as the are in Table A. There's nothing wrong with
Insert into TableA (ColA, ColB, ColC) . . .
Select TableX.Col1, TableY.Col1, TableZ.Col5
From TableX, TableY, TableZ
Where . . .
You will need to join the tables that you want to make the selection from.
Here is a resource on SQL joins:
www.w3schools.com/sql/sql_join.asp
You also might want to check out this free PDF book from the guys at www.simple-talk.com that covers SQL basics: