 |
|
 |
|
Next: How do I run an Access query from a Sql Server Ag..
|
| Author |
Message |
External

Since: Oct 27, 2008 Posts: 2
|
(Msg. 1) Posted: Mon Oct 27, 2008 3:41 am
Post subject: Dynamic SQL to call a function with RETURN value? Archived from groups: comp>databases>oracle>misc (more info?)
|
|
|
Hi
Please can someone help me out. I have done quite a bit of searching
and 'trial and error' without much success.
I have simplified the problem for readability, but a solution to this
will allow me to solve my problem.
PROBLEM:
I have a table BATCH_FUNCTION which will contain records with various
existing DB functions, with parameters- (Return Type BOOLEAN).
In an Anonymous Block (lets say for example), if I had a list of the
functions to call in a cursor (select function_name from
batch_function), how would I call these functions with parameters, as
well as be able to test the Return value in my Anonymous block?
I presume that Dynamic SQL is the way to go, but I have not been able
to get it right.
-----------------------------------------------------------------------------------------------------------
EXAMPLE TABLE: BATCH_FUNCTION
-----------------------------------------------------------------------------------------------------------
func_id function_name
----------- ---------------------
111 test_positive(1)
-----------------------------------------------------------------------------------------------------------
EXAMPLE FUNCTION
-----------------------------------------------------------------------------------------------------------
FUNCTION test_positive(i_number IN NUMBER) RETURN BOOLEAN AS
BEGIN
if(i_number >0) THEN
RETURN true;
else
RETURN false;
end if;
END test_positive; >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
External

Since: Dec 15, 2007 Posts: 172
|
(Msg. 2) Posted: Mon Oct 27, 2008 5:25 pm
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
wrote:
>I presume that Dynamic SQL is the way to go, but I have not been able
>to get it right.
Dynamic SQL is the correct method to develop an application which has
'DISASTER' inscribed all over it.
Your description is very vague, and from what I get from it you seem
to have re-invented a feature which already exists in PL/SQL, albeit
in packages, called 'Overloading'
You would need to present much more detail (as well as a database
version) to find out why you have a desire to end on the electrical
chair or to make the life of your potential customers miserable.
--
Sybrand Bakker
Senior Oracle DBA >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
External

Since: Jan 30, 2008 Posts: 6
|
(Msg. 3) Posted: Tue Oct 28, 2008 6:00 am
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Oct 27, 4:24 pm, sybra... DeleteThis @hccnet.nl wrote:
> On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
> wrote:
>
> >I presume that Dynamic SQL is the way to go, but I have not been able
> >to get it right.
>
> Dynamic SQL is the correct method to develop an application which has
> 'DISASTER' inscribed all over it.
> Your description is very vague, and from what I get from it you seem
> to have re-invented a feature which already exists in PL/SQL, albeit
> in packages, called 'Overloading'
> You would need to present much more detail (as well as a database
> version) to find out why you have a desire to end on the electrical
> chair or to make the life of your potential customers miserable.
>
> --
> Sybrand Bakker
> Senior Oracle DBA
>>Dynamic SQL is the correct method to develop an application which has
'DISASTER' inscribed all over it. <<
Could you point to Oracle documentation, where it lists Dynamic SQL
limits?
Thx
Thomas >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
External

Since: Dec 20, 2007 Posts: 294
|
(Msg. 4) Posted: Tue Oct 28, 2008 6:58 am
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Oct 27, 4:24 pm, sybra....DeleteThis@hccnet.nl wrote:
> On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
> wrote:
>
> >I presume that Dynamic SQL is the way to go, but I have not been able
> >to get it right.
>
> Dynamic SQL is the correct method to develop an application which has
> 'DISASTER' inscribed all over it.
> Your description is very vague, and from what I get from it you seem
> to have re-invented a feature which already exists in PL/SQL, albeit
> in packages, called 'Overloading'
> You would need to present much more detail (as well as a database
> version) to find out why you have a desire to end on the electrical
> chair or to make the life of your potential customers miserable.
>
> --
> Sybrand Bakker
> Senior Oracle DBA
I also recommend avoiding use of dynamic SQL unless you cannot solve
your business problem any other way.
If you really need dynamic SQL then look in the PL/SQL manual at the
execute immediate statement and for more complex SQL look in the PL?
SQL Packages and Type Reference manual at the entry for package
dbms_sql.
Packages and overloading procedure/function calls as Sybrand mentioned
is a potential alternate approach. You might also be able to make use
of weakly typed reference cursors.
HTH -- Mark D Powell -- >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
|
gym dot scuba dot kennedy
|
External

Since: Jun 08, 2008 Posts: 77
|
(Msg. 5) Posted: Tue Oct 28, 2008 11:31 am
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Thomas Olszewicki" wrote in message
On Oct 27, 4:24 pm, sybra....DeleteThis@hccnet.nl wrote:
> On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
> wrote:
>
> >I presume that Dynamic SQL is the way to go, but I have not been able
> >to get it right.
>
> Dynamic SQL is the correct method to develop an application which has
> 'DISASTER' inscribed all over it.
> Your description is very vague, and from what I get from it you seem
> to have re-invented a feature which already exists in PL/SQL, albeit
> in packages, called 'Overloading'
> You would need to present much more detail (as well as a database
> version) to find out why you have a desire to end on the electrical
> chair or to make the life of your potential customers miserable.
>
> --
> Sybrand Bakker
> Senior Oracle DBA
>>Dynamic SQL is the correct method to develop an application which has
'DISASTER' inscribed all over it. <<
Could you point to Oracle documentation, where it lists Dynamic SQL
limits?
Thx
Thomas
If you read the performance guide you will see you are forcing hard parses
all over the place with this approuch. This severly limits scalability. In
addition, dynamic sql is very difficult to debug.
Jim >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
External

Since: Oct 27, 2008 Posts: 2
|
(Msg. 6) Posted: Thu Oct 30, 2008 1:11 am
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Oct 28, 4:49 pm, "gym dot scuba dot kennedy at gmail"
wrote:
> "Thomas Olszewicki" wrote in message
>
>
> On Oct 27, 4:24 pm, sybra... DeleteThis @hccnet.nl wrote:
>
>
>
> > On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
> > wrote:
>
> > >I presume that Dynamic SQL is the way to go, but I have not been able
> > >to get it right.
>
> > Dynamic SQL is the correct method to develop an application which has
> > 'DISASTER' inscribed all over it.
> > Your description is very vague, and from what I get from it you seem
> > to have re-invented a feature which already exists in PL/SQL, albeit
> > in packages, called 'Overloading'
> > You would need to present much more detail (as well as a database
> > version) to find out why you have a desire to end on the electrical
> > chair or to make the life of your potential customers miserable.
>
> > --
> > Sybrand Bakker
> > Senior Oracle DBA
> >>Dynamic SQL is the correct method to develop an application which has
>
> 'DISASTER' inscribed all over it. <<
>
> Could you point to Oracle documentation, where it lists Dynamic SQL
> limits?
> Thx
> Thomas
> If you read the performance guide you will see you are forcing hard parses
> all over the place with this approuch. This severly limits scalability.. In
> addition, dynamic sql is very difficult to debug.
> Jim
Thanks everyone for your response. - PROBLEM SOLVED
I have slightly changed Thomas suggestion to suit my requirement.
This is what I have done in the event somebody comes across the same
requirement in the future:
I have used comments to best try describe what I will ultimately do,
but tested a sample function and all is good.
For those who have warned me against using dynamic SQL for performance
and scalability issues, thaks for the warning - NOTED.
I have spelled these out to the client as risks, and not the prefered
approach.
However, I have to work with what I have, and can not change other
areas of the system due to project mandate.
DECLARE
/*
* Return Code will be used to determine whether program completed
successfullly, or encountered error
* All batch functions return true or false indication function
status
*/
nReturnCode NUMBER;
/*
* str_func will be set in a cursor Loop using function calls
obtained from a job setup table
*/
str_func VARCHAR2(255) := 'test_func(-2)'; --will get this function
and parameters from a job_setup table using a cursor
cStmt VARCHAR2(600);
BEGIN
-- LOOP THROUGH cursor (all functions)
-- set str_func = get function from cursor
-- execute dynamic sql function call
cStmt := 'BEGIN DECLARE bool_rtn BOOLEAN ; ';
cStmt := cStmt || 'BEGIN bool_rtn := '||
str_func||'; ';
cStmt := cStmt || 'IF bool_rtn THEN :o_Rtn :=
0; ';
cStmt := cStmt || 'ELSE :o_Rtn := -1; END IF;
';
cStmt := cStmt || 'END;';
cStmt := cStmt || 'END;';
EXECUTE IMMEDIATE cStmt USING OUT nReturnCode;
-- test execution status of function
IF(nReturnCode=0) THEN
--do successfull completion code
ELSE
--do failed completion code
--will exit loop and write to neccessary error tables.
END IF;
END; >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
External

Since: Dec 15, 2008 Posts: 1
|
(Msg. 7) Posted: Mon Dec 15, 2008 6:21 am
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Oct 30, 3:11 am, mwmann wrote:
> On Oct 28, 4:49 pm, "gym dot scuba dot kennedy at gmail"
>
>
>
>
>
> wrote:
> > "Thomas Olszewicki" wrote in message
>
> >
> > On Oct 27, 4:24 pm, sybra....DeleteThis@hccnet.nl wrote:
>
> > > On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
> > > wrote:
>
> > > >I presume that Dynamic SQL is the way to go, but I have not been able
> > > >to get it right.
>
> > > Dynamic SQL is the correct method to develop an application which has
> > > 'DISASTER' inscribed all over it.
> > > Your description is very vague, and from what I get from it you seem
> > > to have re-invented a feature which already exists in PL/SQL, albeit
> > > in packages, called 'Overloading'
> > > You would need to present much more detail (as well as a database
> > > version) to find out why you have a desire to end on the electrical
> > > chair or to make the life of your potential customers miserable.
>
> > > --
> > > Sybrand Bakker
> > > Senior Oracle DBA
> > >>Dynamic SQL is the correct method to develop an application which has
>
> > 'DISASTER' inscribed all over it. <<
>
> > Could you point to Oracle documentation, where it lists Dynamic SQL
> > limits?
> > Thx
> > Thomas
> > If you read the performance guide you will see you are forcing hard parses
> > all over the place with this approuch. This severly limits scalability. In
> > addition, dynamic sql is very difficult to debug.
> > Jim
>
> Thanks everyone for your response. - PROBLEM SOLVED
> I have slightly changed Thomas suggestion to suit my requirement.
>
> This is what I have done in the event somebody comes across the same
> requirement in the future:
> I have used comments to best try describe what I will ultimately do,
> but tested a sample function and all is good.
>
> For those who have warned me against using dynamic SQL for performance
> and scalability issues, thaks for the warning - NOTED.
> I have spelled these out to the client as risks, and not the prefered
> approach.
> However, I have to work with what I have, and can not change other
> areas of the system due to project mandate.
>
> DECLARE
> /*
> * Return Code will be used to determine whether program completed
> successfullly, or encountered error
> * All batch functions return true or false indication function
> status
> */
> nReturnCode NUMBER;
> /*
> * str_func will be set in a cursor Loop using function calls
> obtained from a job setup table
> */
> str_func VARCHAR2(255) := 'test_func(-2)'; --will get this function
> and parameters from a job_setup table using a cursor
> cStmt VARCHAR2(600);
>
> BEGIN
>
> -- LOOP THROUGH cursor (all functions)
> -- set str_func = get function from cursor
> -- execute dynamic sql function call
> cStmt := 'BEGIN DECLARE bool_rtn BOOLEAN ; ';
> cStmt := cStmt || 'BEGIN bool_rtn := '||
> str_func||'; ';
> cStmt := cStmt || 'IF bool_rtn THEN :o_Rtn :=
> 0; ';
> cStmt := cStmt || 'ELSE :o_Rtn := -1; END IF;
> ';
> cStmt := cStmt || 'END;';
> cStmt := cStmt || 'END;';
> EXECUTE IMMEDIATE cStmt USING OUT nReturnCode;
>
> -- test execution status of function
> IF(nReturnCode=0) THEN
> --do successfull completion code
> ELSE
> --do failed completion code
> --will exit loop and write to neccessary error tables.
> END IF;
> END;- Hide quoted text -
>
> - Show quoted text -
I have developed a user defined data type called AnonymousFunction
that might also do the trick for you. Check it out here:
http://radio.weblogs.com/0137094/2008/11/09.html >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
External

Since: Dec 20, 2007 Posts: 422
|
(Msg. 8) Posted: Mon Dec 15, 2008 2:32 pm
Post subject: Re: Dynamic SQL to call a function with RETURN value? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Dec 15, 6:21 am, jas... RemoveThis @aol.com wrote:
> On Oct 30, 3:11 am, mwmann wrote:
>
>
>
>
>
> > On Oct 28, 4:49 pm, "gym dot scuba dot kennedy at gmail"
>
> > wrote:
> > > "Thomas Olszewicki" wrote in message
>
> > >
> > > On Oct 27, 4:24 pm, sybra... RemoveThis @hccnet.nl wrote:
>
> > > > On Mon, 27 Oct 2008 03:41:00 -0700 (PDT), mwmann
> > > > wrote:
>
> > > > >I presume that Dynamic SQL is the way to go, but I have not been able
> > > > >to get it right.
>
> > > > Dynamic SQL is the correct method to develop an application which has
> > > > 'DISASTER' inscribed all over it.
> > > > Your description is very vague, and from what I get from it you seem
> > > > to have re-invented a feature which already exists in PL/SQL, albeit
> > > > in packages, called 'Overloading'
> > > > You would need to present much more detail (as well as a database
> > > > version) to find out why you have a desire to end on the electrical
> > > > chair or to make the life of your potential customers miserable.
>
> > > > --
> > > > Sybrand Bakker
> > > > Senior Oracle DBA
> > > >>Dynamic SQL is the correct method to develop an application which has
>
> > > 'DISASTER' inscribed all over it. <<
>
> > > Could you point to Oracle documentation, where it lists Dynamic SQL
> > > limits?
> > > Thx
> > > Thomas
> > > If you read the performance guide you will see you are forcing hard parses
> > > all over the place with this approuch. This severly limits scalability. In
> > > addition, dynamic sql is very difficult to debug.
> > > Jim
>
> > Thanks everyone for your response. - PROBLEM SOLVED
> > I have slightly changed Thomas suggestion to suit my requirement.
>
> > This is what I have done in the event somebody comes across the same
> > requirement in the future:
> > I have used comments to best try describe what I will ultimately do,
> > but tested a sample function and all is good.
>
> > For those who have warned me against using dynamic SQL for performance
> > and scalability issues, thaks for the warning - NOTED.
> > I have spelled these out to the client as risks, and not the prefered
> > approach.
> > However, I have to work with what I have, and can not change other
> > areas of the system due to project mandate.
>
> > DECLARE
> > /*
> > * Return Code will be used to determine whether program completed
> > successfullly, or encountered error
> > * All batch functions return true or false indication function
> > status
> > */
> > nReturnCode NUMBER;
> > /*
> > * str_func will be set in a cursor Loop using function calls
> > obtained from a job setup table
> > */
> > str_func VARCHAR2(255) := 'test_func(-2)'; --will get this function
> > and parameters from a job_setup table using a cursor
> > cStmt VARCHAR2(600);
>
> > BEGIN
>
> > -- LOOP THROUGH cursor (all functions)
> > -- set str_func = get function from cursor
> > -- execute dynamic sql function call
> > cStmt := 'BEGIN DECLARE bool_rtn BOOLEAN ; ';
> > cStmt := cStmt || 'BEGIN bool_rtn := '||
> > str_func||'; ';
> > cStmt := cStmt || 'IF bool_rtn THEN :o_Rtn :=
> > 0; ';
> > cStmt := cStmt || 'ELSE :o_Rtn := -1; END IF;
> > ';
> > cStmt := cStmt || 'END;';
> > cStmt := cStmt || 'END;';
> > EXECUTE IMMEDIATE cStmt USING OUT nReturnCode;
>
> > -- test execution status of function
> > IF(nReturnCode=0) THEN
> > --do successfull completion code
> > ELSE
> > --do failed completion code
> > --will exit loop and write to neccessary error tables.
> > END IF;
> > END;- Hide quoted text -
>
> > - Show quoted text -
>
> I have developed a user defined data type called AnonymousFunction
> that might also do the trick for you. Check it out here:
>
> http://radio.weblogs.com/0137094/2008/11/09.html
That does seem pretty cool, though the previous comments about
performance and debugging certainly apply.
But the really funny thing was the ads that google deemed appropriate
- for dump trucks.
jg
--
@home.com is bogus.
http://www.telegraph.co.uk/news/newstopics/howaboutthat/3743289/Zzz-ma...What-ha >> Stay informed about: Dynamic SQL to call a function with RETURN value? |
|
| Back to top |
|
 |  |
| Related Topics: | if I call a plsql is there a commit? - Hi, excuse me for the simplicity of the question but I'm a beginner.. I have seen somethings similar in this forum. If i have a procedure plsql that calls an other one like this: CREATE OR REPLACE PROCEDURE p1 as begin p2(); p3(); .... end; After p...
Call Sqlldr from DOS Batch. - Hi, I'm getting error when I try to call Sqlldr from a batch file: My batch file look like this: SQLLDR PARFILE=C:\TMP\mypar.par CONTROL=C:\TMP\mycontrol.txt ........ Someone told to set Oracle Var. but anyway the problem persist. This the error..
Dynamic spool file name - I need to create files based on data returned from a cursor. I know I can use a variable and the column statement to dynamically set the spool filename in a script. However, I am using cursors in a block and that doesn't seem to work inside the block (I...
Dynamic "In" w a Cursor in a Package - I'm pretty new to Oracle and PL-SQL, I have a SQL Server/T-SQL background. I've googled for this and found some examples, but no exact luck yet. We're on version 9. We have a need, in a package, to execute a Select statement using an In List that wil...
Stored procedure call working on one server but not on ano.. - I have 2 servers the old one is running 9.2.0.1.0 on Red Hat Linux I login as STEST, both scripts below work this one: DECLARE a VARCHAR(100); begin STEST.GETNEXT_ID('ABC',1,a); end; as well as this one; DECLARE a VARCHAR(100); begin.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|