Cannot call oracle stored procedure and function - oracle

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?

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;

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' :).

Related

Stored Procedure Related

I am creating a stored procedure in oracle that is selecting records from login table -
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
begin
select * from login;
end;
Whenever I am going to compile this it shows an error:
PLS-00428: an INTO clause is expected in this SELECT statement
What does this mean? I do not understand this.
You have to store the result of your SELECT statement into a variable, you can use sys_refcursor to display the result.
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number, result_out OUT SYS_REFCURSOR)
as
l_query varchar2(1000) := Null;
begin
l_query := 'select * from login';
open result_out
for l_query;
end;
above code will give you the output
PLS-00428: an INTO clause is expected in this SELECT statement
That means you need an INTO close when you issue bare SELECT from PL/SQL.
:D
More constructively: where do you think the result of your select would go in that code fragment?
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
begin
select * from login;
end;
You have to retrieve it somehow in order to be processed by your PL/SQL code. Assuming you have several rows to collect, you should use BULK COLLECT INTO:
create or replace procedure login_info
(username IN varchar2, password IN varchar2, result OUT number)
as
type my_tbl_type IS TABLE OF login%ROWTYPE;
my_tbl my_tbl_type;
begin
select * BULK COLLECT INTO my_tbl from login;
-- do whatever you
-- need here
-- on `my_tbl`.
end;
As a final note, maybe are you looking for an explicit CURSOR instead? You should definitively take a look at PL/SQL 101: Working with Cursors. This is an interesting discussion both about SELECT ... INTO ... and CURSOR manipulation.

Oracle dynamic run error

I created a procedure with dynamic sql,but cannot run it successfully.
create or replace procedure getdata(string par1, results out cursor)
as
declare sqlBase varchar2(100);
begin
sqlBase := 'open '||results|| ' for select * from studetns';
end;
After running, the following error message pops up:
PLS-00306, wrong number or types of arguments in call to '||'
I just need to filter data by some parameters ,but some parameters may be null or empty,
so I need to filter dynamic. like if(par1 is not null) then ........
so here I need to use dynamic sql. in C# programe, use cursor to return result.
like here ,I use cursor type to open select statements.
but in sql editor, I get right sql statement.
Can Somebody help me with this?
Your syntax is a little bit wrong. Try with this:
create or replace procedure getdata(par1 varchar2, par2 varchar2, results out sys_refcursor)
as
begin
open results for
select *
from students
where name = nvl(par1, name)
and surname = nvl(par2, surname);
end;
Why do you need parameter par1? Better to use PL/SQL type varchar2, not string. They work the same, but varchar2 is a base data type, while string is a subtype of it.
Depending on what you want to achieve, I would try something like that:
create or replace procedure getdata(par1 varchar2, results out sys_refcursor)
as
sqlBase varchar2(100);
begin
sqlBase := 'begin open :X for select * from students;end;';
execute immediate sqlbase USING IN OUT results;
end;
You can't concatenate a cursor into a string as you tried it, that's where your error came from.
But if you could clearify your question we could help you better.

Unable to execute oracle procedure in PLSQL Developer

I am having sample Procedure as Below
CREATE OR REPLACE PROCEDURE proc_Table1
BEGIN
SELECT 'Sample UserName' AS UserName
FROM Dual;
END;
Now I want to run this Proc in PLSQL developer.I tried the below but its generating error
begin
proc_Table1;
end;
Its Displaying
Thanks for the Help.
Any Idea why this Happens.
When you created the procedure, you should have been alerted to the fact that you had syntax errors. If you're going to run a SELECT statement, you need to do something with the results-- either populate a local variable or open a cursor or something else. The code you've posted is also missing the IS/AS keyword
CREATE OR REPLACE PROCEDURE proc_Table1
AS
l_username VARCHAR2(30);
BEGIN
SELECT 'Sample UserName' AS UserName
INTO l_username
FROM Dual;
END;
will be syntactically valid. It doesn't appear, however, to be particularly useful-- you procedure isn't modifying the database and has no way to communicate with the caller so it isn't doing anything meaningful. As a general principal, you would also generally want to use a simple PL/SQL assignment operator to populate a local variable rather than selecting from dual, i.e.
l_username := 'Sample UserName';
Try this, it should work:
CREATE OR REPLACE PROCEDURE proc_Table1(UserName OUT VARCHAR2)
AS
BEGIN
SELECT 'Sample UserName' INTO UserName
FROM Dual;
dbms_output.put_line(UserName);
END PROC_TABLE1;
-------------------------------
How to Execute
-------------------------------
declare
name varchar2(50);
result varchar2(100);
begin
proc_table1(name);
end;

Calling a stored PROCEDURE in Toad

I have a defined a new stored procedure but get a error while calling it,
CREATE OR REPLACE PROCEDURE SCOTT.getempsal(
p_emp_id IN NUMBER,
p_emp_month IN CHAR,
p_emp_sal OUT INTEGER)
AS
BEGIN
SELECT EMP_SAL
INTO p_emp_sal
FROM EMPLOYEE_SAL
WHERE EMP_ID = p_emp_id
AND EMP_MONTH = p_emp_month;
END getempsal;
And trying to call it:
getempsal(1,'JAN',OUT) --Invalid sql statement.
Your procedure contains an out parameter, so you need to call it in block like:
declare
a number;
begin
getempsal(1,'JAN',a);
dbms_output.put_line(a);
end;
A simple procedure (let's say with a number parameter) can be called with
exec proc(1);
or
begin
proc(1);
end;
Just write EXECUTE procedure_name('provide_the_valueof_IN parameter','value of in parameter', :k) ;
Run this statement a popup will come set the parameters as in out and the datatype too. U will see the output in another popup window.

What to pass as a SYS_REFCURSOR argument

Here I have a stored procedure in Oracle:
CREATE OR REPLACE PROCEDURE StP_COMPS
IS
CV_1 SYS_REFCURSOR;
BEGIN
OPEN CV_1 FOR SELECT * FROM COMPUTERS;
END;
When I execute the procedure like EXEC SP_COMPS I get no error, the SQL Developer just shows "ananymous block completed". Then I change the procedure to a
CREATE OR REPLACE PROCEDURE SP_COMPS
(cv_1 OUT SYS_REFCURSOR)
IS
BEGIN
OPEN CV_1 FOR SELECT * FROM COMPUTERS;
END;
and when I execute I get error stating that the number of type of the arguments are wrong. I'm very curious what I could send as an argument to the procedure if it's just an output parameter. I want to get the result set of the query run inside the procedure. What am I doing wrong here?
P.S. When I try to run the procedure by right clicking the procedure and selecting Run I get:
DECLARE
CV_2 sys_refcursor;
BEGIN
SP_COMPS(
CV_2 => CV_2
);
:CV_2 := CV_2; -- <--Can't understand this part
END;
You have a variable, you should execute the procedure like:
DECLARE
CV_1 SYS_REFCURSOR;
BEGIN
SP_COMPS(CV_1);
--use cv_1
END;
UPDATE(after OP update): That's a simple template for testing. As explained here: Easiest method to test an Oracle Stored Procedure, just run that code, and select ref_cursor as type of cv2 variable.

Resources