Oracle: INSERT values from SELECT...JOIN, SQL Error: ORA-00947: not enough values

20.9k views Asked by At

I'm trying to do the following:

INSERT INTO MyTable(a, b, c)
SELECT a FROM source1
JOIN source2 ON ...

Where source2 contains columns B and C.

However Oracle doesn't seem to like this and is telling me "SQL Error: ORA-00947: not enough values".

Am I doing something wrong here? Is this syntax even possible? Or do I have to rewrite it as:

SELECT a, b, c FROM source1, source2 WHERE ....

Thanks!

2

There are 2 answers

2
Vincent Malgrat On BEST ANSWER

Use as many identifiers in the SELECT clause as in the INSERT clause, as in:

INSERT INTO MyTable(a, b, c)
SELECT s1.a, s2.b, s2.c FROM source1 s1
  JOIN source2 s2 ON ...
0
AudioBubble On

The select needs to return the same number of columns as you listed in the INSERT statement.

So: yes, you need to rewrite the query to SELECT a,b,c FROM ...