Solved

Help with an insert query. sequence, and subquery

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

Join & Write a Comment

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…
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 syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.

762 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

22 Experts available now in Live!

Get 1:1 Help Now