Link to home
Start Free TrialLog in
Avatar of dylanone
dylanone

asked on

Insert Query in Oracle 8i

In Oracle 8i I keep getting ORA-00907 Missing right parenthesis and the cursor brings me to this line:
(Test, Testdata, Testmessage, Hello Testing),
anyone see anything wrong anywhere?

INSERT INTO MAIN
            (
               ID,
               NAME,
               FAMILY,
               SUBJECT,
               MESSAGE,
               MAINDATE,
               SUBJECTID,
               MSGTIME)
               VALUES
               (
               (select id_seq.nextval from dual),
               (Test, Testdata, Testmessage, Hello Testing),
               (select to_char(sysdate, 'mm/dd/yy') from dual),
               (select id_seq.currval from dual),
               (select to_char(sysdate, 'hh:mi a.m.') from dual)
               )
Avatar of Bigfam5
Bigfam5

You cannot have a select inside a values list; and you cannot have multiple selects in one insert.

you this as a guid

insert into main
(ID, NAME, FAMILY,..MSGTIME)
(SELECT ID_SEQ.NEXTVAL, 'name','family',..., TO_CHAR(SYSDATE,'HH:MI A.M')
   from dual )
/
Try this:
INSERT INTO MAIN
           (
              ID,
              NAME,
              FAMILY,
              SUBJECT,
              MESSAGE,
              MAINDATE,
              SUBJECTID,
              MSGTIME)
              VALUES
              (
              (select id_seq.nextval from dual),
              Test, Testdata, Testmessage, Hello Testing,
              (select to_char(sysdate, 'mm/dd/yy') from dual),
              (select id_seq.currval from dual),
              (select to_char(sysdate, 'hh:mi a.m.') from dual)
              )

Hope that helps!
Is there a way to recall my stupid comment?! :)
ASKER CERTIFIED SOLUTION
Avatar of Mark Geerlings
Mark Geerlings
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops!
You can't have both "values" and "select" in an insert statement, just one or the other, and if "select...", then no parenthesis.  Here's how it should be:

INSERT INTO MAIN
          (
             ID,
             NAME,
             FAMILY,
             SUBJECT,
             MESSAGE,
             MAINDATE,
             SUBJECTID,
             MSGTIME)
             select id_seq.nextval,
             'Test', 'Testdata', 'Testmessage', 'Hello Testing',
             to_char(sysdate, 'mm/dd/yy'),
             id_seq.currval,
             to_char(sysdate, 'hh:mi a.m.')
             ;

Let me add some more thoughts...
My initial version of the statement will not work, but only because the "Test, Testdata, Testmessage, Hello Testing" values are strings (or at least it seams so), and have to be enclosed in quotes. ((that's why I called myself stupid, because I posted it without looking into the details)
Bigfam5 is right that it would be better to have just one select from dual, but he's not right that you cannot have select into the values list and that you cannot have multiple selects.
So if you add the quotes on my version it will work:
INSERT INTO MAIN
          (
             ID,
             NAME,
             FAMILY,
             SUBJECT,
             MESSAGE,
             MAINDATE,
             SUBJECTID,
             MSGTIME)
             VALUES
             (
             (select id_seq.nextval from dual),
             'Test', 'Testdata', 'Testmessage', 'Hello Testing',
             (select to_char(sysdate, 'mm/dd/yy') from dual),
             (select id_seq.currval from dual),
             (select to_char(sysdate, 'hh:mi a.m.') from dual)
             )

Avatar of dylanone

ASKER

Actually this one worked for me - after I took out the select statement before the id_seq.nextval

This proves a few things to me as well as others maybe:
1) you can use nextval currval in the same insert statement
2) strings should always be put in ' ' - I always miss that :)
3) you need the values keyword

I'm glad I'm not the only one who had trouble with this :)

Thank you all
The simplest way cuold be as follows:

INSERT INTO MAIN
           (
              ID,
              NAME,
              FAMILY,
              SUBJECT,
              MESSAGE,
              MAINDATE,
              SUBJECTID,
              MSGTIME)
              VALUES
              (
              id_seq.nextval,
              'Test', 'Testdata', 'Testmessage', 'Hello Testing',
              to_char(sysdate, 'mm/dd/yy'),
              id_seq.currval,
              to_char(sysdate, 'hh:mi a.m.')
              )

Bye, sidcap.