?
Solved

Help with an insert query. sequence, and subquery

Posted on 2004-04-14
5
Medium Priority
?
1,444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

719 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