Make the declared variable inside a trigger append its value if more than one condition is met - sql

Well I was writing a trigger and I wanted that the declared variable v_message to append every time the condition is met. So for example if the first two statements are met the v_message should be something like "Invalid account Invalid amount". I must also find a way to separate these two messages by a space if the v_message already contains some sort of an error message as it wont be readable if you had something like this "Invalid accountInvalid amount".
CREATE OR REPLACE TRIGGER checkPayments
BEFORE INSERT ON tbl_payments
FOR EACH ROW
DECLARE
v_message VARCHAR(100 CHAR);
v_is_valid CHAR(1) := 'N';
BEGIN
IF getActiveUser(getOrderUsername(:new.order_id)) = 0
THEN v_message := 'Invalid account';
ELSIF isValidAmount(:new.payment_amount) = 0
THEN v_message := 'Invalid amount';
ELSIF checkOrderExist(:new.order_id) = 0
THEN v_message := 'Invalid order ID';
ELSIF (getValidOrderPayments(:new.order_id) + :new.payment_amount ) > getOrderTotal(:new.order_id)
THEN v_message := 'Payment exceeds total';
ELSE v_message := 'OK' ;
v_is_valid := 'Y';
END IF;
:new.payment_id:= seq_payment_id.nextval;
:new.payment_date:= LOCALTIMESTAMP;
:new.payment_message := v_message;
:new.is_valid := v_is_valid;
END;
/

CREATE OR REPLACE TRIGGER checkPayments
BEFORE INSERT ON tbl_payments
FOR EACH ROW
DECLARE
v_message VARCHAR(100 CHAR);
v_is_valid CHAR(1) := 'N';
BEGIN
v_is_valid := 'Y';
IF getActiveUser(getOrderUsername(:new.order_id)) = 0
THEN v_message := 'Invalid account';
v_is_valid := 'N';
END IF;
IF isValidAmount(:new.payment_amount) = 0
THEN v_message := v_message || ' Invalid amount';
v_is_valid := 'N';
END IF;
IF checkOrderExist(:new.order_id) = 0
THEN v_message := v_message || ' Invalid order ID';
v_is_valid := 'N';
END IF;
IF (getValidOrderPayments(:new.order_id) + :new.payment_amount ) > getOrderTotal(:new.order_id)
THEN v_message := v_message || ' Payment exceeds total';
v_is_valid := 'N';
END IF;
IF v_is_valid = 'Y'
THEN v_message := 'OK' ;
END IF;
:new.payment_id:= seq_payment_id.nextval;
:new.payment_date:= LOCALTIMESTAMP;
:new.payment_message := trim(v_message);
:new.is_valid := v_is_valid;
END;
/

Related

ORA-06550: line 1, column 7 (PL/SQL: Statement ignored) Error

I am getting following error for the stored procedure and not able to understand the issue (must be from db side) While googling, I found similar issues but couldn't get the solution. Can any one help me please find the error in PROCEDURE ??
Error :-
18:58:50,281 ERROR [STDERR] java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SP_DIST_RETAILER_REMAP'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Stored Prodedure(SP_DIST_RETAILER_REMAP) :-
CREATE OR REPLACE PROCEDURE SMAPRD02.SP_DIST_RETAILER_REMAP (
i_old_dist_code IN VARCHAR2,
i_new_dist_code IN VARCHAR2,
i_territory_remapping IN NUMBER,
i_remapping_reason IN VARCHAR2,
i_trans_doneby_rolename IN VARCHAR2,
i_trans_doneby_id IN NUMBER,
i_trans_dist_rolename IN VARCHAR2,
i_trans_ret_rolename IN VARCHAR2,
i_activity_type IN VARCHAR2,
i_ret_list IN V_ARRAY,
result OUT VARCHAR2,
i_o_query OUT VARCHAR2
)
AS
--i_ret_codes OUT VARCHAR2;
v_dist_count NUMBER;
v_ret_count NUMBER;
v_ret_codes VARCHAR2(10000) := '';
v_flag VARCHAR2(10) := 'true';
v_trans_id NUMBER;
v_query VARCHAR2(10000);
BEGIN
IF i_territory_remapping = 1 then
SELECT count(*) into v_dist_count FROM tblemployee where EMPCODE = i_new_dist_code and circle_code = (select emp.circle_code
from tblemployee emp where emp.empcode = i_old_dist_code) and upper(user_type) like upper('%dist%') and upper(ACCESS_TO) in ('SALES','BOTH') and upper(stage) not in (upper('InActive'));
ELSE
SELECT count(*) into v_dist_count FROM tblemployee where EMPCODE = i_new_dist_code and circle_code = (select emp.circle_code from tblemployee emp
where emp.empcode = i_old_dist_code) and cluster_code = (select emp.cluster_code from tblemployee emp where emp.empcode = i_old_dist_code)
and upper(user_type) like upper('%dist%') and upper(ACCESS_TO) in ('SALES','BOTH') and upper(stage) not in (upper('InActive'));
END IF;
IF v_dist_count =0 THEN
result := 'invalid_new_dist_code';
v_flag := 'false';
ELSIF v_dist_count = 1 THEN
SELECT count(*) into v_ret_count FROM tblretailer t where t.DIST_CODE = i_old_dist_code and (upper(t.ACCESS_TO) = 'SALES' or upper(t.ACCESS_TO) = 'BOTH');
--SELECT count(*) into v_ret_count FROM tblretailer t where t.DIST_CODE = i_old_dist_code and upper(t.ACCESS_TO) = 'SALES' and upper(t.stage) in ('APPROVED','INACTIVE');
IF v_ret_count=i_ret_list.count THEN
IF i_territory_remapping = 1 THEN
result := 'no_ret_left';
v_flag := 'false';
END IF;
ELSE
IF i_territory_remapping != 1 THEN
result := 'ret_left';
v_flag := 'false';
END IF;
END IF;
END IF;
IF i_ret_list is null or i_ret_list.count = 0 THEN
result := 'empty retailers list';
v_flag := 'false';
END IF;
/*FOR i IN i_ret_list.FIRST .. i_ret_list.LAST
LOOP
IF v_ret_codes is null
THEN
v_ret_codes := ''''||i_ret_list(i)||'''';
ELSE
v_ret_codes := v_ret_codes||','''||i_ret_list(i)||'''';
END IF;
IF v_ret_codes is null
THEN
v_ret_codes := i_ret_list(i);
ELSE
v_ret_codes := v_ret_codes||','||i_ret_list(i);
END IF;
END LOOP;
i_ret_codes := v_ret_codes;
v_flag := 'false';
result := 'success';*/
IF v_flag = 'true' THEN
FOR i IN i_ret_list.FIRST .. i_ret_list.LAST
LOOP
IF v_ret_codes is null
THEN
v_ret_codes := ''''||i_ret_list(i)||'''';
ELSE
v_ret_codes := v_ret_codes||','''||i_ret_list(i)||'''';
END IF;
END LOOP;
--i_ret_codes := v_ret_codes;
--update tblretailer set dist_code=i_new_dist_code,DIST_ID=to_number(i_new_dist_code),cluster_code=(select cluster_code from tblemployee where empcode = i_new_dist_code),FOSID='',FOS_CODE='',DSR_ID='',DSR_CODE='',LAST_UPDATED_DATE=sysdate where retcode in (v_ret_codes);
v_query := 'update tblretailer set dist_code='||i_new_dist_code||',DIST_ID=to_number('||i_new_dist_code||'),cluster_code=(select cluster_code from tblemployee where empcode = '||i_new_dist_code||'),FOSID='''',FOS_CODE='''',DSR_ID='''',DSR_CODE='''',LAST_UPDATED_DATE=sysdate where retcode in ('||v_ret_codes||')';
execute immediate (v_query);
--i_query :='update tblretailer set dist_code='||i_new_dist_code||',DIST_ID=to_number('||i_new_dist_code||'),cluster_code=(select cluster_code from tblemployee where empcode = '||i_new_dist_code||'),FOSID='',FOS_CODE='',DSR_ID='',DSR_CODE='',LAST_UPDATED_DATE=sysdate where retcode in ('||v_ret_codes||');';
insert into TBL_TRANSFER_SUP_MASTER(MASTER_ID,TRANS_ID,TRANS_DONEBY_ROLENAME,TRANS_DONEBY_ID,TRANS_FROM_ROLENAME,TRANS_FROM,TRANS_TO_ROLENAME,TRANS_TO,ACTIVITY_CODE,TRANS_DATE,TRANSFER_REASON,LAST_UPDATED_DATE)
values(SUP_MASTER_TRANS_ID_SEQ.nextval,SUP_MASTER_TRANS_ID_SEQ.nextval,i_trans_doneby_rolename,i_trans_doneby_id,i_trans_dist_rolename,i_old_dist_code,i_trans_dist_rolename,i_new_dist_code,'101',sysdate,i_remapping_reason,sysdate) return TRANS_ID into v_trans_id;
FOR i IN i_ret_list.FIRST .. i_ret_list.LAST
LOOP
insert into TBL_TRANSFER_SUP_DTLS(DTLS_ID,TRANS_ID,TRANS_ON_ROLENAME,TRANS_ON_ID,LAST_UPDATED_DATE)
values(SUP_DTLS_ID_SEQ.nextval,v_trans_id,i_trans_ret_rolename,i_ret_list(i),sysdate);
END LOOP;
IF SQL%ROWCOUNT>0 THEN
result := 'success';
ELSE
result := 'failure';
END IF;
--update tblstock set NEW_DIST_CODE_REMAP=i_new_dist_code,REMAP_DATE=sysdate,LAST_UPDATED_DATE=sysdate where (DIST_CODE=i_old_dist_code or NEW_DIST_CODE_REMAP=i_old_dist_code) and RET_CODE in (v_ret_codes);
v_query := 'update tblstock set NEW_DIST_CODE_REMAP='||i_new_dist_code||',REMAP_DATE=sysdate,LAST_UPDATED_DATE=sysdate where (DIST_CODE='||i_old_dist_code||' or NEW_DIST_CODE_REMAP='||i_old_dist_code||') and RET_CODE in ('||v_ret_codes||')';
execute immediate (v_query);
i_o_query := v_query;
insert all into TBL_ACTIVITY_LOG (LOG_ID,TRANS_ID,ACTIVITY_DONEBY_ROLENAME,ACTIVITY_DONEBY_ID,ACTIVITY_REFERENCE_ID,ACTIVITY_CODE,ACTIVITY_DATE)
values(ACTIVITY_LOG_TRANS_ID_SEQ.NEXTVAL,ACTIVITY_LOG_TRANS_ID_SEQ.NEXTVAL,i_trans_doneby_rolename,i_trans_doneby_id,v_trans_id,
act_code,sysdate) select log_config.ACTIVITY_CODE act_code from TBL_ACTIVITY_LOG_CONFIG log_config
where upper(log_config.ACTIVITY_TYPE)= upper(i_activity_type);
END IF;
END;
/
Java Code :-
try{
if(ret_list.size()>0)
ret_code = ret_list.toArray();
con = ConnectionManager.getDirectConnection();
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(PropertyLoader.RET_SECONDARY_V_ARRAY,con);
ARRAY array_to_pass = new ARRAY( descriptor,con, ret_code );
cstmt = con.prepareCall("{ call SP_DIST_RETAILER_REMAP(?,?,?,?,?,?,?,?,?,?,?,?)}");
cstmt.setString(1,old_dist_code.trim());
cstmt.setString(2,new_dist_code.trim());
if(territory_remapping)
cstmt.setInt(3,1);
else
cstmt.setInt(3,2);
cstmt.setString(4,remapping_reason);
cstmt.setString(5,userVO.getRolename().trim());
cstmt.setInt(6,userVO.getEmpid());
cstmt.setString(7,PropertyLoader.DISTRIBUOTR_ROLENAME);
cstmt.setString(8,PropertyLoader.RETAILER_ROLENAME);
cstmt.setString(9,PropertyLoader.ACTIVITY_TYPES_RETAILER_REMAPPING);
cstmt.setArray(10,array_to_pass);
cstmt.registerOutParameter(11,Types.VARCHAR);
cstmt.registerOutParameter(12,Types.VARCHAR);
/*cstmt.registerOutParameter(13,Types.VARCHAR);*/
cstmt.execute();
status = cstmt.getString(11);
System.out.println("Remap Update Query "+cstmt.getString(12));
//System.out.println(cstmt.getString(13));
}
If the value stored in PropertyLoader.RET_SECONDARY_V_ARRAY is not "V_ARRAY", then you are using different types; even if they are declared identically (e.g. both are table of number) this will not work.
You're hitting this data type compatibility restriction:
You can assign a collection to a collection variable only if they have
the same data type. Having the same element type is not enough.
You're trying to call the procedure with a parameter that is a different type to the one it's expecting, which is what the error message is telling you.
The procedure which you are using is should be properly declared in the place which you are using it. For an example if it is under a user and in a package then it should be in that order. The username, package and procedurename.

PL/SQL CRUD matrix from a source code

I am trying to create a CRUD matrix for my function from a source code of that function, so I created a procedure that will read a source code.
create or replace procedure test_
IS
CURSOR c_text is
SELECT USER_SOURCE.TEXT
FROM USER_SOURCE
WHERE USER_SOURCE.name='TEST_FUNCTION'
AND USER_SOURCE.type='FUNCTION';
order by line;
v_single_text varchar2(4000);
v_tmp_text varchar2(10000) := ' ';
begin
open c_text;
loop
fetch c_text into v_single_text;
exit when c_text%notfound;
v_tmp_text := v_tmp_text|| chr(10) || rtrim(v_single_text);
dbms_output.put_line(v_single_text);
end loop;
close c_text;
end test_;
And that works very good for me, I get the source code of my desired function. It's a very simple function and I use this to learn PL/SQL. Output of that procedure look's like this.
function test_funkction Return varchar2
IS
kpp_value varchar2(20);
begin
select KPP
into kpp_value
from CUSTOMER
where CUSTOMER_ID = 200713;
dbms_output.put_line (kpp_value);
Return kpp_value;
end test_function;
Now, how to parse the string I've got in the output to get a desired result, my result should be like this
==TABLE_NAME==========OPERATIONS==
CUSTOMER - R - -
==================================
Now I have managed to do it.
But it will only work with my simple function, now I want to make a procedure that will work with any function.
Source code below.
create or replace procedure test_
IS
v_string_fnc varchar2(10000) := UPPER('function test_function
Return varchar2
IS
kpp_value varchar2(20);
begin
select KPP into kpp_value from CUSTOMER where CUSTOMER_ID = 200713;
dbms_output.put_line (kpp_value);
Return kpp_value;
end test_function;');
v_check PLS_INTEGER;
CURSOR c_text is
SELECT USER_SOURCE.TEXT
FROM USER_SOURCE
WHERE USER_SOURCE.name = 'TEST_FUNCTION'
AND USER_SOURCE.type = 'FUNCTION'
order by line;
v_single_text varchar2(4000);
v_tmp_text varchar2(10000) := ' ';
/*v_string varchar2(10000);*/
insert_flag char := '-';
read_flag char := '-';
update_flag char := '-';
delete_flag char := '-';
empty_space char(34) := ' ';
underline char(42) := '==========================================';
/*v_txt varchar2(10000) := ' ';*/
result_table varchar2(1000) := '/';
begin
open c_text;
loop
fetch c_text
into v_single_text;
exit when c_text%notfound;
v_tmp_text := v_tmp_text || chr(10) || rtrim(v_single_text);
/* print source code*/
/*dbms_output.put_line(v_single_text);*/
end loop;
close c_text;
/*DELETE SEARCH*/
v_check := instr(v_string_fnc, 'DELETE ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO DELETE COMMAND');
else
dbms_output.put_line('THERE IS A DELETE COMMAND');
delete_flag := 'D';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY DELETE: ' || result_table);
end if;
/*SELECT SEARCH*/
v_check := instr(v_string_fnc, 'SELECT ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO READ COMMAND');
else
dbms_output.put_line('THERE IS A READ COMMAND');
read_flag := 'R';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY READ: ' || result_table);
end if;
/*UPDATE SEARCH*/
v_check := instr(v_string_fnc, 'UPDATE ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO UPDATE COMMAND');
else
dbms_output.put_line('THERE IS A UPDATE COMMAND');
update_flag := 'U';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY UPDATE: ' || result_table);
end if;
/*INSERT SEARCH*/
v_check := instr(v_string_fnc, 'INSERT ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO CREATE COMMAND');
else
dbms_output.put_line('THERE IS A CREATE COMMAND');
insert_flag := 'C';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY CREATE: ' || result_table);
end if;
dbms_output.put_line(' ');
dbms_output.put_line('==========' || 'TABLE_NAME' || '==========' ||
'OPERATIONS' || '==');
dbms_output.put_line(empty_space || insert_flag || read_flag ||
update_flag || delete_flag);
dbms_output.put_line(underline);
end test_;
With that procedure I can extract and output my code, dbms needs a bit clean up but it will give the result I need.
Now a few questions, how to put a source code of my function to a variable that is not predefined, here is v_string_fnc but it needs to be predefined to work.
And how to link a certain operation with the table, here in my example is easy, one SELECT and keyword FROM that gives me a name of table.
Struggling continues
The bigger part it's done, just a tuning after this.
v_check := instr2(v_string_fnc, 'DROP ');
if v_check > 0 then
delete_flag := 'D';
v_check := instr2(v_string_fnc, 'TABLE ', v_check);
v_check := v_check + 6;
result_table := substr(v_string_fnc, v_check);
rest_string := result_table;
result_table := substr(result_table, 0, instr(result_table, ' '));
result_table := rtrim(result_table);
result_table := rtrim(result_table, ';');
merge into result_set
using dual
on (tables_used = result_table)
when matched then
update set drop_operation = delete_flag
when not matched then
insert
(tables_used, drop_operation)
values
(result_table, delete_flag);
while v_check > 0 loop
v_check := instr2(rest_string, 'DROP ');
if v_check > 0 then
delete_flag := 'D';
v_check := instr2(rest_string, 'TABLE ', v_check);
v_check := v_check + 6;
result_table := substr(rest_string, v_check);
rest_string := result_table;
result_table := substr(result_table, 0, instr(result_table, ' '));
result_table := rtrim(result_table);
result_table := rtrim(result_table, ';');
merge into result_set
using dual
on (tables_used = result_table)
when matched then
update set drop_operation = delete_flag
when not matched then
insert
(tables_used, drop_operation)
values
(result_table, delete_flag);
end if;
end loop;
end if;

Selection of columns in stored procedure based on parameters

I need to check the parameters in the stored procedure if it is entered then I need to select that
i=j=k=l=m=1;
IF (p_plant_cd IS NULL) THEN
i=0;
END IF;
IF(p_global_duns_nbr IS NULL) THEN
j=0
END IF;
IF(p_global_duns_nbr IS NULL) THEN
k=0
END IF;
IF(p_matrl_grp IS NULL) THEN
l=0
END IF;
IF (p_mrp IS NULL) THEN
m=0
END IF ;
Which ever value is 1 I need to add corresponding parameters in the variable v_select
For eg ;
if k and l are 1 then
v_select='p_global_duns_nbr,p_matrl_grp'
Pls suggest me how to do this.
You can declare a variable , holding column names , then concat to select statement and you have SQL select statement in v_select , use it as you want
declare
v_columns varchar2(255);
v_select varchar2(2000);
v_result sys_refcursor;
begin
v_columns := 'rownum';
IF p_plant_cd = 1 THEN
v_columns := v_columns || ',p_plant_cd';
END IF;
IF p_global_duns_nbr = 1 THEN
v_columns := v_columns || ',p_global_duns_nbr';
END IF;
IF p_global_duns_nbr = 1 THEN
v_columns := v_columns || ',p_global_duns_nbr';
END IF;
IF p_matrl_grp = 1 THEN
v_columns := v_columns || ',p_matrl_grp';
END IF;
IF p_mrp = 1 THEN
v_columns := v_columns || ',p_mrp';
END IF;
v_select := 'SELECT ' || v_columns || ' FROM table';
open v_result for v_select;
end;

How to write a FOR EACH loop in PL/SQL?

Is it possible to run a for each loop on a PL/SQL array?
for i in my_array.first ..my_array.last loop
--do_something with my_array(i);
end loop;
It's no possible to iterate over the associative arrays with a non numeric index with a FOR-loop. The solution below works just fine.
-- for-each key in (associative-array) loop ...
declare
type items_type is table of varchar2(32) index by varchar2(32);
items items_type;
begin
items('10') := 'item 10';
items('20') := 'item 20';
items('30') := 'item 30';
dbms_output.put_line('items=' || items.count);
<<for_each>> declare key varchar2(32); begin loop
key := case when key is null then items.first else items.next(key) end;
exit when key is null;
dbms_output.put_line('item(' || key || ')=' || items(key));
--do something with an item
end loop; end for_each;
end;
In my opinion 0xdb solution is best.
Even if you have numeric index it is better to us this construct
DECLARE
TYPE TTab_SomeTable IS TABLE OF VARCHAR2(2000) INDEX BY PLS_INTEGER;
--
vt_SomeTable TTab_SomeTable;
vi_Idx NUMBER;
BEGIN
vt_SomeTable(1) := 'First';
vt_SomeTable(2) := 'Second';
vt_SomeTable(5) := 'Fifth';
vt_SomeTable(10) := 'Tenth';
vi_Idx := vt_SomeTable.FIRST;
LOOP
--
EXIT WHEN vi_Idx IS NULL;
--
dbms_output.Put_Line('vt_SomeTable(' || vi_Idx || ') = ' || vt_SomeTable(vi_Idx));
--
vi_Idx := vt_SomeTable.NEXT(vi_Idx);
--
END LOOP vi_Idx;
END;
It is not susceptible to index discontinuity like below two examples, which will fail on index 3:
DECLARE
TYPE TTab_SomeTable IS TABLE OF VARCHAR2(2000) INDEX BY PLS_INTEGER;
--
vt_SomeTable TTab_SomeTable;
BEGIN
vt_SomeTable(1) := 'First';
vt_SomeTable(2) := 'Second';
vt_SomeTable(5) := 'Fifth';
vt_SomeTable(10) := 'Tenth';
-- Throw No_data_found on vi_Idx = 3
FOR vi_Idx IN vt_SomeTable.FIRST .. vt_SomeTable.LAST
LOOP
dbms_output.Put_Line('vt_SomeTable(' || vi_Idx || ') = ' || vt_SomeTable(vi_Idx));
END LOOP vi_Idx;
END;
DECLARE
TYPE TTab_SomeTable IS TABLE OF VARCHAR2(2000) INDEX BY PLS_INTEGER;
--
vt_SomeTable TTab_SomeTable;
BEGIN
vt_SomeTable(1) := 'First';
vt_SomeTable(2) := 'Second';
vt_SomeTable(5) := 'Fifth';
vt_SomeTable(10) := 'Tenth';
-- Throw No_data_found on vi_Idx = 3.
FOR vi_Idx IN 1 .. vt_SomeTable.COUNT
LOOP
dbms_output.Put_Line('vt_SomeTable(' || vi_Idx || ') = ' || vt_SomeTable(vi_Idx));
END LOOP vi_Idx;
END;

Encountered the symbol "end-of-file"

I have written foll query but getting error:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
:= . ( # % ;
Following is the stored procedure query
I know its the syntax error but cannot figure out where exactly the problem is.
It will be a great help if someone help me.
CREATE OR REPLACE PROCEDURE MCSIL_HR.sp_ins_emp_mst_DEV (
inemp_id emp_mst.emp_id%TYPE,
inemp_restrict_emp emp_mst.restrict_emp%TYPE,
inemp_name emp_mst.emp_name%TYPE,
inemp_email emp_mst.email_id%TYPE,
inemp_pwd emp_mst.emp_pwd%TYPE,
indob VARCHAR2,
indoj VARCHAR2,
indor VARCHAR2,
inemp_status_lkp_id emp_mst.emp_status_lkp_id%TYPE,
inemp_has_reportee_status emp_mst.has_reportees_status_yn%TYPE,
inupd_emp_id emp_mst.emp_id%TYPE,
inupd_flag CHAR,
indept VARCHAR2,
inmgr_id emp_mst.mgr_id%TYPE,
outmsg OUT VARCHAR2,
outstatus OUT VARCHAR2
)
IS
pgm_error EXCEPTION;
as_emp_id emp_mst.emp_id%TYPE;
as_cnt NUMBER;
upd_flag VARCHAR2 (3);
asdob DATE;
asdoj DATE;
asdor DATE;
BEGIN
outmsg := '';
outstatus := 'TRUE';
upd_flag := UPPER (TRIM (inupd_flag));
-- check if Emp Status lookup value exists in lookup table , if not raise error
SELECT COUNT (1)
INTO as_cnt
FROM lookup l
WHERE l.lkp_id = inemp_status_lkp_id;
IF as_cnt = 0
THEN
outmsg :=
'Employee Sattus LOOK UP VALUE : '
|| TO_CHAR (inemp_status_lkp_id)
|| ' NOT EXISTS IN LOOKUP TABLE ';
outstatus := 'INSERT/UPDATE/DELETE Failed.';
RAISE pgm_error;
END IF;
-- check if Yes/No Status ookup value exists in lookup table , if not raise error
SELECT COUNT (1)
INTO as_cnt
FROM lookup l
WHERE l.lkp_id = inemp_has_reportee_status;
IF as_cnt = 0
THEN
outmsg :=
'Has_Reportees Status LOOK UP VALUE : '
|| TO_CHAR (inemp_has_reportee_status)
|| ' NOT EXISTS IN LOOKUP TABLE ';
outstatus := 'INSERT/UPDATE/DELETE Failed.';
RAISE pgm_error;
END IF;
-- check whether user modifying data is valid employee
SELECT COUNT (1)
INTO as_cnt
FROM emp_mst e
WHERE e.emp_id = inupd_emp_id;
IF as_cnt = 0
THEN
outmsg :=
'UPDATING USER ID: '
|| TO_CHAR (inupd_emp_id)
|| ' NOT EXISTS IN EMPLOYEE MASTER ';
outstatus := 'INSERT/UPDATE/DELETE Failed.';
RAISE pgm_error;
END IF;
-- checking date formatt (dd-mm-yyyy)
IF indob IS NULL
OR NOT ( SUBSTR (TRIM (indob), 3, 1) = '-'
AND SUBSTR (TRIM (indob), 6, 1) = '-'
)
THEN
outmsg :=
'Date of Birth is Blank or Formatt for Date of Birth is not in DD-MM-YYYY, Contact Administrator';
outstatus := 'INSERT/UPDATE/DELETE Failed.';
RAISE pgm_error;
END IF;
IF indoj IS NULL
OR NOT ( SUBSTR (TRIM (indoj), 3, 1) = '-'
AND SUBSTR (TRIM (indoj), 6, 1) = '-'
)
THEN
outmsg :=
'Date of Join is Blank or Formatt for Date of Join is not in DD-MM-YYYY, Contact Administrator';
outstatus := 'INSERT/UPDATE/DELETE Failed.';
RAISE pgm_error;
END IF;
IF indor IS NOT NULL
AND NOT ( SUBSTR (TRIM (indor), 3, 1) = '-'
AND SUBSTR (TRIM (indor), 6, 1) = '-'
)
THEN
outmsg :=
'Formatt for Date of Resign is not in DD-MM-YYYY, Contact Administrator';
outstatus := 'INSERT/UPDATE/DELETE Failed.';
RAISE pgm_error;
END IF;
asdob := TO_DATE (TRIM (indob), 'DD-MM-YYYY');
asdoj := TO_DATE (TRIM (indoj), 'DD-MM-YYYY');
asdor := TO_DATE (TRIM (indor), 'DD-MM-YYYY');
DBMS_OUTPUT.put_line ( 'LKP SRNO, UPDATE USER EMPCODE OK,'
|| 'DOB :'
|| TO_CHAR (asdob)
|| ',DOJ :'
|| TO_CHAR (asdoj)
|| ',DOR :'
|| TO_CHAR (asdor)
);
-- check whether user modifying data is valid employee
SELECT COUNT (*)
INTO as_cnt
FROM emp_mst e
WHERE e.emp_id = inemp_id;
IF upd_flag = 'U'
THEN
IF as_cnt = 0
THEN
outmsg :=
'EMPLOYEE CODE : '
|| TO_CHAR (inemp_id)
|| ' NOT EXISTS IN EMPLOYEE MASTER';
outstatus := 'UPDATE Failed.';
RAISE pgm_error;
END IF;
UPDATE emp_mst a
SET a.rec_dtl = rec_dtl (NULL, NULL, NULL, NULL)
WHERE a.rec_dtl IS NULL;
-- For password change call another procedure
UPDATE emp_mst a
SET a.emp_name = inemp_name,
a.restrict_emp = inemp_restrict_emp,
a.email_id=inemp_email,
a.emp_pwd = ENCRYPT(inemp_pwd),
a.dob = asdob,
a.doj = asdoj,
a.dor = asdor,
a.emp_status_lkp_id = inemp_status_lkp_id,
a.has_reportees_status_yn = inemp_has_reportee_status,
a.rec_dtl.updated_by = inupd_emp_id,
a.DEPT= indept,
a.MGR_ID = inmgr_id,
a.rec_dtl.updated_on = SYSDATE
WHERE a.emp_id = inemp_id;
IF SQLCODE <> 0
THEN
ROLLBACK;
outstatus := 'FALSE';
outmsg := 'UPDATE Failed.';
RAISE pgm_error;
ELSE
COMMIT;
outstatus := 'TRUE';
outmsg := 'Process sucessfully executed';
END IF;
ELSIF upd_flag = 'I'
THEN
IF as_cnt > 0
THEN
outmsg :=
'EMPLOYEE ID : '
|| TO_CHAR (inemp_id)
|| ' ALREADY IN EMPLOYEE MASTER';
outstatus := 'INSERT Failed.';
RAISE pgm_error;
END IF;
INSERT INTO mcsil_hr.emp_mst
(emp_id,restrict_emp, emp_name,email_id, emp_pwd, dob, doj, dor, ---added by shailesh
emp_status_lkp_id, has_reportees_status_yn,
rec_dtl,DEPT,MGR_ID
)
VALUES (inemp_id,inemp_restrict_emp, inemp_name, inemp_email, ENCRYPT(inemp_pwd), asdob, asdoj, asdor, ---added by shailesh
inemp_status_lkp_id, inemp_has_reportee_status,
rec_dtl (inupd_emp_id, SYSDATE, NULL, NULL),indept,inmgr_id
);
IF SQLCODE <> 0
THEN
ROLLBACK;
outstatus := 'FALSE';
outmsg := 'INSERT Failed.';
RAISE pgm_error;
ELSE
COMMIT;
outstatus := 'TRUE';
outmsg := 'Process Sucessfully executed';
END IF;
ELSIF upd_flag = 'D'
THEN
IF as_cnt = 0
THEN
outmsg :=
'EMPLOYEE ID : '
|| TO_CHAR (inemp_id)
|| ' NOT EXISTS IN EMPLOYEE MASTER';
outstatus := 'DELETE Failed.';
RAISE pgm_error;
END IF;
UPDATE emp_mst a
SET a.rec_dtl = rec_dtl (NULL, NULL, NULL, NULL)
WHERE a.rec_dtl IS NULL;
UPDATE emp_mst a
SET a.emp_status_lkp_id = 202, -- INCATIVE ACCOUNT CODE
a.rec_dtl.updated_by = inupd_emp_id,
a.rec_dtl.updated_on = SYSDATE
WHERE a.emp_id = inemp_id;
IF SQLCODE <> 0
THEN
ROLLBACK;
outstatus := 'FALSE';
outmsg := 'DELTE Failed.';
RAISE pgm_error;
ELSE
COMMIT;
outstatus := 'TRUE';
outmsg := 'Process sucessfully executed';
END IF;
ELSE
outstatus := 'FALSE';
outmsg := 'ACTION FLAG SHOULD BE IN I(Insert)/U(Update)/D(Delete).';
RAISE pgm_error;
END IF;
DBMS_OUTPUT.put_line ('STATUS :' || outstatus || ', MGS :' || outmsg);
EXCEPTION
WHEN pgm_error
THEN
BEGIN
outstatus := 'FALSE';
DBMS_OUTPUT.put_line ('STATUS :' || outstatus || ', MGS :' || outmsg);
raise_application_error (-20999, outmsg);
END;
WHEN NO_DATA_FOUND
THEN
BEGIN
outmsg := 'INSERT/UPDATE/DELETE FAILED. NO DATA FOUND';
outstatus := 'FALSE';
DBMS_OUTPUT.put_line ('STATUS :' || outstatus || ', MGS :' || outmsg);
raise_application_error (-20999, outmsg);
END;
WHEN OTHERS
THEN
BEGIN
outmsg := 'INSERT/UPDATE/DELETE FAILED. ';
outstatus := 'FALSE';
DBMS_OUTPUT.put_line ('STATUS :' || outstatus || ', MGS :' || outmsg);
raise_application_error (-20999, outmsg || SQLERRM);
END;
END sp_ins_emp_mst_DEV ;
/
Are you using SQL*Plus? If you are, then it's probably the blank lines in your code, which would be interpreted as the end of a line.
I would suggest you do this:
set echo on
spool sp_ins_emp_mst_dev.log
<your procedure declaration>
spool off
And look very carefully at your line numbers. If they don't run smoothly from the beginning to the end, try making the blank lines into comments (my choice) or set sqlblanklines on.