Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to avoid querying the same table two times.

921556Mar 27 2012 — edited Mar 28 2012
Hi all,

I am wondering if any one can tell me a better way to do what I am trying to do.
I am querying a table twice to get some values before I insert into the same table.
I am pretty sure there is a better way to do this.

select nvl(max(pd.line_no), 0) + 1
into p_new_line_no
from po_details pd
where pd.event_id = p_event_id;

select pd.cause, pd.cause_code
into l_cause, l_cause_code
from po_details pd
where pd.po_number = p_po_number
and pd.comp_code = p_comp_code
group by pd.comp_code;


insert into po_details
(qty, cost, cause, cause_code, line_no)
select p_qty, p_cost, l_cause, l_cause_code, p_new_line_no);

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 25 2012
Added on Mar 27 2012
7 comments
1,038 views