dbTalk Databases Forums  

Output parameter and dynamic sql

comp.databases.oracle comp.databases.oracle


Discuss Output parameter and dynamic sql in the comp.databases.oracle forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Robert Scheer
 
Posts: n/a

Default Output parameter and dynamic sql - 07-20-2004 , 08:57 PM






Hi.

I have a stored procedure on a Oracle 8.1.6 database that generates a
dynamic sql statement. This stored procedure has an output parameter
that needs to return a count from a view. I can generate and run the
sql successfuly, but when I try to return the count I get errors. I am
showing the relevant part of the procedure, since the rest of
concatenations are used to compose the sql.

CREATE OR REPLACE PROCEDURE getScoreEntries (user VARCHAR2, level
NUMBER, ret OUT NUMBER)
AS

sCall VARCHAR2(500);
nTotal NUMBER;

BEGIN
sCall := 'SELECT COUNT(*) INTO nTotal FROM vw_Scores WHERE
UserName=''' || user || '''';
sCall := sCall || --dynamic sql continues here...

EXECUTE IMMEDIATE sCall;
ret := nTotal;

END getTotalEntries;

Can anyone help me with this issue?

Thanks,
Robert Scheer

Reply With Quote
  #2  
Old   
Romeo Olympia
 
Posts: n/a

Default Re: Output parameter and dynamic sql - 07-21-2004 , 01:48 AM






A quick look of your code shows two things that need revising:
(1) You need to use the INTO clause of EXECUTE IMMEDIATE when you want
to get the results of a single-row query. In your case, the "count(*)"
value.
(2) Use bind arguments whenever possible (USING clause). That's the
"where username" condition for you.

So try something like this:

....
sCall := 'SELECT COUNT(*) FROM vw_Scores WHERE UserName = :username';

EXECUTE IMMEDIATE sCall INTO nTotal USING user;
....

Do read the PL/SQL guide for more detailed info. HTH.

rbscheer (AT) my-deja (DOT) com (Robert Scheer) wrote in message news:<cfd22ab6.0407201757.996c4ca (AT) posting (DOT) google.com>...
Quote:
Hi.

I have a stored procedure on a Oracle 8.1.6 database that generates a
dynamic sql statement. This stored procedure has an output parameter
that needs to return a count from a view. I can generate and run the
sql successfuly, but when I try to return the count I get errors. I am
showing the relevant part of the procedure, since the rest of
concatenations are used to compose the sql.

CREATE OR REPLACE PROCEDURE getScoreEntries (user VARCHAR2, level
NUMBER, ret OUT NUMBER)
AS

sCall VARCHAR2(500);
nTotal NUMBER;

BEGIN
sCall := 'SELECT COUNT(*) INTO nTotal FROM vw_Scores WHERE
UserName=''' || user || '''';
sCall := sCall || --dynamic sql continues here...

EXECUTE IMMEDIATE sCall;
ret := nTotal;

END getTotalEntries;

Can anyone help me with this issue?

Thanks,
Robert Scheer

Reply With Quote
  #3  
Old   
Romeo Olympia
 
Posts: n/a

Default Re: Output parameter and dynamic sql - 07-21-2004 , 01:50 AM



A quick look of your code shows two things that need revising:
(1) You need to use the INTO clause of EXECUTE IMMEDIATE when you want
to get the results of a single-row query. In your case, the "count(*)"
value.
(2) Use bind arguments whenever possible (USING clause). That's the
"where username" condition for you.

So try something like this:

....
sCall := 'SELECT COUNT(*) FROM vw_Scores WHERE UserName = :username';

EXECUTE IMMEDIATE sCall INTO nTotal USING user;
....

Do read the PL/SQL guide for more detailed info. HTH.

rbscheer (AT) my-deja (DOT) com (Robert Scheer) wrote in message news:<cfd22ab6.0407201757.996c4ca (AT) posting (DOT) google.com>...
Quote:
Hi.

I have a stored procedure on a Oracle 8.1.6 database that generates a
dynamic sql statement. This stored procedure has an output parameter
that needs to return a count from a view. I can generate and run the
sql successfuly, but when I try to return the count I get errors. I am
showing the relevant part of the procedure, since the rest of
concatenations are used to compose the sql.

CREATE OR REPLACE PROCEDURE getScoreEntries (user VARCHAR2, level
NUMBER, ret OUT NUMBER)
AS

sCall VARCHAR2(500);
nTotal NUMBER;

BEGIN
sCall := 'SELECT COUNT(*) INTO nTotal FROM vw_Scores WHERE
UserName=''' || user || '''';
sCall := sCall || --dynamic sql continues here...

EXECUTE IMMEDIATE sCall;
ret := nTotal;

END getTotalEntries;

Can anyone help me with this issue?

Thanks,
Robert Scheer

Reply With Quote
  #4  
Old   
sybrandb@yahoo.com
 
Posts: n/a

Default Re: Output parameter and dynamic sql - 07-21-2004 , 03:54 AM



rbscheer (AT) my-deja (DOT) com (Robert Scheer) wrote in message news:<cfd22ab6.0407201757.996c4ca (AT) posting (DOT) google.com>...
Quote:
Hi.

I have a stored procedure on a Oracle 8.1.6 database that generates a
dynamic sql statement. This stored procedure has an output parameter
that needs to return a count from a view. I can generate and run the
sql successfuly, but when I try to return the count I get errors. I am
showing the relevant part of the procedure, since the rest of
concatenations are used to compose the sql.

CREATE OR REPLACE PROCEDURE getScoreEntries (user VARCHAR2, level
NUMBER, ret OUT NUMBER)
AS

sCall VARCHAR2(500);
nTotal NUMBER;

BEGIN
sCall := 'SELECT COUNT(*) INTO nTotal FROM vw_Scores WHERE
UserName=''' || user || '''';
sCall := sCall || --dynamic sql continues here...

EXECUTE IMMEDIATE sCall;
ret := nTotal;

END getTotalEntries;

Can anyone help me with this issue?

Thanks,
Robert Scheer
Don't see an INTO clause after the execute immediate statement.

Other than that: do you think anyone here can guess what your
unspecified errors are?

Sybrand Bakker
Senior Oracle DBA


Reply With Quote
  #5  
Old   
Robert Scheer
 
Posts: n/a

Default Re: Output parameter and dynamic sql - 07-22-2004 , 09:03 AM



Hi Romeo,

thanks, it worked!

Robert Scheer


rolympia (AT) hotmail (DOT) com (Romeo Olympia) wrote in message news:<42fc55dc.0407202248.2b953649 (AT) posting (DOT) google.com>...
Quote:
A quick look of your code shows two things that need revising:
(1) You need to use the INTO clause of EXECUTE IMMEDIATE when you want
to get the results of a single-row query. In your case, the "count(*)"
value.
(2) Use bind arguments whenever possible (USING clause). That's the
"where username" condition for you.

So try something like this:

...
sCall := 'SELECT COUNT(*) FROM vw_Scores WHERE UserName = :username';

EXECUTE IMMEDIATE sCall INTO nTotal USING user;
...

Do read the PL/SQL guide for more detailed info. HTH.

rbscheer (AT) my-deja (DOT) com (Robert Scheer) wrote in message news:<cfd22ab6.0407201757.996c4ca (AT) posting (DOT) google.com>...
Hi.

I have a stored procedure on a Oracle 8.1.6 database that generates a
dynamic sql statement. This stored procedure has an output parameter
that needs to return a count from a view. I can generate and run the
sql successfuly, but when I try to return the count I get errors. I am
showing the relevant part of the procedure, since the rest of
concatenations are used to compose the sql.

CREATE OR REPLACE PROCEDURE getScoreEntries (user VARCHAR2, level
NUMBER, ret OUT NUMBER)
AS

sCall VARCHAR2(500);
nTotal NUMBER;

BEGIN
sCall := 'SELECT COUNT(*) INTO nTotal FROM vw_Scores WHERE
UserName=''' || user || '''';
sCall := sCall || --dynamic sql continues here...

EXECUTE IMMEDIATE sCall;
ret := nTotal;

END getTotalEntries;

Can anyone help me with this issue?

Thanks,
Robert Scheer

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.