Am getting compilation error whenever I execute - oracle

CREATE or REPLACE PROCEDURE SP_INSERT_OVERSEAS_COMPANY
(
COMPANY_CODE IN NUMBER,
COMPANY_NAME IN VARCHAR2,
STREET_ADDR IN VARCHAR2,
COMPANY_STATE IN VARCHAR2,
ZIP_CODE IN VARCHAR2,
COUNTRY IN VARCHAR2,
CONTACT_PERSON IN VARCHAR2,
CONTACT_NUMBER IN VARCHAR2
)
AS
BEGIN
IF sysdate between trunc(sysdate,'DD')+interval '9' hour and trunc(sysdate,'DD')+interval '17' hour
THEN
INSERT INTO Overseas_Company
VALUES
(COMPANY_NAME,COMPANY_NAME,STREET_ADDR,COMPANY_STATE,ZIP_CODE,COUNTRY,CONTACT_PERSON,CONTACT_NUMBER);
ELSE
dbms_output.put_line ('Process is outside of normally working hours');
END IF;
END SP_INSERT_OVERSEAS_COMPANY;
/

Should COMPANY_NAME,COMPANY_NAME,... be COMPANY_CODE,COMPANY_NAME,...? The parameter COMPANY_CODE is not used in the procedure. Are you getting numeric conversion errors?

Related

How can I run this stored procedure with options?

CREATE TABLE Docente
(
opcion varchar2(10),
id_Docente NUMBER,
nombre VARCHAR2(30),
apellido VARCHAR2(30),
cedula NUMBER,
titulo VARCHAR2(100),
observaciones VARCHAR2(200),
estado VARCHAR2(10),
mensaje varchar2(50) NULL,
CONSTRAINT PK_Docente PRIMARY KEY (id_Docente)
);
CREATE OR REPLACE PROCEDURE sp_crud_docente(
p_opcion varchar2,
p_id_Docente NUMBER,
p_nombre VARCHAR2,
p_apellido VARCHAR2,
p_cedula NUMBER,
p_titulo VARCHAR2,
p_observaciones VARCHAR2,
p_estado VARCHAR2,
p_mensaje out varchar2)
as
v_valor int;
v_row Docente%rowtype;
begin
if (p_opcion = 'I') then
begin
Select MAX(id_Docente)+ 1 into v_valor from Docente;
if v_valor is null then
v_valor := 1;
p_mensaje:= 'Registro inserted...';
DBMS_OUTPUT.PUT_LINE(p_mensaje);
insert into Docente
values (p_opcion,v_valor,p_id_Docente,p_nombre,p_apellido,p_cedula,p_titulo,p_observaciones,p_estado);
end if;
end;
else if (p_opcion = 'U') then
update Docente set opcion=p_opcion,
id_Docente=p_id_Docente,
nombre=p_nombre,
apellido=p_apellido,
cedula=p_cedula,
titulo=p_titulo,
observaciones=p_observaciones,
estado=p_estado
where id_Docente=p_id_Docente;
p_mensaje:= 'Registro updated...';
DBMS_OUTPUT.PUT_LINE(p_mensaje);
else if (p_opcion = 'S') then
begin
Select
opcion,
id_Docente,
nombre,
apellido,
cedula,
titulo,
observaciones,
estado,
mensaje
into v_row
from Docente;
p_mensaje := ('Ok');
DBMS_OUTPUT.PUT_LINE(p_mensaje||' --> '||v_row.opcion||'|'||v_row.id_Docente||'|'||v_row.nombre||'|'||v_row.apellido||'|'||v_row.cedula||'|'||v_row.titulo||'|'||v_row.observaciones||'|'||v_row.estado||'|'||v_row.mensaje);
end;
else if (p_opcion = 'D') then
Delete from Docente where id_Docente=p_id_Docente;
p_mensaje := ('Proceso ejecutado correctamente');
DBMS_OUTPUT.PUT_LINE(p_mensaje);
commit;
end if;
end if;
end if;
end if;
EXCEPTION
WHEN OTHERS then
p_mensaje := ('ERROR. No se pudo ejecutar el proceso');
rollback;
end;
Take out the length of the incoming parameters:
create or replace procedure sp_crud_docente (
p_opcion varchar2,
p_id_Docente NUMBER,
p_nombre VARCHAR2,
p_apellido VARCHAR2,
p_cedula NUMBER,
p_titulo VARCHAR2,
p_observaciones VARCHAR2,
p_estado VARCHAR2,
p_mensaje out varchar2)
See 8.7.1 Formal and Actual Subprogram Parameters section of the 19c manual "Database PL/SQL Language Reference"
Bobby
Apart from the fact that varchar2 parameters can't have size, this is practically unsolvable without you posting the docente table description.
Bad habits kick; you're wrongly relying on the fact that you know what you're doing, but it turns out you don't.
I can't figure out how many nor which columns docente contains, and in which order.
insert into docente should explicitly mention all columns you're inserting into, one-by-one. Code you wrote suggests that table contains 9 columns (opcion, id_docente, nombre, apellido, cedula, titulo, observaciones, estado).
update, on the other hand, updates only 6 of them (nombre, apellido, cedula, tutlo, observaciones, estado) which doesn't collide with insert; it is OK if you don't update all columns.
However, p_opcion = 'S' selects only 7 columns into v_row which is declared as docente%rowtype. Out of those 7 columns, I believe you "forgot" a comma between id_docente and nombre - those are two columns, it's not that nombre is id_docente's alias, right? Suppose it is 8 columns that are selected after all. Now, that collides with number of columns you used in insert (9 of them). If you're selecting into %rowtype, you must select ALL columns in EXACT ORDER.
I suggest you review code you wrote, pay attention about what I said and fix those errors.

Accessing record fields by their name in Oracle

I want to know if there exists something like Reflection API in PL/SQL or not.
I have a table like
create table my_table (
id number,
value1 number,
value2 number,
value3 number,
value4 number,
value5 number);
And I have a variable as
rec as my_table%rowtype
... fill rec
insert into my_table values rec;
is there any way I can populate rec field dynamically by its name.
I mean I know the index (in this case something between 1 and 5), so I want to set 'value'||index to something.
As in my real case the last index is much more than 5, using a set of if/elsif is not proper. By the way number of fields is increased in long term (for example value6, value7 ma be added next year and so on, so I wan to write some kind of code not to be changed on every new column).
You can access variables in a program using dynamic SQL only if they are available globally. If you declare you record in the spec you can build utility functions that will use EXECUTE IMMEDIATE to build a small PL/SQL block to set the value. Here is a simple example of what you are looking for. Notice you can overload the set procedure to keep your datatypes intact.
CREATE TABLE my_table (
value1 NUMBER,
value2 VARCHAR2(100),
value3 DATE);
CREATE OR REPLACE PACKAGE pkg_my_table IS
my_table_rec my_table%ROWTYPE;
FUNCTION build_statement(i_record IN VARCHAR2,
i_field IN VARCHAR2) RETURN VARCHAR2;
PROCEDURE set_value(i_record IN VARCHAR2,
i_field IN VARCHAR2,
i_value IN VARCHAR2);
PROCEDURE set_value(i_record IN VARCHAR2,
i_field IN VARCHAR2,
i_value IN NUMBER);
PROCEDURE set_value(i_record IN VARCHAR2,
i_field IN VARCHAR2,
i_value IN DATE);
PROCEDURE insert_a_row;
END pkg_my_table;
/
CREATE OR REPLACE PACKAGE BODY pkg_my_table IS
FUNCTION build_statement(i_record IN VARCHAR2,
i_field IN VARCHAR2) RETURN VARCHAR2 IS
BEGIN
RETURN 'BEGIN ' || lower($$PLSQL_UNIT) || '.' || i_record || '.' || i_field || ' := :x; END;';
END build_statement;
PROCEDURE set_value(i_record IN VARCHAR2,
i_field IN VARCHAR2,
i_value IN VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE build_statement(i_record => i_record,
i_field => i_field)
USING i_value;
END set_value;
PROCEDURE set_value(i_record IN VARCHAR2,
i_field IN VARCHAR2,
i_value IN NUMBER) IS
BEGIN
EXECUTE IMMEDIATE build_statement(i_record => i_record,
i_field => i_field)
USING i_value;
END set_value;
PROCEDURE set_value(i_record IN VARCHAR2,
i_field IN VARCHAR2,
i_value IN DATE) IS
BEGIN
EXECUTE IMMEDIATE build_statement(i_record => i_record,
i_field => i_field)
USING i_value;
END set_value;
PROCEDURE insert_a_row IS
BEGIN
my_table_rec := NULL;
set_value(i_record => 'my_table_rec',
i_field => 'value1',
i_value => 42);
set_value(i_record => 'my_table_rec',
i_field => 'value2',
i_value => 'forty-two');
set_value(i_record => 'my_table_rec',
i_field => 'value3',
i_value => to_date('1/1/1942',
'mm/dd/yyyy'));
INSERT INTO my_table
VALUES my_table_rec;
END insert_a_row;
END pkg_my_table;
/
BEGIN
pkg_my_table.insert_a_row;
END;
/
SELECT *
FROM my_table;
-- 42 forty-two 1/1/1942
Beware globals: you need to reset them correctly before using them again or you might get data carried over from previous invocations. Setting an entire record type variable to NULL will reset all the underlying fields (handy).
You will also be prone to ORA-04068: existing state of packages has been discarded errors should you compile PL/SQL with globals where there are active sessions referencing your code. Generally this is not a problem, but it is a behavioral difference.

Encountered the symbol

I'm new in oracle pl/sql. I've created new package procedure list then I wanted to realise each procedure in seperate block. Here is list of procedures that I wanted creat for now it's only one.
create or replace package listProcedures is
procedure bss_claim12
( o_claim_id out number,
o_error_code out varchar2,
o_error_msg out varchar2,
i_card_number varchar2,
i_phone_number varchar2,
i_product_id number,
i_summ_loan number,
client_code number,
mfo varchar2
);
Then I wanted to create it's body but it's giving following errors.
Compilation errors for PACKAGE BODY IBS.LISTPROCEDURES
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
create or replace package body ibs.listprocedures is
procedure bss_claim12
( o_claim_id out number,
o_error_code out varchar2,
o_error_msg out varchar2,
i_card_number varchar2,
i_phone_number varchar2,
i_product_id number,
i_summ_loan number,
client_code number,
mfo varchar2
) is
begin
dbms_output.put_line(o_claim_id);
dbms_output.put_line(o_error_msg);
dbms_output.put_line(client_code);
end bss_claim12;
What should i do in body of this procedure to make it work well.
You should end both specification and body parts of the package :
SQL> create or replace package listProcedures is
procedure bss_claim12
( o_claim_id out number,
o_error_code out varchar2,
o_error_msg out varchar2,
i_card_number varchar2,
i_phone_number varchar2,
i_product_id number,
i_summ_loan number,
client_code number,
mfo varchar2
);
end listProcedures;
/
SQL> create or replace package body listProcedures is
procedure bss_claim12
( o_claim_id out number,
o_error_code out varchar2,
o_error_msg out varchar2,
i_card_number varchar2,
i_phone_number varchar2,
i_product_id number,
i_summ_loan number,
client_code number,
mfo varchar2
) is
begin
dbms_output.put_line(o_claim_id);
dbms_output.put_line(o_error_msg);
dbms_output.put_line(client_code);
end bss_claim12;
end listProcedures;
/

Stored procedure not returning the expected value

Stored procedure is not returning the value that I am expecting. I did not know what is the problem with the procedure.
Here is the procedure:
CREATE OR REPLACE procedure PRC_CUSTOMER_WITH_LOGIN
(p_name_out out varchar2,
p_count out int,
p_all_records out SYS_REFCURSOR,
p_mode in varchar2,
p_id in varchar2,
p_name_in in varchar2,
p_contact_no in varchar2,
p_email in varchar2,
p_address in varchar2)
IS
BEGIN
IF p_mode='q'
THEN
select NAME into p_name_out from customer where id='1';
ELSIF p_mode='i'
THEN
INSERT into customer(id,name,contactNo,email,address)
Values(p_id, p_name_in, p_contact_no , p_email , p_address);
ELSIF p_mode='u'
THEN
UPDATE customer set name=p_name_in, contactNo=p_contact_no, email=p_email, address=p_address
where id=p_id;
ELSIF p_mode='d'
THEN
DELETE from customer where id=p_id;
ELSIF p_mode='a'
THEN
OPEN p_all_records FOR
select * from customer;
ELSIF p_mode='l'
THEN
SELECT COUNT(*) into p_count from customer WHERE name=p_name_in AND id=p_id;
END IF;
END;
/
This is the procedure all other conditions are working fine but the last condition is not working correctly this condition return 0 all the time whether I enter the correct id and name or wrong.
Here I am calling this procedure
cs = (OracleCallableStatement) con.prepareCall("{call TESTDB.PRC_CUSTOMER_WITH_LOGIN(?,?,?,?,?,?,null,null,null)}");
cs.registerOutParameter(1, OracleTypes.VARCHAR);
cs.registerOutParameter(2, OracleTypes.INTEGER);
cs.registerOutParameter(3, OracleTypes.CURSOR);
cs.setString(4, "l");
cs.setString(5, password);
cs.setString(6, name);
cs.executeQuery();
chk=cs.getInt(2);
System.out.println(chk);
Can someone tell me what the mistake is that I'm making here? I shall be thankful .... :)
Try to use UPPER both side so if any case sensitive names are there then it will nullify it.
SELECT COUNT(*) into p_count from customer WHERE upper(name)=upper(p_name_in) AND id=p_id;
Maybe you are wrong when you set id with the password value?
1-> p_name_out out varchar2,
2-> p_count out int,
3-> p_all_records out SYS_REFCURSOR,
4-> p_mode in varchar2,
5-> p_id in varchar2, --- 5 is id, not password ??!?
6-> p_name_in in varchar2,
p_contact_no in varchar2,
p_email in varchar2,
p_address in varchar2
cs = (OracleCallableStatement) con.prepareCall(
"{call TESTDB.PRC_CUSTOMER_WITH_LOGIN(?,?,?,?,?,?,null,null,null)}");
cs.registerOutParameter(1, OracleTypes.VARCHAR);
cs.registerOutParameter(2, OracleTypes.INTEGER);
cs.registerOutParameter(3, OracleTypes.CURSOR);
cs.setString(4, "l");
cs.setString(5, password);
cs.setString(6, name);
Another possibility is your id/password is stored as encrypted thing?

Wrong number or types of arguments error while calling procedure

How can i call procedure inside another procedure in Oracle?I'm trying as follow but i am getting that error "PLS-00306 (325: 13): PLS-00306: wrong number or types of arguments in call to 'TITLE_CRUD'"
PROCEDURE create_title(
P_TITLE varchar2,
P_USER varchar2,
P_ERR OUT VARCHAR2
)
IS
BEGIN
IF P_TITLE IS NULL THEN
P_ERR := 'Null value';
ELSE
title_crud('I',NULL,P_TITLE,P_USER);
END IF;
END;
PROCEDURE title_crud(
P_OP_TYPE VARCHAR2,
P_ID number,
P_TITLE varchar2,
P_USER varchar2,
P_ERR OUT VARCHAR2
)...
See the change below, the call to title_crud doesn't have a variable to hold what is being returned by it (p_err)
PROCEDURE create_title(
P_TITLE varchar2,
P_USER varchar2,
P_ERR OUT VARCHAR2
)
IS
BEGIN
IF P_TITLE IS NULL THEN
P_ERR := 'Null value';
ELSE
title_crud('I',NULL,P_TITLE,P_USER,P_ERR); -- Extra Parameter
END IF;
END;
PROCEDURE title_crud(
P_OP_TYPE VARCHAR2,
P_ID number,
P_TITLE varchar2,
P_USER varchar2,
P_ERR OUT VARCHAR2
)...
An easy mistake to make but I find easier to avoid when calling a procedure you pass parameters by named notation. Your call to title_crud becomes
...
title_crud(P_OP_TYPE => 'I',
P_ID => NULL,
P_TITLE => P_TITLE,
P_USER => P_USER,
P_ERR => P_ERR);
...

Resources