Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call oracle stored procedure and function

Might be too simple question to ask, but I do need help.

I am creating a stored procedure in Oracle 10g, but I cannot call it. I am using SQL Developer to manage the database.

CREATE OR REPLACE
FUNCTION check_login 
  (username IN VARCHAR2, pwd IN VARCHAR2)
  RETURN VARCHAR2
IS
  isUserValid INTEGER;
BEGIN
  SELECT Count(*) INTO isUserValid
  FROM users
  WHERE Username = username AND PASS_WORD = pwd;
  return isUserValid;
END;

I have tried this also:

CREATE OR REPLACE
PROCEDURE check_login 
  (username IN VARCHAR2, pwd IN VARCHAR2, RESULT OUT INTEGER)
IS
  isUserValid INTEGER;
BEGIN
  SELECT Count(*) INTO isUserValid
  FROM users
  WHERE Username = username AND PASS_WORD = pwd;
  RESULT := isUserValid;
END;

Parsing both does not give any error message. I used following syntax to call them:

BEGIN 
  check_login('admin', 'admin'); 
END;

AND

EXECUTE check_login('admin', 'admin');

I get this error message....

PLS-00221: 'CHECK_LOGIN' is not a procedure or is undefined
PL/SQL: Statement ignored

The SELECT statement inside both works fine if run directly.

Am I doing something wrong?

like image 618
Kumar Kush Avatar asked Oct 12 '11 07:10

Kumar Kush


People also ask

Why can't we call stored procedure from function?

You cannot execute a stored procedure inside a function, because a function is not allowed to modify database state, and stored procedures are allowed to modify database state.

Can we call function in stored procedure in Oracle?

You can call a stored procedure inside a user-defined function. Consider this example: SQL> create table test_tab (tab_id number);

Can we call functions in stored procedure?

You can call a function from a stored procedure. You can call a function using a select statement. You cannot call a procedure using select statements.

Can we call function from stored procedure and vice versa?

You can call Functions can be from Procedure. But the vice-versa is not correct. As you can't call Procedures from a Function.


2 Answers

If you want to execute a function you have to collect the return value into a variable.

So you need to define a variable and execute function to return into the variable as below

and run it using the run Script option not the Run Statement option.

variable ret varchar2(20);

execute :ret:=check_login(dd,dd);

select :ret from dual

Or if you do it from plsql

declare  v_ret varchar2(100); 
begin

  v_ret:=check_login(a,b); 
end;
like image 71
josephj1989 Avatar answered Oct 10 '22 15:10

josephj1989


I find the easiest way to call a function is just selecting the function from dual eg-

select check_login('admin', 'admin') from dual;

Note the error message said "No procedure' :).

like image 41
Steve Ronaldson Ewing Avatar answered Oct 10 '22 16:10

Steve Ronaldson Ewing