Solved

Running a stored proc and returning a single value

Posted on 2013-02-01
13
266 Views
Last Modified: 2014-05-27
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
Comment
Question by:wilflife
  • 6
  • 2
  • 2
  • +1
13 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 300 total points
ID: 38843781
dual contains one column  called "DUMMY"

and the contents are a string 'X'

you can't put 'X' into an INTEGER type variable
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 38843790
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
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 300 total points
ID: 38843794
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
 

Author Comment

by:wilflife
ID: 38843802
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
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 100 total points
ID: 38843812
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
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

 
LVL 73

Expert Comment

by:sdstuber
ID: 38843815
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
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38843821
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
 
LVL 73

Expert Comment

by:sdstuber
ID: 38843829
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
 

Author Comment

by:wilflife
ID: 38843845
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
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 300 total points
ID: 38843855
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
 
LVL 20

Assisted Solution

by:flow01
flow01 earned 100 total points
ID: 38844344
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.

746 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

13 Experts available now in Live!

Get 1:1 Help Now