Solved

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

Posted on 2016-09-25
7
25 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 11

Expert Comment

by:Máté Farkas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Accepted Solution

by:
Vignesh Marthandan earned 2 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

744 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

17 Experts available now in Live!

Get 1:1 Help Now