Solved

Running a stored proc and returning a single value

Posted on 2013-02-01
13
276 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 74

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 74

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 74

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
LVL 74

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 74

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 74

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to recover a database from a user managed backup

820 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