Writing a simple procedure to call another procedure
Greetings,
im pretty new to pl/sql and just recently started playing around with it, I was wondering howi would go about creating a basic skeleton of procedure that would call another procedure.
function testfunction is the function I wish to call and accepts 3 params, it then returns a pl/sql table which contains all the rows I want. I then want to populate a table with this data. Do I need to create a loop within the procedure to populate the table or can I use a function of some sort to just directly insert the entire pl/sql table into the actual table?
Thankyou both for the follow ups! I may have to go with the loop method but before I dive into that I was reading about the merge function and feel as though it would do what I need perfectly (I just need to append data to a table)
So I came up with something like this
PROCEDURE test_procedure
IS
BEGIN
MERGE INTO test_table
USING (test_package.test_function(123, 456, 789))
WHEN MATCHED THEN UPDATE SET FIELDA = resulta, FIELDB = resultb, FIELDC = resultc
WHEN NOT MATCHED THEN INSERT (FIELDA, FIELDB, FIELDC) VALUES (resulta, resultb, resultc)
END test_procedure;
This seems like it would work great, the resulta, resultb, and resultc are all returned from the function but im not sure exactally about that being the correct way to call it. I will go read up on this approach but fall back on the looping if it doesnt work out, thankyou everyone =D
Similar Messages
-
In SQLScript, how to use EXEC to call another procedure with parameters in procedure?
Hi experts,
In SQLScript, How to use EXEC to call another procedure with input and output parameters in procedure?thanks very muchHi Sagar,
thank you! I generate another procedure with an input parameter and an output parameter in a procedure. Then i need to call the generated procedure using EXEC. Here is my code:
create procedure ftest1(out sum_num bigint)
as
begin
declare fa_output bigint;
declare v_sql_drop varchar(200);
declare v_sql varchar(500);
declare cursor c_cursor1 for select num from TABLE1;
--v_sql_drop := 'drop procedure fe';
--exec v_sql_drop;
v_sql := 'create procedure fe(in i_num bigint,out o_num bigint) as begin';
v_sql := :v_sql || ' o_num := :i_num * 2 + :i_num * :i_num;';
v_sql := :v_sql || ' end';
exec v_sql;
open c_cursor1;
for c_item as c_cursor1 do
exec 'call fe(c_item.num,o_num=>fa_output)';
if sum_num is null then
sum_num := fa_output;
else
sum_num := :sum_num + fa_output;
end if;
end for;
close c_cursor1;
end;
The underline code is using exec to call the generated procedure. But this method cannot work. Any suggestion? thanks again! -
Dynamically call another procedure
In SQLServer you can have a variable that holds a procedure
name. The variable can then be used in the execute statement.
Is there any way in PLSQL to dynamicaly call another procedure.
example:
begin
declare procname varchar2(32) = 'proc1';
--now call the procedure
procname -- ?
execute immediate procname -- ?
end;
Thanks,
Dermot.Just wrap it in an anonymous block:
create or replace procwrap
(p_procname in varchar2)
is
begin
execute immediate 'begin ' || p_procname || '; end;';
end; -
Need to write a plsql block for calling another procedure.
Hi Guys,
I have a requirement to write a plsql block and to call another procedure which is having 5 parameters (each parameter having multiple values). Calling procedure generates one SQL query and it needs to be generated all combinations and print in another table.
I need to pass multiple values for first three parameters from my PLSQL block and run.
call sample proc:
procedure(
param1 varchar2,
param2 p_varchar2,
param3 p_varchar2,
param4 in varchar2,
param5 in number
is
begin
ls_sql_query := 'with client_query as (select * from table)';
end;
Thanks in advance!
Rgds,
LRKThis article is on exactly your subject. It is worth a careful read.
Ask Tom: On Popularity and Natural Selection
I paraphrase: "Always code a reference to every possible bind variable but do so in such a way that the optimizer removes the bind variable for us if we aren't going to actually use it in that particular execution of the SQL statement."
P.S. This is the same answer I gave when you asked the same question earlier. -
PL/SQL: Executing a procedure from within another procedure
Hello, I'm a newbie and I need help on how to execute procedures from within another procedure. The procedure that I call from within the procedure have return values that I want to check.
I tried: EXECUTE(user_get_forum_info(p_forumid, var_forum_exists, var_forum_access, var_forumname));
but I get the error message:
PLS-00103: Encountered the symbol "USER_GET_FORUM_INFO" when expecting one of the following::= . ( @ % ; immediate
The symbol ":=" was substituted for "USER_GET_FORUM_INFO" to continue.
And when I tried: EXECUTE(user_get_forum_info(p_forumid, var_forum_exists, var_forum_access, var_forumname));
I get the error message:
PLS-00222: no function with name 'USER_GET_FORUM_INFO' exists in this scope
PL/SQL: Statement ignored
The procedure USER_GET_FORUM_INFO exists. (don't understand why it says "no FUNCTION with name", it's a procedure I'm executing)
I'm stuck so thanks for any help...
Below is all the code. I'm using Oracle 9i on RedHat Linux 7.3.
================================================================================
CREATE OR REPLACE PROCEDURE user_forum_requestsaccess (
p_forumid IN NUMBER,
p_requestmessage IN VARCHAR2
AS
var_forumid NUMBER;
var_forum_exists NUMBER;
var_forum_access NUMBER;
request_exists NUMBER;
var_forumname VARCHAR2(30);
FORUM_DOESNT_EXIST EXCEPTION;
FORUM_USER_HAS_ACCESS EXCEPTION;
FORUM_REQUEST_EXIST EXCEPTION;
BEGIN
SELECT SIGN(NVL((SELECT request_id FROM forum.vw_all_forum_requests WHERE forum_id = p_forumid AND db_user = user),0)) INTO request_exists FROM DUAL;
EXECUTE(user_get_forum_info(p_forumid, var_forum_exists, var_forum_access, var_forumname));
IF var_forum_exists = 0 THEN
RAISE FORUM_DOESNT_EXIST;
ELSIF var_forum_access = 1 THEN
RAISE FORUM_USER_HAS_ACCESS;
ELSIF request_exists = 1 THEN
RAISE FORUM_REQUEST_EXIST;
ELSE
INSERT INTO tbl_forum_requests VALUES (SEQ_TBL_FORUM_REQ_REQ_ID.NEXTVAL, SYSDATE, p_requestmessage, p_forumid, user);
INSERT INTO tbl_forum_eventlog VALUES (SEQ_TBL_FORUM_EVNTLOG_EVNT_ID.NEXTVAL,SYSDATE,1,'User ' || user || ' requested access to forum ' || var_forumname || '.', p_forumid,user);
COMMIT;
END IF;
EXCEPTION
WHEN
FORUM_DOESNT_EXIST
THEN RAISE_APPLICATION_ERROR(-20003,'Forum doesnt exist.');
WHEN
FORUM_USER_HAS_ACCESS
THEN RAISE_APPLICATION_ERROR(-20004,'User already have access to this forum.');
WHEN
FORUM_REQUEST_EXIST
THEN RAISE_APPLICATION_ERROR(-20005,'A request to this forum already exist.');
END;
GRANT EXECUTE ON user_forum_requestsaccess TO forum_user;
================================================================================
Regards Goranyou don't have to use execute when you want to execute a procedure (only on sql*plus, you would use it)
just give the name of the funtion
create or replace procedure test
as
begin
dbms_output.put_line('this is the procedure test');
end test;
create or replace procedure call_test
as
begin
dbms_output.put_line('this is the procedure call_test going to execute the procedure test');
test;
end call_test;
begin
dbms_output.put_line('this is an anonymous block calling the procedure call_test');
call_test;
end;
/ -
Pocedure calling another procedure
Hi all,
I hav a procedure which will display all the student records.
CREATE OR REPLACE procedure a
AS
CURSOR o IS
SELECT * FROM lgrade;
o_rec o%ROWTYPE;
BEGIN
OPEN o;
LOOP
FETCH o INTO o_rec;
EXIT WHEN o%NOTFOUND;
dbms_output.put_line(o_rec.cno||' '||o_rec.csno ||' '||o_rec.descript||' '|| o_rec.sfname||' '||o_rec.slname||' '|| o_rec.score||' '|| o_rec.grade);
END LOOP;
CLOSE o;
END;
It displays course, section numbers, student's name , score and grade.
CS 10 2 DP Overview Jeff Runyan 86.333 B
CS 100 2 Hands, On Windows Peggy Noviello 89 B
CS 100 1 Hands, On Windows Debra Boyce 88.766 B
CS 100 1 Hands, On Windows Rommel Frost 88.1 B
CS 100 2 Hands, On Windows J. Segall 85.333 B
CS 100 5 Hands, On Windows Jose Benitez 91.6 A
CS 100 4 Hands, On Windows Mrudula Philpotts 90.613 A
CS 100 4 Hands, On Windows Gene Bresser 84.568 B
CS 100 3 Hands, On Windows Michael Lefbowitz 86.916 B
CS 100 1 Hands, On Windows Lula Oates 82.1 B
CS 100 1 Hands, On Windows Roger Snow 90.266 A
CS 100 3 Hands, On Windows Monica Waldman 86.472 B
CS 100 1 Hands, On Windows Regina Bose 94.1 A
CS 100 1 Hands, On Windows Jenny Golsmith 87.1 B
1) I want to use this procedure inside another procedure say b to display only certain cousre records. How can i do that?
2) I want to use this procedure inside another procedure to display particular student's different course grades. How can i do that?PL/SQL is not usually used this way - displaying data. The DBMS_OUTPUT interface is very primitive - and does not "display" anything. It is a static PL buffer that contains lines. The client, e.g. SQL*Plus/TOAD, displays its contents after calling Oracle to execute code.
That said, PL/SQL can be used to teach programming fundamentals - despite the fact that this is NOT how one would usually apply the PL/SQL language to real problems.
A display procedure should be just that - display. Nothing else. A well designed procedure (or function) does a single job, and does it well. So a display proc can look as follows:
create or replace procedure DisplayStudent( studentRow lgrage%rowtype ) is
begin
dbms.output.put_line( ... insert your display contents ... );
.. etc ..
end;
1) I want to use this procedure inside another procedure say b to display only certain cousre records. How can i do that?
2) I want to use this procedure inside another procedure to display particular student's different course grades. How can i do that?The same way. By using SQL to specify the data set that you want to display. For example:
begin
for student in ( select l.* from lgrade l where l.score >= 80 ) -- display students with a score of 80% plus
loop
DisplayStudent( student ); -- call the display proc and pass it a student row to display
end loop;
end;
/And that is the basic approach. Two important factors.
Modularise. A good program design means creating building blocks to solve the problem. Where a block can be re-used (like the DisplayStudent procedure above).
Maximize SQL. SQL is a very capable and powerful program language. You use it to crunch the data. You do not pull all the rows from the table into PL/SQL and then apply an IF condition to filter the data. That is row-by-row and slow-by-slow processing. -
Calling another procedure in a procedure
How can we execute procedure in another procedure?
Try running following code
create or replace procedure child is
begin
dbms_output.put_line('This text from Child');
end;
create or replace procedure parent is
begin
child;
end;
exec parent;
null -
Call procedure inside from another procedure.
I have Procedure Name stored in tables.
Based on certain logic i need to call some procedure.
Procedure will be called from inside the procedure.
How can i apply this logic.
Please provide any explain.
Regards,
AgrawalV
Message was edited by:
AgrawalVExample: Re: How to make a dynamic function call from within a package procedure
-
How to call a procedure from within another procedure?
What's the syntax to call a procedure from within a procedure.
I have a procedure z(user_id IN number, ....)
then
procedure a (user_id IN number, ....)
procedure b (user_id IN number, ....)
procedure c (user_id IN number, ....)
I want to call procedure a, b, c from inside procedure z.
How would I do that?Same way :
SCOTT@db102 SQL> create or replace procedure a (p1 in varchar2) is
2 begin
3 dbms_output.put_line (p1);
4* end;
SCOTT@db102 SQL> /
Procedure created.
SCOTT@db102 SQL> create or replace procedure z (par1 in number) is
2 begin
3 if par1 != 0 then
4 a ('This is proc a');
5 end if;
6* end;
SCOTT@db102 SQL> /
Procedure created.
SCOTT@db102 SQL> set serveroutput on
SCOTT@db102 SQL> exec z (1);
This is proc a
PL/SQL procedure successfully completed.
SCOTT@db102 SQL> -
How to call another procedure???
I create a procedure, and inside this procedure there are others procedures in xxx.sql (vi file)...
how to call these procedutes???
Urgent...
ThanksIf I understood well u have two sql files, say x.sql and y.sql (u used vi to write them).
Suppose u want to call y.sql in a certain row of the x.sql file. Then just put inside the x.sql file.
@y
If you have a PL/sql procedure inside your sql file, maybe it's different.
I hav't tried all of the possibilities, but if the procedure u want to call is a stored one, just call its name.
I hope it helps.
Bye Dino -
Psql procedure to call 2 procedures(one by one)
Hi All,
I am very new to plsql, great if any1 could help me in getting to the right track
my problem is , I need to write a stored procedure which is going to call another stored procedure(lets call this first stored proc as procedure_A) and I have to make sure that it gets completed and then only I call the next stored procedure(lets call it procedure_B) and so on .. I have to do this for 26 stored procedures.
PS: I need to make sure when 1st procedure gets going, it needs to finish and then only the next procedure should start and so on for the 26 stored procedures that I need to call, I hope I am clear with my problem, please help me.
Regards
RahulI cannot log from the sys/system user, could you please tell me which privilege should I need.
Regards
Rahul
PS: I have these many privilege's , got it from -> select * from session_privs; :
ALTER SYSTEM
AUDIT SYSTEM
CREATE SESSION
ALTER SESSION
RESTRICTED SESSION
CREATE TABLESPACE
ALTER TABLESPACE
MANAGE TABLESPACE
DROP TABLESPACE
UNLIMITED TABLESPACE
CREATE USER
BECOME USER
ALTER USER
DROP USER
CREATE ROLLBACK SEGMENT
ALTER ROLLBACK SEGMENT
DROP ROLLBACK SEGMENT
CREATE TABLE
CREATE ANY TABLE
ALTER ANY TABLE
BACKUP ANY TABLE
DROP ANY TABLE
LOCK ANY TABLE
COMMENT ANY TABLE
SELECT ANY TABLE
INSERT ANY TABLE
UPDATE ANY TABLE
DELETE ANY TABLE
CREATE CLUSTER
CREATE ANY CLUSTER
ALTER ANY CLUSTER
DROP ANY CLUSTER
CREATE ANY INDEX
ALTER ANY INDEX
DROP ANY INDEX
CREATE SYNONYM
CREATE ANY SYNONYM
DROP ANY SYNONYM
CREATE PUBLIC SYNONYM
DROP PUBLIC SYNONYM
CREATE VIEW
CREATE ANY VIEW
DROP ANY VIEW
CREATE SEQUENCE
CREATE ANY SEQUENCE
ALTER ANY SEQUENCE
DROP ANY SEQUENCE
SELECT ANY SEQUENCE
CREATE DATABASE LINK
CREATE PUBLIC DATABASE LINK
DROP PUBLIC DATABASE LINK
CREATE ROLE
DROP ANY ROLE
GRANT ANY ROLE
ALTER ANY ROLE
AUDIT ANY
ALTER DATABASE
FORCE TRANSACTION
FORCE ANY TRANSACTION
CREATE PROCEDURE
CREATE ANY PROCEDURE
ALTER ANY PROCEDURE
DROP ANY PROCEDURE
EXECUTE ANY PROCEDURE
CREATE TRIGGER
CREATE ANY TRIGGER
ALTER ANY TRIGGER
DROP ANY TRIGGER
CREATE PROFILE
ALTER PROFILE
DROP PROFILE
ALTER RESOURCE COST
ANALYZE ANY
GRANT ANY PRIVILEGE
CREATE MATERIALIZED VIEW
CREATE ANY MATERIALIZED VIEW
ALTER ANY MATERIALIZED VIEW
DROP ANY MATERIALIZED VIEW
CREATE ANY DIRECTORY
DROP ANY DIRECTORY
CREATE TYPE
CREATE ANY TYPE
ALTER ANY TYPE
DROP ANY TYPE
EXECUTE ANY TYPE
UNDER ANY TYPE
CREATE LIBRARY
CREATE ANY LIBRARY
ALTER ANY LIBRARY
DROP ANY LIBRARY
EXECUTE ANY LIBRARY
CREATE OPERATOR
CREATE ANY OPERATOR
DROP ANY OPERATOR
EXECUTE ANY OPERATOR
CREATE INDEXTYPE
CREATE ANY INDEXTYPE
ALTER ANY INDEXTYPE
DROP ANY INDEXTYPE
UNDER ANY VIEW
QUERY REWRITE
GLOBAL QUERY REWRITE
EXECUTE ANY INDEXTYPE
UNDER ANY TABLE
CREATE DIMENSION
CREATE ANY DIMENSION
ALTER ANY DIMENSION
DROP ANY DIMENSION
MANAGE ANY QUEUE
ENQUEUE ANY QUEUE
DEQUEUE ANY QUEUE
CREATE ANY CONTEXT
DROP ANY CONTEXT
CREATE ANY OUTLINE
ALTER ANY OUTLINE
DROP ANY OUTLINE
ADMINISTER RESOURCE MANAGER
ADMINISTER DATABASE TRIGGER
MERGE ANY VIEW
ON COMMIT REFRESH
RESUMABLE
SELECT ANY DICTIONARY
DEBUG CONNECT SESSION
DEBUG ANY PROCEDURE
FLASHBACK ANY TABLE
GRANT ANY OBJECT PRIVILEGE
CREATE EVALUATION CONTEXT
CREATE ANY EVALUATION CONTEXT
ALTER ANY EVALUATION CONTEXT
DROP ANY EVALUATION CONTEXT
EXECUTE ANY EVALUATION CONTEXT
CREATE RULE SET
CREATE ANY RULE SET
ALTER ANY RULE SET
DROP ANY RULE SET
EXECUTE ANY RULE SET
EXPORT FULL DATABASE
IMPORT FULL DATABASE
CREATE RULE
CREATE ANY RULE
ALTER ANY RULE
DROP ANY RULE
EXECUTE ANY RULE
ANALYZE ANY DICTIONARY
ADVISOR
CREATE JOB
CREATE ANY JOB
EXECUTE ANY PROGRAM
EXECUTE ANY CLASS
MANAGE SCHEDULER
SELECT ANY TRANSACTION
DROP ANY SQL PROFILE
ALTER ANY SQL PROFILE
ADMINISTER SQL TUNING SET
ADMINISTER ANY SQL TUNING SET
CREATE ANY SQL PROFILE
MANAGE FILE GROUP
MANAGE ANY FILE GROUP
READ ANY FILE GROUP
CHANGE NOTIFICATION
CREATE EXTERNAL JOB
Edited by: Mac_Freak_Rahul on Sep 9, 2011 4:40 AM -
Executing a procedure automatically after another procedure
Hi , I have a procedure X , I want a procedure Y to run right after procedure X is executed ...Is there some kind of database trigger or scheduling to do that?
ThanksAlthough there is often too much reticence about using AQ, I'm not sure I agree about it being the most appropriate or simplest solution here.
Oasis,
As I understand it, you've got a form calling a procedure X.
When procedure X finishes you want to call procedure Y.
You're not bothered about that call to procedure Y happening in the same transaction.
This seems a classic case for using DBMS_JOB to do some asynchronous work
And I mean DBMS_JOB and not DBMS_SCHEDULER from 10g onwards because when you can't create a job with DBMS_SCHEDULER as part of a wider transaction, i.e. the creating of a job via DBMS SCHEDULER does an implicit commit.
In which case, you would make a change something like
PROCEDURE x
l_job BINARY_INTEGER;
BEGIN
DBMS_JOB.SUBMIT
(job => l_job,
what => 'begin y; commit; end;');
END;Your user will need CREATE JOB privilege.
Edited by: DomBrooks on Jan 21, 2010 2:45 PM -
Passing a html array to a procedure then to another procedure
Hi Sir,
Passing an array from html to procedure can be done by having html form elements with same name and a IN parameter in procedure that uses OWA_UTIL.IDENT_ARR as the datatype.
What if from the receiving procedure A, I want to pass the array further to another procedure B:
1)How should I write the statement to pass?
2)How should I declare the parameter in procedure B?
Please advise.
Thanks.Hi Sir,
Passing an array from html to procedure can be done by having html form elements with same name and a IN parameter in procedure that uses OWA_UTIL.IDENT_ARR as the datatype.
What if from the receiving procedure A, I want to pass the array further to another procedure B:
1)How should I write the statement to pass?
2)How should I declare the parameter in procedure B?
Please advise.
Thanks. -
PL/SQL precedure calling another procedure
Can someone provide an example of a procedure calling a second procedure. I have two procedures which work fine when called via exec from sql*plus, but when the first trys to call the second, I start getting errors. The first execute immediate runs, but errors out, the second execute immediate doesn't compile.
Thanks
HeaderProc
is
cursor h is
select * from header where val2 <> 'Y';
--func_call varchar2(100);
begin
dbms_output.put_line('Starting HeaderProc');
for current_header in h
loop
dbms_output.put_line('"' | | current_header.val1 | | '","' | | current_header.val2 | | '"');
-- EXECUTE IMMEDIATE 'exec user.childproc(10)';
--EXECUTE IMMEDIATE 'exec childproc(' | | 'current_header'.'key' | | ')';
end loop;
end;
ChildProc
(p_key IN number) is
cursor h is
select * from child where parent = p_key;
begin
dbms_output.put_line('Starting child');
for current_child in h
loop
dbms_output.put_line('"' | | current_child.cval1 | | '","' | | current_child.cval2 | | '"');
end loop;
end;
nullTo call a proc from another proc simply call it by name from the first proc.
childproc(current_header.key) ;
null -
SSRS dataset throws error when another stored procedure is called inside dataset stored procedure
Hello;
I am using Report Build 3.0, I have a simple report which gets data using dataset which is created from a Stored Procedure. I have another stored procedure which updates the data in the table which is used for the report. I want to get the live data on report everytime
the report is run so that I call that stored procedure (sp_updatedata) inside my report dataset stored procedure and here where my report fails as it throws error while creating dataset.
Here is sample:
sp_updatedata (this only returns "Command(s) completed successfully"
Create Proce sp_getReportData
As
Begin
Exec sp_updatedata -- I call it to update the data before it displays on the report
Select * from customers
End
If I remove this line it works.
Exec sp_updatedata -- I call it to update the data before it displays on the report
Thanks
Essa MughalHi MESSA,
According to your description, you create a dataset based on a stored procedure. In this procedure, it calls another procedure. Now it throws error when creating dataset. Right?
In Reporting Services, when creating dataset, all the query or stored procedure will be executed in SSMS. So if the procedure can be executed in SSMS, it supposed to be working in SSRS. However, it has a limitation in SSRS. In a dataset, it can only return
one result set.
In this scenario, I don't think it's the issue of calling other procedure inside of procedure. Because we tested in our local environment, it works fine. I guess the sp_updatedata returns a result set, and the "select * from customers" returns
another result set. This might be the reason cause the error.
Reference:
Query Design Tools in Report Designer SQL Server Data Tools (SSRS)
Reporting Services Query Designers
If you still have any question, please post the error message and the store procedure (sp_updatedata).
Best Regards,
Simon Hou
Maybe you are looking for
-
I bought a new MacBook just a week ago. I noticed that some videos stop playing after a few seconds when I try to watch them while surfing the internet! For example: I wanted to watch some of the one to one tutorials but I couldn't because ALL the vi
-
Printer driver for canon LBP-1120
Hi guys, I'm searching for a printer driver for the Canon Lasershot LBP-1120. I've been looking around a bit on google but haven't found anything very helpful. Thanks.
-
Hi, What is the default collation sequence of Publisher when doing sorting using the <?sort:...?> tag? My machines are configured "US English" as the locale, and my BI Publisher user (i.e. Administrator) is configured to run in "US English" as well.
-
Cs3/windows 7/ hp e9270f can't record sound
I loaded Premiere CS3 on my new computer and got message that I have an uncompatable sound card and I can't record sound, was running XP before on an HP computer with no problems. I also loaded encore CS3 and can't open it, it just freezes up, any id
-
How to merge data from SQL server to Oracle Database
Dear Expert, I am having problem with SQL server with Oracle. Problem: I want to merge table records from SQL server to Oracle Table. Which License free tool I can use to achive the goals. If I able to make a free DB link from Oracle to SQL server ,