Solved

Insert Query in Oracle 8i

Posted on 2002-03-12
8
11,872 Views
Last Modified: 2011-08-18
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)
               )
0
Comment
Question by:dylanone
8 Comments
 
LVL 7

Expert Comment

by:Bigfam5
ID: 6857290
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 )
/
0
 
LVL 11

Expert Comment

by:pennnn
ID: 6857293
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!
0
 
LVL 11

Expert Comment

by:pennnn
ID: 6857299
Is there a way to recall my stupid comment?! :)
0
 
LVL 34

Accepted Solution

by:
Mark Geerlings earned 80 total points
ID: 6857344
Trying to use both "nextval" and "currval" in the same SQL statement is something I've never attempted - I'm not sure if that will work or not, but it may.  If it does then this should work for you:
INSERT INTO MAIN
           (
              ID,
              NAME,
              FAMILY,
              SUBJECT,
              MESSAGE,
              MAINDATE,
              SUBJECTID,
              MSGTIME)
              VALUES
              (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.')
              );
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Expert Comment

by:Mark Geerlings
ID: 6857356
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.')
             ;

0
 
LVL 11

Expert Comment

by:pennnn
ID: 6857369
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)
             )

0
 

Author Comment

by:dylanone
ID: 6857740
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
0
 
LVL 3

Expert Comment

by:sidcap
ID: 6857751
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now