Solved

write a function to call a procedure to insert values into a table in PL SQL

Posted on 2016-09-25
7
28 Views
Last Modified: 2016-11-08
I am trying to create a SQL report with complex calculation and even though i created a procedure and function with the complete logic when i am trying to call my function to execute the procedure which has to insert records into my table its throwing an error  
ORA-14551: cannot perform a DML operation inside a query
But if i run the procedure explicitly the insertion operation is taking place. But my integrator only accepts function not procedure kindly help me out with this . this is the sample structure i am following

function ()
declare
...
...
..
begin
procedure(parameter);
..
..
..
end


procedure (parameter)
declare
..
..
..
begin
insert into table_name values (....);
or
EXECUTE IMMEDIATE insert into table_name values (....);
...
end


I have tried both the ways to insert but still its not working
0
Comment
Question by:Vignesh Marthandan
7 Comments
 
LVL 12

Expert Comment

by:Máté Farkas
ID: 41814635
Some notices:
  • This is not an MSSQL problem it is in Oracle (tag list extended)
  • In SQL Server or Oracle you cannot execute a procedure, dynamic sql or any DML/DDL operation inside a function
  • Why do you want to call a procedure inside a function? You have to create a procedure not a function.
  • How do you want to call your function?
0
 

Author Comment

by:Vignesh Marthandan
ID: 41814644
My DML operation is inside my procedure. I need to call my procedure using function to execute the DML statement. so is there any alternate way to execute my Procedure using select statement .
0
 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 332 total points
ID: 41814663
I cannot speak for SQL Server, but in Oracle, you most certainly can do dynamic SQL and DDL/DML within a function.  How you call it determines whether or not that is legal.

You can also call a procedure from within a function.  Again, how you call it determines whether that is legal or not.

Simple example:
create or replace procedure check_ret2 as
  cnt pls_integer;
begin
  select count(1) into cnt from user_tables;
end;
/
create or replace function check_ret return number as
begin
execute immediate 'create table z (x number)';
check_ret2();
return 0;
end;
/
declare 
  retval number;
begin
  retval := check_ret();
end;
/
select check_ret() from dual;

Open in new window

This shows a function that does dynamic DDL.  Then it calls a procedure.  If you execute from an anonymous block (the first method), it works just fine (this would be true if you were calling from any other type of PL/SQL object as well.  If you execute it the second way (within a select statement), then it will fail.

Judging by the original post stating that this is a report, my guess is that they are executing the function the second way, which would cause an error like the one they are seeing.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Accepted Solution

by:
Vignesh Marthandan earned 2 total points
ID: 41814695
Thank you johnsone. I have found an alternate solution for the same

create or replace PROCEDURE RECURRENCE_WEEK(inE# IN NUMBER := NULL) AS
PRAGMA AUTONOMOUS_TRANSACTION;

the key word PRAGMA AUTONOMOUS_TRANSACTION helps to perform DML operations in function and procedure
0
 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 332 total points
ID: 41814732
Sorry, we cross posted.  I was writing my case up while you were posting your other comments.

Yes, AUTONOMOUS_TRANSACTION will allow you to do that.  As long as you are aware of the consequences.  It is a separate transaction and if the main transaction is rolled back, the autonomous transaction is not.  Also, because it is a separate transaction outside of your select statement and function call, since the select was issued first, it will not be able to see the data from the autonomous transaction.  Those are the main ones that apply to what you are doing.
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
ID: 41815103
Creating tables inside procedures is rarely needed in Oracle.  Especially temporary ones like you seem to be describing.  There is almost always a better way.  You also need to worry about only one person executing the code at a time.

If you can explain a little more about your specific requirements we can probably offer alternatives.

I would look at one of the collection types offered.  This keeps everything in the procedure/function and in most cases, in memory.

http://docs.oracle.com/database/121/LNPLS/composites.htm#LNPLS005

In very rare cases, you might need temporary tables but even then, you don't create them in the code.  You use Oracle's Global Temporary tables.  You create them once and just keep reusing them in code.

http://docs.oracle.com/database/121/ADMIN/tables.htm#ADMIN11633
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

911 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

16 Experts available now in Live!

Get 1:1 Help Now