Solved

Help with an insert query. sequence, and subquery

Posted on 2004-04-14
5
1,430 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

867 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

12 Experts available now in Live!

Get 1:1 Help Now