Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with an insert query. sequence, and subquery

Posted on 2004-04-14
5
Medium Priority
?
1,445 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 500 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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.
Suggested Courses

885 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