error when entering values by Oracle PL/SQL - oracle

I want to enter a value by console in Oracle 10g with PL SQL. I am using the web based interface and not the SQL*.
The program is like this:
DECLARE
v_desc VARCHAR2(50);
BEGIN
v_desc:=show_desc(&sv_cnumber);
DBMS_OUTPUT.PUT_LINE(v_desc);
END;
and the function is:
CREATE OR REPLACE FUNCTION show_desc
(i_course_id course.course_id%TYPE)
RETURN varchar2
AS
v_desc varchar2(50);
BEGIN
SELECT description
INTO v_desc
FROM courses
WHERE course_id=i_course_id;
RETURN v_desc;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN('no description found');
END;
when I run this code, I got an error that says:
ORA-06550: line 4, column 37:
PLS-00103: Encountered the symbol "&" when expecting one of the following:
what is the mistake?
Thanks

If you want a prompt to be displayed so you can enter a value for the variable, try replacing & with colon
v_desc:=show_desc(:sv_cnumber);
& is a special symbol in PL/SQL

Related

Cannot execute a stored procedure in Oracle

Here is a simple example using Toad for Data Analysts 3.0.1.1734. I have full permissions on the schema JSWEENEY.
Create the table
CREATE TABLE JSWEENEY.TEMP_SQL
(
SQL VARCHAR2(3000)
);
Create the procedure
CREATE OR REPLACE PROCEDURE JSWEENEY.SP_INSERT_SQL
IS
BEGIN
INSERT INTO JSWEENEY.TEMP_SQL(SQL) VALUES('SELECT * FROM TEMP_SQL');
COMMIT;
END JSWEENEY.SP_INSERT_SQL;
/
Execute the procedure:
BEGIN
JSWEENEY.SP_INSERT_SQL;
END;
The first error:
ORA-06550: line 2, column 11:
PLS-00905: object JSWEENEY.SP_INSERT_SQL is invalid
ORA-06550: line 2, column 2: PL/SQL: Statement ignored
Execute the procedure:
BEGIN
EXECUTE JSWEENEY.SP_INSERT_SQL;
END;
The second error:
ORA-06550: line 2, column 10:
PLS-00103: Encountered the symbol "JSWEENEY" when expecting one of the following: := . ( # % ; immediate The symbol ":=" was substituted for "JSWEENEY" to continue.
Any suggestions would be greatly appreciated.
When you compile the procedure you will get an error; if your client doesn't display that then you can query the user_errors view (or all_errors if you're creating it in a different schema) to see the problem. Here it will be complaining that:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/13 PLS-00103: Encountered the symbol "." when expecting one of the following:
;
It's valid to use the schema name in the create call; but not as part of the end. So if you need to specify the schema at all - which you don't if you're creating an object in your own schema, but your reference to permissions makes it sound like you aren't - then it should be:
CREATE OR REPLACE PROCEDURE JSWEENEY.SP_INSERT_SQL
IS
BEGIN
INSERT INTO JSWEENEY.TEMP_SQL(SQL) VALUES('SELECT * FROM TEMP_SQL');
COMMIT;
END SP_INSERT_SQL;
/
Your second error is because execute on its is a client command (in SQL*Plus and relations), not a PL/SQL statement. The error refers to immediate because PL/SQL does have an execute immediate statement which is used for dynamic SQL, not for making static calls to procedures. Your first syntax to run the procedure is correct, once the procedure itself is valid:
BEGIN
JSWEENEY.SP_INSERT_SQL;
END;
/
try this edited the SQL statement.
create table TEMP_SQL ( col1 varchar2(100));
CREATE OR REPLACE PROCEDURE SP_INSERT_SQL
AS
BEGIN
INSERT INTO TEMP_SQL SELECT * FROM TEMP_SQL;
COMMIT;
END SP_INSERT_SQL;

How to execute oracle function in oracle apex online

I have function in oracle apex that returns true or false. When i use sqlcommand function of oracle apex I'm not able to execute the function
function authenticateUser(p_username in varchar2 ,p_password in varchar2 )
return boolean
is
p_user Users.USERNAME%type;
begin
select USERNAME into p_user from Users where upper(USERNAME)=upper(p_username) and upper(PASSWORD) = upper(p_password);
return true;
exception
when NO_DATA_FOUND then
return false;
end authenticateUser;
When i run
begin
PKG_AUTHORIZATION.authenticateUser(:p_username,:p_password);
end;
It display error as
ORA-06550: line 3, column 1:
PLS-00221: 'AUTHENTICATEUSER' is not a procedure or is undefined
ORA-06550: line 3, column 1:
PL/SQL: Statement ignored
ORA-06512: at "SYS.DBMS_SQL", line 1721
1. begin
2. PKG_AUTHORIZATION.authenticateUser(:p_username,:p_password);
3. end;
and When I write
select PKG_AUTHORIZATION.authenticateUser(:p_username,:p_password) from dual;
It throws error as
ORA-00902: invalid datatype
I suppose that function exists within the package, right?
As it returns Boolean, you can't use it directly from SQL, but PL/SQL, such as:
declare
l_val varchar2(20);
begin
l_val := case when PKG_AUTHORIZATION.authenticateUser(:p_username,:p_password) then 'true'
else 'false'
end;
end;
But, you don't have to worry about it. As you tagged the question with the Oracle Apex tag, I presume you use this function for your own authentication (instead of built-in one). If that's so, you just have to name the function which is supposed to do the job and put its name into the "Authentication function name" item.

ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'TEST' ORA-06550: line 1, column 7: PL/SQL: Statement ignored

create or replace procedure test(supplierid in number,
supplier out varchar) is
begin
select first_name
into Supplier
from lup_sup_master
where sup_id = supplierid;
end;
/
execute test(2279, :supplierid);
or
begin
execute test(2279, :supplierid); dbms_output.enable; dbms_output.put_line(supplier);
end;
Your procedure test has 2 input arguments. One has type IN which is supposed to be used with the procedure and the other parameter supplier has type OUT which means this parameter is suppose to hold the value which the Procedure returns.
As explained by #Barbaros, if you want to execute the Procedure via a SQL command prompt, you can follow the steps.
However the second way you showed was using a PLSQL Block.
begin
execute test(2279, :supplierid);
dbms_output.enable;
dbms_output.put_line(supplier); end;
In the above example of calling, you must note that Execute keyword is only be used whenever you use a SQL command line prompt. While using a PLSQL block you cal directly call the Procedure by its name as shown below. Also note that incase you have a OUT parameter, you must have OUT parameter passed to the Procedure since the Procedure is expecting 2 arguments. See below demo.
declare
var varchar2(100);
begin
test( supplierid => 2279, supplier =>var);
dbms_output.enable;
dbms_output.put_line(var);
End;
It seems you're confused between supplierid and supplier. You need to define a variable for supplier to get an output line printed, and most probably you defined a command line variable supplierid which's numeric type.
So, use below :
SQL> set serveroutput on;
SQL> var supplier varchar2;
SQL> execute test(2279, :supplier);
supplier
---------
Afzal -- as an example name

How to run a procedure having cursor output in oracle?

I have created a procedure in oracle as follows
create or replace procedure jobsfetch
(id varchar2,jobcursor out sys_refcursor)
as
begin
open jobcursor for
select * from shop.jobs where job_id = id;
end;
I run the procedure in SQL*Plus using:
exec jobsfetch('AD_ASST');
But I'm getting the following error
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'JOBSFETCH'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
How to execute this procedure since its having only one input parameter?
try this:
variable CURSOR_LIST REFCURSOR;
exec jobsfetch('AD_ASST',:CURSOR_LIST);
print CURSOR_LIST;
You can simply use a plsql block:
declare
outCur sys_refcursor;
begin
jobsfetch('AD_ASST', outCur);
/* what you need to do with the cursor */
end;
The problem is that your procedure has an out parameter, but you don't supply it.
You can't exec jobsfetch('AD_ASST');
I recommend putting the whole thing in a plsql block like Aleksej.
declare
outCur sys_refcursor;
begin
jobsfetch('AD_ASST', outCur);
/* what you need to do with the cursor */
end;
This will give you the cursor that you opened in outCur and you can do whatever you need to do with it.

How to create/call procedure in oracle 10g?

I created a procedure in Oracle but am getting an error when I attempt to execute it. Below are listed the steps I am taking to create this procedure:
SQL> ed getuserinfo
create or replace procedure getUserInfo
( p_username out Users.username%TYPE,
p_password out Users.password%TYPE ) IS
BEGIN
select username,password into p_username,p_password from users where username='yogi';
END;
/
SQL> exec getuserinfo
BEGIN getuserinfo; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'GETUSERINFO' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
What is the problem and how can I solve it? Olease can anyone help me?
You need to actually create the procedure, which you haven't done. You need a semi-colon after end and if you're creating it in SQL*Plus you need to add / to inform SQL*Plus that the block is finished:
create or replace procedure getUserInfo
( p_username out Users.username%TYPE,
p_password out Users.password%TYPE ) IS
BEGIN
select username,password into p_username,p_password from users;
END;
/
show error
It's always wise to add show error afterwards as well so that any errors are returned to the console in an understandable format.
Did you actually execute your create procedure statement? Did you get a "Procedure created." message? The fact that Oracle does not know of your getuserinfo procedure indicates to me that this statement was not performed.
I think it's procedure calling mistake!! Calling Should be like below:
SQL>var var1 varchar2(50);
SQL>var var2 varchar2(50);
SQL> exec getuserinfo(:var1, :var2);
SQL> print var1, var2;
Have Fun!!
You need to specify the out parameters when you call the procedure.
DECLARE
x Users.username%TYPE;
y Users.password%TYPE;
BEGIN
getuserinfo(x, y);
DBMS_OUTPUT.PUT_LINE('username: ' || x || ', password: ' || y);
END;

Resources