Solved

Help with an insert query. sequence, and subquery

Posted on 2004-04-14
5
1,435 Views
Last Modified: 2008-02-01
Hi,
I have the following insert query - that does not work. Is there a solution to my problem.

NOTES: VCIL_KEY is a constant
             VCIL_OP_KEY needs to be the next value from an oracle sequence.

insert into vcil_summary (vcil_key,
                          vcil_op_key,
                          vcil_part_no,
                          vcil_find_no,
                          vcil_ret_rat,
                          vcil_location,
                          vcil_add_mod)
                  values (302,
                          vcil_op_key.nextval,
                          (select distinct part_no,
                                  cil_find_no,
                                  retention_rational,
                                  cil_location,
                                  'A'
                             from cil_view
                            where part_no = 'PART8001'
                         and cil_location = 'V'));


Thanks for the help.

Romans

0
Comment
Question by:Romans
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
pennnn earned 125 total points
ID: 10824776
insert into vcil_summary (vcil_key,
                          vcil_op_key,
                          vcil_part_no,
                          vcil_find_no,
                          vcil_ret_rat,
                          vcil_location,
                          vcil_add_mod)
                  select 302,
                          vcil_op_key.nextval,
                          t.* -- it's better to list the columns, I'm just lazy...
                     from (select distinct part_no,
                                  cil_find_no,
                                  retention_rational,
                                  cil_location,
                                  'A'
                             from cil_view
                            where part_no = 'PART8001'
                         and cil_location = 'V') t;
Hope that helps!
0
 
LVL 23

Expert Comment

by:seazodiac
ID: 10825562
or even simpler:

insert into vcil_summary (vcil_key,
                         vcil_op_key,
                         vcil_part_no,
                         vcil_find_no,
                         vcil_ret_rat,
                         vcil_location,
                         vcil_add_mod)
select distinct 302,
vcil_op_key.nextval,
 part_no,
cil_find_no,
retention_rational,
cil_location,
'A'
from cil_view
where part_no = 'PART8001'
and cil_location = 'V';
0
 

Author Comment

by:Romans
ID: 10826605
seazodiac,
Thanks for the input however the query does not work. I have included the error message etc. Looks like I will use the solution from pennnn.

Thanks


SQL> insert into vcil_summary (vcil_key,
  2                           vcil_op_key,
  3                           vcil_part_no,
  4                           vcil_find_no,
  5                           vcil_ret_rat,
  6                           vcil_location,
  7                           vcil_add_mod)
  8  select distinct 302,
  9  vcil_op_key.nextval,
 10   part_no,
 11  cil_find_no,
 12  retention_rational,
 13  cil_location,
 14  'A'
 15  from cil_view
 16  where part_no = 'PART8001'
 17  and cil_location = 'V';
vcil_op_key.nextval,
            *
ERROR at line 9:
ORA-02287: sequence number not allowed here
0
 

Author Comment

by:Romans
ID: 10826739
Pennnn,

The query works just fine but one further question if it's not too late. What do you mean by "-- it's better to list the columns". Can you explain this as I don't understand.

Thanks
Romans

0
 
LVL 11

Expert Comment

by:pennnn
ID: 10827181
What I meant was:
--....
select 302,
                          vcil_op_key.nextval,
                          t.part_no,
                          t.cil_find_no,
                          t.retention_rational,
                          t.cil_location,
                          t.a,
                     from (select distinct part_no,
                                  cil_find_no,
                                  retention_rational,
                                  cil_location,
                                  'A' a
                             from cil_view
--....
I think it's safer (you make sure that the order of the columns is correct) and more readable and easy to understand.
Hope that helps!
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

832 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