Home » SQL & PL/SQL » SQL & PL/SQL » INSERT with subquery in VALUES
INSERT with subquery in VALUES [message #628313] Sun, 23 November 2014 20:58 Go to next message
aliyesami
Messages: 28
Registered: October 2005
Location: Florida
Junior Member
i am reading everywhere that subquery cant be used in the value clause of an insert statement . I did some tests and if i use just one column than the subquery succeeds in value clause but for more than one column i am getting error , please zee below . Can someone explain this behaviour ?

thanks

SQL> CREATE TABLE NEW_TEST  AS SELECT * FROM TEST;

Table created.

SQL> INSERT INTO TEST VALUES (1,3);

1 row created.

SQL> COMMIT;

Commit complete.

SQL> INSERT INTO NEW_TEST (ID,VALUE) VALUES((SELECT ID,VALUE FROM TEST WHERE ID>
1));
INSERT INTO NEW_TEST (ID,VALUE) VALUES((SELECT ID,VALUE FROM TEST WHERE ID>1))
                                *
ERROR at line 1:
ORA-00947: not enough values


SQL>
SQL> INSERT INTO NEW_TEST (ID) VALUES((SELECT ID FROM TEST WHERE ID>1));

1 row created.

SQL> DESC  TEST
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                                 NUMBER(38)
 VALUE                                              NUMBER(38)

SQL> DESC NEW_TEST
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                                 NUMBER(38)
 VALUE                                              NUMBER(38)

SQL>



Edited by Lalit : Added code tags

[Updated on: Mon, 24 November 2014 01:18] by Moderator

Report message to a moderator

Re: INSERT with subquery in VALUES [message #628317 is a reply to message #628313] Mon, 24 November 2014 00:22 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Depends on your definition of "subquery". Your statements are trying to use scalar subquery expression, which may be used as an expression, e.g. in VALUES clause.
It is described e.g. in SQL Language Reference: https://docs.oracle.com/cd/E11882_01/server.112/e41084/expressions013.htm#SQLRF52093
As you can see, it has the limits you observed: one column and at most one value returned, so the first INSERT statement fails.

By the way, how did you find out the necessity of extra parenthesis? Seems that that source is not part of "everywhere".
Re: INSERT with subquery in VALUES [message #628319 is a reply to message #628313] Mon, 24 November 2014 01:18 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
aliyesami wrote on Mon, 24 November 2014 08:28

SQL> INSERT INTO NEW_TEST (ID,VALUE) VALUES((SELECT ID,VALUE FROM TEST WHERE ID>
1));
INSERT INTO NEW_TEST (ID,VALUE) VALUES((SELECT ID,VALUE FROM TEST WHERE ID>1))
*
ERROR at line 1:
ORA-00947: not enough values



You cannot do that using VALUES clause.

SQL> drop table emp_new purge;

Table dropped.

SQL> create table emp_new as select * from emp where 1=2;

Table created.

SQL> insert into emp_new select * from emp;

14 rows created.

SQL> select count(*) from emp_new;

  COUNT(*)
----------
        14

SQL> insert into emp_new(empno, ename) select empno, ename from emp;

14 rows created.

SQL> select count(*) from emp_new;

  COUNT(*)
----------
        28

SQL>


And please use code tags. Read http://www.orafaq.com/forum/t/174502/
Re: INSERT with subquery in VALUES [message #628320 is a reply to message #628319] Mon, 24 November 2014 01:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
You cannot do that using VALUES clause.


And what about what flyboy said?

Re: INSERT with subquery in VALUES [message #628325 is a reply to message #628320] Mon, 24 November 2014 02:39 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel Cadot wrote on Mon, 24 November 2014 12:59

Quote:
You cannot do that using VALUES clause.


And what about what flyboy said?



Sorry, I don't understand your question.

I completely agree with Flyboy, he said, Quote:
it has the limits you observed: one column and at most one value returned
.

And I showed how it could be done for multiple columns, i.e. not using VALUES clause.
Re: INSERT with subquery in VALUES [message #628333 is a reply to message #628325] Mon, 24 November 2014 03:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So, you just added confusion, Flyboy's answer was complete and clear, and OP already knows it is not possible as this is his question.

Re: INSERT with subquery in VALUES [message #628363 is a reply to message #628317] Mon, 24 November 2014 09:01 Go to previous messageGo to next message
aliyesami
Messages: 28
Registered: October 2005
Location: Florida
Junior Member
flyboy wrote on Mon, 24 November 2014 00:22

By the way, how did you find out the necessity of extra parenthesis? Seems that that source is not part of "everywhere".

That was my second question I was planning to ask, why the following fails ? I found out the right syntax by just playing around with the syntax and guessing.

SQL> INSERT INTO NEW_TEST (ID) VALUES(SELECT ID FROM TEST WHERE ID>1);
INSERT INTO NEW_TEST (ID) VALUES(SELECT ID FROM TEST WHERE ID>1)
                                 *
ERROR at line 1:
ORA-00936: missing expression


thanks for your help
Re: INSERT with subquery in VALUES [message #628369 is a reply to message #628363] Mon, 24 November 2014 09:32 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Flyboy almost exlained it too Smile The braces are critical in this case. Values clause expects a single value for one column inside braces, however, the subquery result should be resolved before to provide the value, and it needs to be inside braces. Else, the values clause thinks the subquery itself to be the value before it is executed.
Re: INSERT with subquery in VALUES [message #628372 is a reply to message #628363] Mon, 24 November 2014 09:38 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Thank you for feedback, hope you read about it in the link I posted.

Just for clearer demonstration of scalar subquery expression, these statements are also correct:
INSERT INTO NEW_TEST (ID,VALUE)
VALUES(1, (SELECT VALUE FROM TEST WHERE ID>1));

INSERT INTO NEW_TEST (ID,VALUE)
VALUES((SELECT ID FROM TEST WHERE ID>1), 3);
Re: INSERT with subquery in VALUES [message #628373 is a reply to message #628369] Mon, 24 November 2014 09:40 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
OP has solution looking for a problem.

http://en.wikipedia.org/wiki/Snipe_hunt
Re: INSERT with subquery in VALUES [message #628404 is a reply to message #628363] Mon, 24 November 2014 15:43 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
aliyesami wrote on Mon, 24 November 2014

I found out the right syntax by just playing around with the syntax and guessing.

This is probably the worst learning path you could have chosen.
Re: INSERT with subquery in VALUES [message #628406 is a reply to message #628404] Mon, 24 November 2014 17:31 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
+1

OP belongs to the Ready, Fire, AIM school of programming.
Previous Topic: PL/SQL email config
Next Topic: json data
Goto Forum:
  


Current Time: Fri Apr 26 07:08:53 CDT 2024