Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Running a stored proc and returning a single value

CREATE OR REPLACE PROCEDURE Check_Run (
  p_retVal OUT INTEGER
)
AS
BEGIN
   select * into p_retVal from dual;
END Check_Run;
exec Check_Run;

I get an error when running this.

I know simple.
0
wilflife
Asked:
wilflife
  • 6
  • 2
  • 2
  • +1
5 Solutions
 
sdstuberCommented:
dual contains one column  called "DUMMY"

and the contents are a string 'X'

you can't put 'X' into an INTEGER type variable
0
 
sdstuberCommented:
either change p_retVal to some string data type (like VARCHAR2)

or,  use some select  other than * from dual.

incidentally, using * is bad practice if you aren't putting the results into a record type since * means "all columns"  and therefore implies it could be more than one
0
 
sdstuberCommented:
also note,  select from dual  is not the proper way to assign a value to a pl/sql variable

use :=  instead

for example

CREATE OR REPLACE PROCEDURE Check_Run (
  p_retVal OUT INTEGER
)
AS
BEGIN
     p_retVal := 1234;
END Check_Run;
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
wilflifeAuthor Commented:
exec Check_Run;

Gives me this error even if i change it to
Error starting at line 18 in command:
exec Check_Run
Error report:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'CHECK_RUN'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

   select 1 into p_retVal from dual;
0
 
Steve WalesSenior Database AdministratorCommented:
You'd need to call it like this:

set serveroutput on
declare
myvar varchar2
begin
Check_Run(myvar);
dbms_output.put_line(myvar);
end;
/

Open in new window


You've specified a parameter in the proc definition and then you aren't calling the proc with that parameter.
0
 
sdstuberCommented:
exec Check_Run;


this doesn't make sense to do this,  
the error message explains why

  "wrong number or types of arguments in call to 'CHECK_RUN' "

your procedure returns a value but you don't have a parameter in your call to hold that  value

declare
   v_my_int integer;
begin
    check_run(v_my_int);
    dbms_output.put_line(v_my_int);
end;
0
 
Steve WalesSenior Database AdministratorCommented:
Further to my response, you'd need to have changed your proc to have the parameter be a varchar2, or if you're doing something like "select 1 from dual" then you'd need to change the data type of the variable when you call the proc to be an integer.
0
 
sdstuberCommented:
as noted above


select 1 into p_retVal from dual;


is the wrong way to do value assignment.
Using sql forces extra work called a "context switch"


p_retVal := 1;

is the right way
0
 
wilflifeAuthor Commented:
The reason i am doing it with the select 1 from dual is later i will put a more complex sql statement i have designed in.

How do i get this to return?

Now runs but just says anonymous block completed and nothing in dbms output?

I ran it like this:

declare
   v_my_int integer;
begin
    check_run(v_my_int);
    dbms_output.put_line(v_my_int);
end;

Thanks.
0
 
sdstuberCommented:
are you using sql*plus?

if so


set serveroutput on

then run the block to see the dbms_output results


if you are using some other tool there will probably be a window or tab available to view dbms_output results and you'll have to enable it there.  How/where you do that will be dependent on your specific tool
0
 
flow01Commented:
And if there will always be a single return value, you can change it to a function.
Unless DML (insert,delete,update) is needed to determine the return value you can call the function from sql.
The return value in this example wil change depending on the moment when the function is called.


SQL> CREATE OR REPLACE FUNCTION  Check_Run
  2  RETURN INTEGER
  3  AS
  4   v_retVal pls_integer;
  5  BEGIN
  6     select mod(to_number(to_char(sysdate,'ss')),2)  into v_retVal from dual;
  7     return v_retVal;
  8  END Check_Run;
  9  /



SQL> select check_run from dual;

 CHECK_RUN
----------
         1

SQL> /

 CHECK_RUN
----------
         1

SQL> /

 CHECK_RUN
----------
         1

SQL> /

 CHECK_RUN
----------
         1

SQL> /

 CHECK_RUN
----------
         0

SQL> /

 CHECK_RUN
----------
         0

SQL> /

 CHECK_RUN
----------
         1
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now