Why do I get an error in calling this procedure? - plsql

I have a procedure named student_id
procedure student_id ( v_surname in varchar2,
v_name in varchar2,
v_date_birth in varchar2,
v_gender in varchar2,
v_state in varchar2) is l_student_id varchar2;
begin
l_student_id := par_surname(v_surname) ||'-'||
par_name(v_name) ||'-'||
par_date_birht(v_date_birth) ||'-'||
par_gender(v_gender) ||'-'||
par_state(v_state);
dbms_output.put_line('Student ID : ' || l_student_id);
end student_id;
What my expected output should be after I will write the values :
(Student ID : JOHN-SMITH-170692-M-CALIFORNIA)
Error I get:
Error with beginning of line: 291 in the command -
procedure cod_fiscale ( v_surname in varchar2,
Report error -
unknown command
Error with beginning of line : 292 in the command -
v_name in varchar2,
Report error -
unknown command
Error with beginning of line : 293 in the command -
v_date_birth in varchar2,
Report error -
unknown command
Error with beginning of line : 294 in the command -
v_gender in varchar2,
Report error -
unknown command
SP2-0044:to get the list of known commands, enter HELP
and enter EXIT to exit.
Error with beginning of line : 295 in the command -
v_state in varchar2) is l_student_id varchar2;
Report error -
unknown command
Error with beginning of line : 296 in the command -
begin
l_student_id := par_surname(v_surname) ||'-'||
par_name(v_name) ||'-'||
par_date_birht(v_date_birth) ||'-'||
par_gender(v_gender) ||'-'||
par_state(v_state);
dbms_output.put_line('Student ID : ' || l_student_id);
end student_id;
Report error -
ORA-06550: line 9, column 25:
PLS-00103: Found symbol ""
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I roughly translated the error I got because it was in another language.
*Note: This isn't a standalone procedure, this code is related to my previous question.
Found at :How to write a PL/SQL procedure with x input parameters and input/ouput of x parameters combined*
What is my error here? What is the cause of the error?

Without seeing the rest of your package, I can spot one issue:
procedure student_id ( v_surname in varchar2,
v_name in varchar2,
v_date_birth in varchar2,
v_gender in varchar2,
v_state in varchar2) is
l_student_id varchar2; -- you need to declare the length of your variable
begin
...
You've declared the type of your variable, but you haven't said how long it needs to be. It should be something like:
l_student_id varchar2(400);
although you should change the 400 to the right value for the expected maximum length of the values that will be stored in that variable.

Related

Getting "Compilation unit analysis terminated" errors in PL/SQL code

This is a subtype of "base_t". When I add the "set_name" or "set_genus" I get some errors.
Here is my code:
CREATE OR REPLACE
TYPE dwarf_t UNDER base_t
( name VARCHAR2(30)
, genus VARCHAR2(30)
, CONSTRUCTOR FUNCTION dwarf_t
( name VARCHAR2
, genus VARCHAR2) RETURN SELF AS RESULT
, OVERRIDING MEMBER FUNCTION get_name RETURN VARCHAR2
-- , OVERRIDING MEMBER FUNCTION set_name (name VARCHAR2)
, MEMBER FUNCTION get_genus RETURN VARCHAR2
, MEMBER FUNCTION set_genus (genus VARCHAR2)
, OVERRIDING MEMBER FUNCTION to_string RETURN VARCHAR2)
INSTANTIABLE NOT FINAL;
/
For reference, I have created get_name in the main type, but not set_name, or get/set_genus.
My errors are as follows:
ERROR at line 1:
ORA-06545: PL/SQL: compilation error - compilation aborted
ORA-06550: line 11, column 3:
PLS-00103: Encountered the symbol "," when expecting one of the following:
return
ORA-06550: line 0, column 0:
PLS-00565: DWARF_T must be completed as a potential REF target (object type)
The (not-so-helpful) error message is because you have a function without a return type. Since this is a SET, I assume you want a procedure instead of a function.
Change this line:
MEMBER FUNCTION set_genus (genus VARCHAR2)
To this:
MEMBER PROCEDURE set_genus (genus VARCHAR2)

Calling a stored procedure with CLOB output

I am trying to use the CLOB datatype as the output parameter in my stored procedure because its resultset exceeds the storage capacity of a var datatype.
How do I execute the procedure? Below are the commands I had run to execute.
I tried assigning the resultset to the the CLOB variable using the INTO query as shown in the query.
var cl CLOB;
EXECUTE procedure_name(:cl);
print cl;
How do i declare the binding variable because if you look at the first command, I am first initializing cl as var I am not able to initialize it as CLOB as it is giving out an error.
This is a sample of my procedure. The actual query in the procedure is 700 lines long.
CREATE OR REPLACE PROCEDURE procedure_name (cl OUT CLOB)
IS
BEGIN OPEN cl FOR
SELECT * FROM .....
statement 1
.
.
.
.
.
statement n
INTO cl
FROM
statement 1
.
.
.
statement n
EXCEPTION
WHEN
OTHERS THEN
DECLARE
err_num NUMBER := SQLCODE;
err_msg VARCHAR2(512) := SQLERRM;
error_id_pk NUMBER;
error_dt DATE;
BEGIN
SELECT (REGEXP_REPLACE(CURRENT_TIMESTAMP, '[^0-9]+', ''))INTO error_id_pk FROM DUAL;
SELECT SYSDATE INTO error_dt FROM DUAL;
INSERT INTO ODS_CONTROL.ERROR_DETAILS(ERROR_ID, ERROR_CODE, ERROR_DATE, PROCEDURE_NAME, ERROR_MSG)
VALUES ( error_id_pk,
err_num,
error_dt,
'PRC_FLEXI_CARD',
err_msg
);
END;
END;
Error message:
Error starting at line : 2 in command -
EXECUTE procedure_name( :clb )
Error report -
ORA-06550: line 1, column 7:
PLS-00905: object procedure_name is invalid
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:
[TL;DR] VAR is a keyword for declaring a variable and is not a data type; your actual error is due to using invalid syntax when you tried to define your procedure and it has not compiled.
VAR is short for VARIABLE and defines a PL/SQL bind variable.
This declaration has the syntax:
VAR[IABLE] [variable [type] ]
where type represents one of the following:
NUMBER
CHAR
CHAR (n [CHAR | BYTE])
NCHAR
NCHAR (n)
VARCHAR2 (n [CHAR | BYTE])
NVARCHAR2 (n)
BLOB
BFILE
CLOB
NCLOB
REFCURSOR
BINARY_FLOAT
BINARY_DOUBLE
So with:
var cl CLOB;
you are declaring a variable using the VAR keyword and the variable is named cl and has the type CLOB.
Also, your CREATE PROCEDURE statement has a syntax error as you cannot have the single quotes around the procedure name. For example:
CREATE PROCEDURE procedure_name (clb OUT CLOB)
IS
BEGIN
clb := 'test';
END;
/
Then:
VAR cl CLOB;
EXECUTE procedure_name( :cl );
PRINT cl;
Outputs:
test
Updated:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE ERROR_DETAILS(
ERROR_ID NUMBER,
ERROR_CODE NUMBER,
ERROR_DATE DATE,
PROCEDURE_NAME VARCHAR2(30),
ERROR_MSG VARCHAR2(512)
)
/
CREATE PROCEDURE procedure_name (cl OUT CLOB)
IS
BEGIN
SELECT DUMMY
INTO cl
FROM dual
WHERE ROWNUM = 1;
EXCEPTION
WHEN
OTHERS THEN
DECLARE
err_num NUMBER := SQLCODE;
err_msg VARCHAR2(512) := SQLERRM;
BEGIN
INSERT INTO /* ODS_CONTROL. */ ERROR_DETAILS(
ERROR_ID,
ERROR_CODE,
ERROR_DATE,
PROCEDURE_NAME,
ERROR_MSG
) VALUES (
TO_NUMBER( TO_CHAR( CURRENT_TIMESTAMP, 'YYYYMMDDHH24MISSFF9' ) ),
err_num,
SYSDATE,
'PRC_FLEXI_CARD',
err_msg
);
END;
END;
/
Query 1:
SELECT * FROM USER_ERRORS
Results:
No rows selected

Passing a XML in Execute Immediate

varSQL: = 'DECLARE
varOptionId NUMBER;
varXML XMLTYPE;
varHsCode VARCHAR2(200);
varHsCodeCount NUMBER;
varId NUMBER;
RVAL OBJ_RETURN:=OBJ_RETURN(NULL,NULL,NULL,NULL);
BEGIN
varXML:=:refXML;
varHsCode:= varXML.EXTRACT('/OBJECT/HsCodes/HsCode/text()').GETSTRINGVAL();
:out:=varHsCode;
END';
EXECUTE IMMEDIATE varSQL USING PXML,OUT varOut;
This gives the following error:
ORA-06550: line 9, column 15:
PLS-00382: expression is of wrong type
ORA-06550: line 9, column 2:
PL/SQL: Statement ignored
ORA-06512: at "BIZZXE_V2_SCH.SYSTEM_UTILITY", line 225
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Is it possible to pass a XML?
Yes, you can pass an XMLType variable as a bind parameter:
declare
varsql varchar2(1024);
varout varchar2(4);
pxml xmltype := xmltype('<OBJECT><HsCodes><HsCode>Test</HsCode></HsCodes></OBJECT>');
begin
varSQL := 'DECLARE
varOptionId NUMBER;
varXML XMLTYPE;
varHsCode VARCHAR2(200);
varHsCodeCount NUMBER;
varId NUMBER;
--RVAL OBJ_RETURN:=OBJ_RETURN(NULL,NULL,NULL,NULL);
BEGIN
varXML:=:refXML;
varHsCode:= varXML.EXTRACT(''/OBJECT/HsCodes/HsCode/text()'').GETSTRINGVAL();
:out:=varHsCode;
END;';
EXECUTE IMMEDIATE varSQL USING PXML,OUT varOut;
dbms_output.put_line(varOut);
end;
/
PL/SQL procedure successfully completed.
Test
The error you are getting is because you have declared PXML as a string; either a varchar2 or CLOB variable:
declare
varsql varchar2(1024);
varout varchar2(4);
</HsCodes></OBJECT>');
pxml varchar2(64) := '<OBJECT><HsCodes><HsCode>Test</HsCode></HsCodes></OBJECT>';
begin
varSQL := 'DECLARE
varOptionId NUMBER;
varXML XMLTYPE;
varHsCode VARCHAR2(200);
varHsCodeCount NUMBER;
varId NUMBER;
--RVAL OBJ_RETURN:=OBJ_RETURN(NULL,NULL,NULL,NULL);
BEGIN
varXML:=:refXML;
varHsCode:= varXML.EXTRACT(''/OBJECT/HsCodes/HsCode/text()'').GETSTRINGVAL();
:out:=varHsCode;
END;';
EXECUTE IMMEDIATE varSQL USING PXML,OUT varOut;
dbms_output.put_line(varOut);
end;
/
Error report -
ORA-06550: line 9, column 10:
PLS-00382: expression is of wrong type
ORA-06550: line 9, column 2:
PL/SQL: Statement ignored
ORA-06512: at line 21
In your example there's no reason to use dynamic SQL anyway, but maybe your real-world example is more complicated.

how to execute pl/sql procedure

Procedure:
create or replace
PROCEDURE ADDITION
(
A IN NUMBER
, B IN NUMBER
, C OUT number
) AS
BEGIN
C := A+B;
dbms_output.put_line(c);
END ADDITION;
executing:
begin
addition(4,5);
end;
Error:
PLS-00306: wrong number or types of arguments in call to 'ADDITION'
ORA-06550: line 2, column 2:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
How to rectify this error.Let me know what is the wrong in code
Your procedure expects an out parameter which you also need to provide:
declare
add_result number;
begin
addition(4,5,add_result);
end;
/

Simple PL/SQL function to test if a string is a number

I'm experienced with T-SQL from SQL Server, but have recently begun working on a project utilizing an Oracle database (11g) and am having some issues writing what seems to be basic code.
I need to test whether a set of values are numeric, and only insert them into a table if they are. PL/SQL doesn't seem to have an is_number function, so I wrote my own based on an AskTom question.
create or replace
function IS_NUMBER(str in varchar2) return boolean
IS
n number;
BEGIN
select to_number(str) into n from dual;
return (true);
EXCEPTION WHEN OTHERS THEN
return (false);
END;
Eventually, I'd like to use this function in a WHERE clause, but for now I'm just trying to get it to run at all:
declare
str varchar2(1);
n boolean;
begin
str := '0';
select ca_stage.is_number(str) into n from dual;
end;
In SQL Developer, trying to run this gives me the following error report:
Error report:
ORA-06550: line 6, column 39:
PLS-00382: expression is of wrong type
ORA-06550: line 6, column 19:
PLS-00382: expression is of wrong type
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
The error report is straight-forward, but doesn't make sense. The function accepts a varchar2, and that's what I'm using as an input variable. It returns a boolean and again, that's what I'm using.
As I said, this is basic code, so I assume that I'm missing something fundamental.
Return a SQL datatype, e.g. VARCHAR2. Also, I'd recommend against using WHEN OTHERS. Also, you don't need a query on dual:
create or replace
function IS_NUMBER(str in varchar2) return varchar2
IS
n number;
BEGIN
n := to_number(str);
return 'Y';
EXCEPTION WHEN VALUE_ERROR THEN
return 'N';
END;

Resources