Solved

Returning into clause for an INSERT from a SELECT statement

Posted on 2008-10-01
8
3,464 Views
Last Modified: 2013-12-19
Hi,
I have the attached insert which creates a new audit_template. How do I get the NEW AUDIT_TEMPLATE_ID?

I tried putting:
returning AUDIT_TEMPLATE_ID into lNewTemplateID ;

get this error:
ORA-06550: line 21, column 4:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 5, column 1:
PL/SQL: SQL Statement ignored1. DECLARE
2.     lNewTemplateID NUMBER;
3.      pCloneTemplateId NUMBER:=205;
4. BEGIN
 
without the
returning AUDIT_TEMPLATE_ID into lNewTemplateID ;
it inserts great!


DECLARE
    lNewTemplateID NUMBER;
     pCloneTemplateId NUMBER:=205;
BEGIN
INSERT INTO AUDIT_TEMPLATE (
   TITLE, LINE_OF_BUSINESS, 
   ACTIVE, RESPONSE_TYPE_1, 
   RESPONSE_TYPE_2, RESPONSE_TYPE_3, FILE_NBR_DISP, 
   CLIENT_DISP, CLAIMANT_DISP, OFFICE_DISP, 
   EXAMINER_DISP, SUPERVISOR_DISP, NURSE_DISP, 
   NURSE_SUPERV_DISP, TEMPL_CLIENT,cloned_from) 
select 
   TITLE||' (Copy)', LINE_OF_BUSINESS, 
   ACTIVE, RESPONSE_TYPE_1, 
   RESPONSE_TYPE_2, RESPONSE_TYPE_3, FILE_NBR_DISP, 
   CLIENT_DISP, CLAIMANT_DISP, OFFICE_DISP, 
   EXAMINER_DISP, SUPERVISOR_DISP, NURSE_DISP, 
   NURSE_SUPERV_DISP, TEMPL_CLIENT,AUDIT_TEMPLATE_ID 
   from AUDIT_TEMPLATE 
   where AUDIT_TEMPLATE_ID = pCloneTemplateId
returning AUDIT_TEMPLATE_ID into lNewTemplateID ;
 
htp.prn('Old ID['|| pCloneTemplateId||']');
htp.prn('New ID['|| lNewTemplateID||']');
END;

Open in new window

0
Comment
Question by:bcarlis
[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
8 Comments
 
LVL 15

Expert Comment

by:Shaju Kumbalath
ID: 22614128
You can't use RETURNING in combination with a subquery on an insert, only with a VALUES clause
0
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 22614156
You cannot use returning clause if you use insert into ... select .... You can use it only for insert into ... values ....

See this:
SQL> create table bla (id number, value number);
 
Table created.
 
SQL> var x number;
SQL> insert into bla values (1, 2) returning id into :x;
 
1 row created.
 
SQL> insert into bla select 1, 2 from dual returning id into :x;
insert into bla select 1, 2 from dual returning id into :x
                                                *
ERROR at line 1:
ORA-00933: SQL command not properly ended

Open in new window

0
 
LVL 2

Author Comment

by:bcarlis
ID: 22614270
What is a solution for getting it?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 14

Accepted Solution

by:
GGuzdziol earned 68 total points
ID: 22614322
You can fetch the row you want to insert into local variables (i.e. instead of insert into ... select ... you put select ... into ...) and then issue INSERT INTO ... VALUES ... RETURNING.
0
 
LVL 4

Assisted Solution

by:KICUSek
KICUSek earned 66 total points
ID: 22623293
I believe You can BULK COLLECT into collection.
0
 
LVL 15

Assisted Solution

by:Shaju Kumbalath
Shaju Kumbalath earned 66 total points
ID: 22634402

DECLARE
lNewTemplateID NUMBER;

TYPE ARRAY IS TABLE OF shaju1%ROWTYPE;
l_data ARRAY;
pCloneTemplateId NUMBER:=205;
BEGIN
 
select AUDIT_TEMPLATE_ID,
TITLE||' (Copy)', LINE_OF_BUSINESS,
ACTIVE, RESPONSE_TYPE_1,
RESPONSE_TYPE_2, RESPONSE_TYPE_3, FILE_NBR_DISP,
CLIENT_DISP, CLAIMANT_DISP, OFFICE_DISP,
EXAMINER_DISP, SUPERVISOR_DISP, NURSE_DISP,
NURSE_SUPERV_DISP, TEMPL_CLIENT,AUDIT_TEMPLATE_ID into l_data
from AUDIT_TEMPLATE
where AUDIT_TEMPLATE_ID = pCloneTemplateId
 
INSERT INTO AUDIT_TEMPLATE
values l_data(1)
returning AUDIT_TEMPLATE_ID into lNewTemplateID ;

htp.prn('Old ID['|| pCloneTemplateId||']');
htp.prn('New ID['|| lNewTemplateID||']');
END;
 
 
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
minium over 4 numeric columns for each row in oracle 2 56
Help with Oracle IF statment 5 50
loop having error 5 45
Moving Oracle Database to other server 2 47
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

752 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