Why does this basic 'Select Into' stored procedure not work? - sql

I'm running Oracle SQL developer and I've got the following Stored Procedure. I'm quite new to this but really not sure why this isn't working:
CREATE OR REPLACE PROCEDURE CHECKDUPLICATE(
username1 IN USERS.USERNAME%TYPE,
o_username OUT USERS.USERNAME%TYPE
)
IS
BEGIN
SELECT USERNAME
INTO o_username
FROM USERS WHERE username1 = o_username;
END;
When I try to call it:
DECLARE
o_username USERS.USERNAME%TYPE;
BEGIN
CHECKDUPLICATE('Jacklin', o_username);
DBMS_OUTPUT.PUT_LINE('username : ' || o_username);
END;
I get the error message:
Error starting at line 1 in command:
DECLARE
o_username USERS.USERNAME%TYPE;
BEGIN
CHECKDUPLICATE(Jacklin, o_username);
DBMS_OUTPUT.PUT_LINE('username : ' || o_username);
END;
Error report:
ORA-06550: line 5, column 19:
PLS-00201: identifier 'JACKLIN' must be declared
ORA-06550: line 5, column 4:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What does it mean by "Identifier 'Jacklin' must be declared? (Table is called USERS, and column name is called USERNAME). Any help would be appreciated.
EDIT** I put Jacklin in quotes, and I get this message now:
Error report:
ORA-01403: no data found
ORA-06512: at "L13JAV04.CHECKDUPLICATE", line 9
ORA-06512: at line 6
01403. 00000 - "no data found"
*Cause:
*Action:
Even though Jacklin does it exist in the database!

Once you quote 'Jacklin' so that it's treated as a string literal rather than an identifier, your SQL statement doesn't look right.
SELECT USERNAME
INTO o_username
FROM USERS
WHERE username1 = o_username;
My wager is that you want to use the input parameter in your WHERE clause, not the output parameter.
SELECT USERNAME
INTO o_username
FROM USERS
WHERE username1 = username;
It doesn't make sense to check the value of an output parameter when you haven't done anything to initialize it.
But your code still doesn't seem to make sense. A SELECT INTO will throw an error if anything other than 1 row is returned. If your query returns 0 rows, you'll get a NO_DATA_FOUND exception. If your query returns more than 1 row, you'll get a TOO_MANY_ROWS exception. Your procedure is named CheckDuplicate so I'm guessing that it's purpose is to check whether a particular username already exists in the table rather than trying to insert it and catching the unique constraint violation error. If that is the intention of your code
You probably want it to be a function
You probably don't want to return the username
You probably want to return an indicator of whether the username already exists
My guess, therefore, is that you would want something like
create or replace function isAvailable( p_username IN USERS.USERNAME%TYPE )
return Boolean
is
l_username USERS.USERNAME%TYPE;
begin
select username
into l_username
from users
where username = p_username;
return false;
exception
when no_data_found
then
return true;
end;

You need to put Jacklin within quotes for it to be treateed as a string. Otherwise the parser thinks it's a field name.

There would have been no user by name of "Jacklin" thats why its giving you the error. Please add an exception at the end
WHEN NO_DATA_FOUND
THEN
......

Related

How to get procedure to run 'PLS-00201: identifier "DISP_GUIDE_NAM' must be declared" oracle

I'm a beginner to sql and I'm having troubling calling a stored procedure. I have put down the procedure, call, and error. I have been researching for hours trying to figure out why I'm getting this error. I am a database administrator. Oracle Database 11g Express Edition.
Thanks for any help.
Procedure
CREATE OR REPLACE PROCEDURE DISP_GUIDE_NAM (I_GUIDE_NUM IN GUIDE.GUIDE_NUM%TYPE) AS
I_LAST_NAME GUIDE.LAST_NAME%TYPE;
I_FIRST_NAME GUIDE.FIRST_NAME%TYPE;
SET SERVEROUTPUT ON;
BEGIN
SELECT LAST_NAME, FIRST_NAME
INTO I_LAST_NAME, I_FIRST_NAME
FROM GUIDE
WHERE GUIDE_NUM=I_GUIDE_NUM;
DBMS_OUTPUT.PUT_LINE(RTRIM(I_FIRST_NAME)||' '||RTRIM(I_LAST_NAME));
END;
/​
Call
begin
disp_guide_nam('AM01');
end;
/
Error:
ORA-06550: line 2, column 1:
PLS-00201: identifier 'DISP_GUIDE_NAM' must be declared
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
1. begin
2. disp_guide_nam('AM01');
3. end;
4. /
it should give me guide number, first name last name.

I am executing following procedure in oracle but get compilation error

Actually I want to get usernames from table using Stored Procedure
CREATE OR REPLACE PROCEDURE GetRecord
(
p_ID IN integer ,
p_user OUT VARCHAR2
)
AS
BEGIN
SELECT
USERNAMES
INTO
p_user
FROM tblUsers
WHERE ID = p_ID ;
END GetRecord;
BEGIN
DECLARE A VARCHAR2
EXECUTE GetRecord(21,A);
END
When I run the above procedure I got following errors
Error starting at line : 17 in command -
BEGIN
DECLARE A VARCHAR2
EXECUTE GetRecord(21,A);
END
Error report -
ORA-06550: line 3, column 1:
PLS-00103: Encountered the symbol "EXECUTE" when expecting one of the following:
:= . ( # % ; not null range default character
The symbol ";" was substituted for "EXECUTE" to continue.
ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Actually I want to get usernames from table using Stored Procedure
The code that you are using to execute your procedure appears to be incorrect. It sounds like you want
DECLARE
l_usernames tblUsers.usernames%type;
BEGIN
GetRecord( 21, l_usernames );
END;
In general, I'd strongly object to your naming convention. It makes no sense to use a plural usernames as a column name. A procedure named GetRecord tells you nothing about what it does-- GetUsername would be much more meaningful. Your anonymous block also appears to be declaring a local variable a that stores the returned username which also makes no sense from the standpoint of using meaningful identifiers.
I'd also suggest that a procedure is the wrong approach here. If your goal is to return a single value, use a function. Functions return things, procedures do not. A function can potentially be used in a SQL statement, a procedure cannot be.

Errors while executing Procedure

I've been struggling to work out why my procedure isn't working for a while. So can anyone help me, Sorry I'm still very new to sql. It it should lookup the course_number based on the course name. if it does not find a match, it defaults the course number to ****. it then inserts a new record into the student_course table. Here is a copy of the database I'm using https://www.dropbox.com/s/cmqmzggiygxbkth/dissertation_database.txt
https://www.dropbox.com/s/hm90jga9zsaawnt/course_procedure.txt
CREATE OR REPLACE PROCEDURE upda_course
( name IN VARCHAR2 )
IS
course_id VARCHAR2;
cursor c1 is
SELECT course_id
FROM courses
WHERE name = name;
BEGIN
open c1;
fetch c1 into course_id;
if c1%notfound then
course_id := 9999;
end if;
INSERT INTO course_modules
( course_id, module_id
)
VALUES
( name,
course_id );
commit;
close c1;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END upda_course;
/
SHOW ERRORS;
EXECUTE upda_course('business computing systems');
SHOW ERRORS;
Error Displayed when Executed
*
ERROR at line 1:
ORA-20001: An error was encountered - -6502 -ERROR- ORA-06502: PL/SQL: numeric
or value error: character to number conversion error
ORA-06512: at "INS2014_106.UPDA_COURSE", line 29
ORA-06512: at line 1
Please try,
Procedure is taking a parameter(Name) as an input:
EXECUTE upda_course 'somename';
Try this:
EXECUTE upda_course('some course');
Specify the input value for input parameter
EXECUTE upda_course 'somename';
I belive course_id column in course_modules table is also a number . You are trying to insert a varchar2 to the column which is Number Hence the error. Also use your input variable as something like v_nameas your query below will return all rows from the table as variable name is same as column name. Same with course_id variable inside the procedure.
SELECT course_id
FROM courses
WHERE name = name;

Call oracle procedure with an empty cursor?

I have a procedure that looks like this:
PROCEDURE myprocedure (
i_area IN somenumber%TYPE,
io_areadata IN OUT areacursor
);
And I wonder if there is any simple way to call procedure? I have data for the first argument but for the second I do not know what enter. Is there any way I could enter an empty cursor or something similar?
This is what I've done so far:
BEGIN
package.myprocedure (
12345
);
END;
And that gives me the following error:
Error report:
ORA-06550: row 2, column 1:
PLS-00306: wrong number or types of arguments in call to 'MYPROCEDURE'
ORA-06550: row 2, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
Thank you in advance and sorry for my (maybe) poor explanation. I have little experience in PL/SQL.
You have to supply that parameter. You could do this:
DECLARE
dummy package.areacursor;
BEGIN
package.myprocedure (
12345, dummy
);
END;
By this way you can call a stored procedure by passing particular value to a particular parameter.
Try this.
Begin
package.myprocedure(i_area => '12345);
end;

stored functions execution issue

I have a table from where I want get next lowest priority DVDid, give me that as output and delete it from table. I'm doing this by creating a Package...code gets compiled but when I execute it give me error. Any suggestion?
table:
RENTALQUEUE (MEMBERID, DVDID, DATEADDEDINQUEUE, PRIORITY)
Here is the package:
create or replace
PACKAGE pk_GET_TITLEID AS
procedure sp_GET_TITLEID(memberid_arg IN NUMBER,
titleid_arg out number) ;
end pk_GET_TITLEID;
create or replace
package body pk_GET_TITLEID as
procedure sp_GET_TITLEID
(memberid_arg IN NUMBER,
titleid_arg out number)
IS
v_priority number;
begin
SELECT MIN(PRIORITY)into v_priority
FROM RENTALQUEUE
WHERE memberid = memberid_arg;
DBMS_OUTPUT.PUT_LINE (titleid_arg);
DELETE FROM RENTALQUEUE
WHERE MEMBERID=memberid_arg AND dvdid=titleid_arg;
END sp_GET_TITLEID;
END PK_GET_TITLEID;
Here is how I call it:
execute pk_get_titleid.sp_get_titleid('1');
Here is the error:
Error:
Error starting at line 403 in command:
execute pk_get_titleid.sp_get_titleid('1')
Error report:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SP_GET_TITLEID'
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.
The error is:
PLS-00306: wrong number or types of arguments in call to 'SP_GET_TITLEID'
How many parameters does your procedure have in its signature?
procedure sp_GET_TITLEID
(memberid_arg IN NUMBER
, titleid_arg out number)
Two. How many parameters are you passing?
execute pk_get_titleid.sp_get_titleid('1')
One.
An out parameter is still a parameter. You need to include it in the call. Specifically you need a variable in the calling environment which can receive the answer from the called procedure. The fact that your code doesn't actually populate the titleid_arg parameter doesn't affect this.
The non-population of the titleid_arg parameter just highlights the confusion in your code. Perhaps it should be an IN parameter? Perhaps you need to select it from another table? What is the point of v_priority?