How to execute a PL/SQL table statements - plsql

Dear friends i have these statements stored in a table and i need to execute them to receive the result.
DECODE (to_char((SELECT s1.streettypename FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||''|| to_char((SELECT s0.streetname FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||''|| to_char(NRO_VIA) ||''|| to_char(NRO_PUERTA),'', 'No Disponible', to_char((SELECT s1.streettypename FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||' '|| to_char((SELECT s0.streetname FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||' '|| to_char(NRO_VIA) ||' '|| to_char(NRO_PUERTA))',DECODE (to_char((SELECT s1.streettypename FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||''|| to_char((SELECT s0.streetname FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||''|| to_char(NRO_VIA) ||''|| to_char(NRO_PUERTA),'', 'No Disponible', to_char((SELECT s1.streettypename FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||' '|| to_char((SELECT s0.streetname FROM autodesk.smstreets s0, autodesk.smstreettypes s1 where s0.streettypeid = s1.streettypeid(+) AND s0.streetantiq=0 AND s0.streetid=COD_VIA)) ||' '|| to_char(NRO_VIA) ||' '|| to_char(NRO_PUERTA)) FROM cliente WHERE NRO_CLIENTE='104631'
DECODE (observaciones, NULL, 'No Disponible', '', 'No Disponible', observaciones)',DECODE (observaciones, NULL, 'No Disponible', '', 'No Disponible', observaciones) FROM cliente WHERE NRO_CLIENTE='104631'
to_char((SELECT areaname FROM autodesk.amareas a0 WHERE a0.areaid=COD_DISTRITO))',to_char((SELECT areaname FROM autodesk.amareas a0 WHERE a0.areaid=COD_DISTRITO)) FROM cliente WHERE NRO_CLIENTE='104631'
to_char((SELECT areaname FROM autodesk.amareas a0 WHERE a0.areaid=COD_PROVINCIA))',to_char((SELECT areaname FROM autodesk.amareas a0 WHERE a0.areaid=COD_PROVINCIA)) FROM cliente WHERE NRO_CLIENTE='104631'
to_char(correlativo)',to_char(correlativo) FROM cliente WHERE NRO_CLIENTE='104631'
sed',sed FROM cliente WHERE NRO_CLIENTE='104631'
to_char(telefono)',to_char(telefono) FROM s_v_cliente WHERE NRO_CLIENTE='104631'
to_char(numero_medidor)',to_char(numero_medidor) FROM tps.s_v_medid, cliente c WHERE NRO_CLIENTE='104631'
to_char(d.Consumo)',to_char(d.Consumo) FROM cliente c,(SELECT Id_Cuenta, Consumo FROM (SELECT a.Id_Cuenta, b.ENERGIA_FP Consumo FROM s_NUC_SERVICIO a, s_his_consumo b WHERE UPPER(a.tipo_Servicio) = 'ELECTRICO' AND a.id_servicio=b.id_servicio AND a.Id_Cuenta= %ID_ELEMENTO% ORDER BY fecha_lectura DESC) WHERE ROWNUM <=1) d WHERE NRO_CLIENTE='104631'
to_char(d.Potencia)',to_char(d.Potencia) FROM cliente c,(SELECT Id_Cuenta, Potencia FROM (SELECT a.Id_Cuenta, (SELECT potencia_cont_fp FROM s_v_cliente WHERE numero_cliente = a.Id_Cuenta) Potencia FROM s_NUC_SERVICIO a, s_his_consumo b WHERE UPPER(a.tipo_Servicio) = 'ELECTRICO' AND a.id_servicio = b.id_servicio AND a.Id_Cuenta = %ID_ELEMENTO% ORDER BY fecha_lectura DESC) WHERE ROWNUM <=1) d WHERE NRO_CLIENTE='104631'
fase',fase FROM cliente WHERE NRO_CLIENTE='104631'
to_char(NRO_CLIENTE)',to_char(NRO_CLIENTE) FROM cliente WHERE NRO_CLIENTE='104631'
nombre',nombre FROM cliente WHERE NRO_CLIENTE='104631'
to_char(pcr)',to_char(pcr) FROM cliente WHERE NRO_CLIENTE='104631'
TO_CHAR(DECODE(TIPO,1,'NORMAL',2,'MAXIMETRO',4,'PIMT',7,'HH.UU. Y PP.JJ',8,'ALUMBRADO PUBLICO','No Disponible'))',TO_CHAR(DECODE(TIPO,1,'NORMAL',2,'MAXIMETRO',4,'PIMT',7,'HH.UU. Y PP.JJ',8,'ALUMBRADO PUBLICO','No Disponible')) FROM cliente WHERE NRO_CLIENTE='104631'
TO_CHAR(DECODE(estado,0,'ACTIVO',1,'ELIMINADO',2,'RETIRADO',3,'NUEVO',4,'PROCESO DE RETIRO','No Disponible'))',TO_CHAR(DECODE(estado,0,'ACTIVO',1,'ELIMINADO',2,'RETIRADO',3,'NUEVO',4,'PROCESO DE RETIRO','No Disponible')) FROM cliente WHERE NRO_CLIENTE='104631'
cadena',cadena FROM cliente WHERE NRO_CLIENTE='104631'
to_char(sector)',to_char(sector) FROM cliente WHERE NRO_CLIENTE='104631'
to_char(zona)',to_char(zona) FROM cliente WHERE NRO_CLIENTE='104631'

If, as it appears, these are all select statements that return 1 row of 2 columns (minus the keyword select at the start, and with syntax errors in many) then you could do this:
declare
l_sql long;
l_col1 long;
l_col2 long;
begin
for r_sql in (select statement from my_table) loop
dbms_output.put_line('Statement: '||r_sql.statement);
l_sql := 'select '||r_sql.statement;
begin
execute immediate l_sql into l_col1, l_col2;
dbms_output.put_line('returned: '||l_col1||','||l_col2);
exception
when no_data_found then
-- take appropriate action
when too_many_rows then ...
-- take appropriate action
end;
end loop;
end;
But that will only work when the syntax errors in the data have been fixed - e.g.
to_char(NRO_CLIENTE)',to_char(NRO_CLIENTE) FROM cliente WHERE NRO_CLIENTE='104631'
... has a spurious ' after to_char(NRO_CLIENTE) that will cause the statement to fail when run.

Related

Displaying table data through plsql procedure

I am trying to display the table info through this plsql code but when it executes the procedure it just show procedure successful. not the output.please help.
CREATE OR REPLACE PROCEDURE CarInfo
AS
V_serial CAR.serial%TYPE;
V_Cid CAR.cid%TYPE;
V_make CAR.make%TYPE ;
V_model CAR.model%TYPE;
V_cyear CAR.cyear%TYPE;
V_color CAR.color%TYPE;
V_trim CAR.trim%TYPE;
V_enginetype CAR.enginetype%TYPE;
V_purchinv CAR.purchinv%TYPE;
V_purchdate CAR.purchdate%TYPE;
V_purchfrom CAR.purchfrom%TYPE;
V_purchcost CAR.purchcost%TYPE;
V_freightcost CAR.freightcost%TYPE;
V_totalcost CAR.totalcost%TYPE;
V_listprice CAR.listprice%TYPE;
BEGIN
SELECT serial, cid, make, model, cyear, color, trim, enginetype, purchinv, purchdate, purchfrom , purchcost, freightcost,
totalcost, listprice
INTO V_serial, V_cid, V_make, V_model, V_cyear, V_color, V_trim, V_enginetype, V_purchinv, V_purchdate, V_purchfrom , V_purchcost, V_freightcost,
V_totalcost, V_listprice
FROM CAR
where cid is null;
Exception
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No Data Found') ;
DBMS_OUTPUT.PUT_LINE(V_serial||' ' || V_cid||' ' ||V_make||' ' ||V_model||' ' ||V_cyear||' ' ||V_color||' ' ||V_trim||' ' ||V_enginetype||' ' ||V_purchinv||' ' ||V_purchdate||' ' ||V_purchfrom ||' ' ||V_purchcost||' ' ||V_freightcost||' ' ||
V_totalcost||' ' ||V_listprice);
END;
In brief, I would write your procedure the following way:
CREATE OR REPLACE PROCEDURE CarInfo
AS
l_result_clob clob;
BEGIN
SELECT serial ||' ' || cid ||' ' ||make ||' ' ||model ||' ' ||cyear||' ' ||
color ||' ' ||trim ||' ' ||enginetype||' ' ||purchinv ||' ' ||
purchdate||' ' ||purchfrom ||' ' ||purchcost ||' ' ||freightcost||' ' ||totalcost||' ' ||listprice
INTO l_result_clob
FROM CAR
where cid is null;
DBMS_OUTPUT.PUT_LINE(l_result_clob);
Exception
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No Data Found') ;
WHEN OTHERS THEN
dbms_output.put_line('SQLCODE: ' || SQLCODE) ;
END CarInfo;
That is of course, if all the columns in the SELECT statement are actually convertible to character/clob, as Oracle would implicitly convert them, in order for the concatenation to happen.
Cheers

PL/SQL code coming with compilation error

I have the following block of code. Can someone help me in rectifying the same.
CREATE OR REPLACE package body NMS.ResourceBindDistribute_withfile is
v_date date :=sysdate ;
v_user varchar2(10) := 'NMS';
procedure create_SIMDMLNK_new(v_SIMRANGE varchar2 , v_MSISDN varchar2,v_TECH varchar2) is
v_SIM_count integer;
v_dlink_dn_cnt integer;
v_dlink_dn integer;
v_counter integer;
v_new_dn_cnt integer;
v_free_count integer := 0;
sm_count integer;
ms_count integer;
cursor sim_curr is
select sim from temp_SIM_MSISDN;
cursor msisdn_curr is
select msidn from temp_SIM_MSISDN;
/* Cursor to selecet SIM for linking*/
cursor simcusrsor_3G (l_SIMRANGE varchar2) is
select sm_id,SM_SERIALNUM from storage_medium where SM_SERIALNUM in
(select SM_SERIALNUM from storage_medium
where SM_SERIALNUM = l_SIMRANGE and SM_STATUS='r' and SMC_ID in (1,2)
minus
(select SM_SERIALNUM from NMS.SIMDMSLNK where SM_SERIALNUM = l_SIMRANGE
union
select SM_SERIALNUM from NMS.SIMULNK where SM_SERIALNUM = l_SIMRANGE));
cursor simcusrsor_4G (l_SIMRANGE varchar2) is
select sm_id,SM_SERIALNUM from storage_medium where SM_SERIALNUM in
(select SM_SERIALNUM from storage_medium
where SM_SERIALNUM = l_SIMRANGE and SM_STATUS='r' and SMC_ID in (3)
minus
(select SM_SERIALNUM from NMS. SIMDMSLNK where SM_SERIALNUM = l_SIMRANGE
union
select SM_SERIALNUM from NMS.SIMULNK where SM_SERIALNUM = l_SIMRANGE));
type t_simcursor is table of simcusrsor_3G%rowtype index by PLS_INTEGER;
v_simsoure t_simcursor;
/* Cursor to select Dlinked MSISDN for linking with New SIM*/
cursor dlinkdn_cur (l_MSISDN varchar2) is
select distinct DN_ID,DN_NUM from NMS.SIMDMSLNK where DN_NUM = l_MSISDN and status='D';
type t_dlinkdn_cur is table of dlinkdn_cur %rowtype index by PLS_INTEGER;
v_dlinkdn_cur_source t_dlinkdn_cur ;
/* Cursor to select new Dummy MSISDN for linking with New SIM*/
cursor dummydn_cur (l_MSISDN varchar2) is
select distinct DN_ID, DN_NUM from directory_number where DN_NUM = l_MSISDN
and dn_status = 'f' and evcode is null and rscode=2 and DN_TYPE=3
minus
select distinct DN_ID,DN_NUM from NMS.SIMDMSLNK where DN_NUM = l_MSISDN;
type t_dummydn_cur is table of dummydn_cur%rowtype index by PLS_INTEGER;
v_dummydn_source t_dummydn_cur;
type t_SIMDMSLNK is table of NMS.SIMDMSLNK%rowtype index by PLS_INTEGER;
v_SIMDM_target t_SIMDMSLNK;
v_simdml_count PLS_INTEGER :=1;
NO_SIM_WITH_RANGE exception;
NO_DUMMY_DN_WITH_RANGE exception;
BEGIN
select count(1) into v_new_dn_cnt from
(select distinct DN_NUM from directory_number where DN_NUM = v_MSISDN
and dn_status = 'f' and evcode is null and rscode=2 and DN_TYPE=3
minus
select distinct DN_NUM from NMS.SIMDMSLNK where DN_NUM = v_MSISDN );
select count(distinct DN_NUM) into v_dlink_dn from NMS.SIMDMSLNK where DN_NUM = v_MSISDN and status='D';
if ( v_dlink_dn =0 and v_new_dn_cnt = 0) then
raise NO_DUMMY_DN_WITH_RANGE;
end if;
v_free_count := v_new_dn_cnt + v_dlink_dn;
/* Cusrsor opened Dlinked MSISDN, to assign first DLINKED MSISDN */
BEGIN
open msisdn_curr;
for i in msisdn_curr
LOOP
select count(*) into ms_count from directory_number where dn_num = i.msisdn;
if
ms_count > 0
then
open dlinkdn_cur (v_MSISDN);
fetch dlinkdn_cur bulk collect into v_dlinkdn_cur_source;
IF v_dlinkdn_cur_source.count = 0 THEN
RAISE NO_DATA_FOUND;
ELSE
v_dlink_dn_cnt := v_dlinkdn_cur_source.count;
DBMS_OUTPUT.PUT_LINE ( 'FIRST D-LINKED MSISDN ARE LINKED TO SIM.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ( 'NO FREE D-LINKED MSISDN ARE AVAILABLE, FREE DUMMY MSISDN WILL BE USED FOR ASSOSOIATION.');
END IF;
END LOOP;
END;
open sim_curr
for i in sim_curr
LOOP
select count(*) into sm_count from storage_medium where sm_serialnum=i.sim;
if
sm_count > 0
then
IF v_TECH = 3 THEN
open simcusrsor_3G (v_SIMRANGE);
fetch simcusrsor_3G bulk collect into v_simsoure;
END IF;
IF v_TECH = 4 THEN
open simcusrsor_4G (v_SIMRANGE);
fetch simcusrsor_4G bulk collect into v_simsoure;
END IF;
v_SIM_count := v_simsoure.count;
IF v_SIM_count = 0 then
raise NO_SIM_WITH_RANGE;
END IF;
END IF;
END LOOP;
/* Cusrsor opened NEW DUMMY MSISDN, to assign NEW DUMMY MSISDN */
BEGIN
open msisdn_curr;
for i in msisdn_curr
LOOP
select count(*) into ms_count from directory_number where dn_num = i.msisdn;
if
ms_count > 0
then
open dummydn_cur (v_MSISDN);
fetch dummydn_cur bulk collect into v_dummydn_source;
IF v_dummydn_source.count = 0 THEN
RAISE NO_DATA_FOUND;
ELSE
DBMS_OUTPUT.PUT_LINE ( 'NEW DUMMY MSISDN ARE LINKED TO SIM.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ( 'NO NEW DUMMY MSISDN IS AVAILABL FOR ASSOSOIATION.');
END IF;
END LOOP;
END;
v_counter :=1;
for l_sim_counter in v_simsoure.first .. v_simsoure.last
loop
if l_sim_counter = ( v_free_count) then
if l_sim_counter <= v_dlink_dn_cnt then
v_SIMDM_target(v_simdml_count).DN_ID := v_dlinkdn_cur_source(l_sim_counter).DN_ID;
v_SIMDM_target(v_simdml_count).DN_NUM := v_dlinkdn_cur_source(l_sim_counter).DN_NUM;
DELETE from NMS.SIMDMSLNK where DN_ID=v_dlinkdn_cur_source(l_sim_counter).DN_ID and STATUS='D';
else
v_SIMDM_target(v_simdml_count).DN_ID := v_dummydn_source(v_counter).DN_ID;
v_SIMDM_target(v_simdml_count).DN_NUM := v_dummydn_source(v_counter).DN_NUM;
v_counter := v_counter +1 ;
end if;
v_SIMDM_target(v_simdml_count).SM_SERIALNUM := v_simsoure(l_sim_counter).SM_SERIALNUM;
v_SIMDM_target(v_simdml_count).SM_ID := v_simsoure(l_sim_counter).SM_ID;
v_SIMDM_target(v_simdml_count).USER_LASTMOD := v_user;
v_SIMDM_target(v_simdml_count).LAST_UPDATE := v_date;
v_SIMDM_target(v_simdml_count).STATUS := 'L';
exit;
end if;
if l_sim_counter <= v_dlink_dn_cnt then
v_SIMDM_target(v_simdml_count).DN_ID := v_dlinkdn_cur_source(l_sim_counter).DN_ID;
v_SIMDM_target(v_simdml_count).DN_NUM := v_dlinkdn_cur_source(l_sim_counter).DN_NUM;
DELETE from NMS.SIMDMSLNK where DN_ID=v_dlinkdn_cur_source(l_sim_counter).DN_ID and STATUS='D';
else
v_SIMDM_target(v_simdml_count).DN_ID := v_dummydn_source(v_counter).DN_ID;
v_SIMDM_target(v_simdml_count).DN_NUM := v_dummydn_source(v_counter).DN_NUM;
v_counter := v_counter +1 ;
end if;
v_SIMDM_target(v_simdml_count).SM_SERIALNUM := v_simsoure(l_sim_counter).SM_SERIALNUM;
v_SIMDM_target(v_simdml_count).SM_ID := v_simsoure(l_sim_counter).SM_ID;
v_SIMDM_target(v_simdml_count).USER_LASTMOD := v_user;
v_SIMDM_target(v_simdml_count).LAST_UPDATE := v_date;
v_SIMDM_target(v_simdml_count).STATUS := 'L';
v_simdml_count := v_simdml_count+1;
end loop;
FORALL i IN v_SIMDM_target.FIRST .. v_SIMDM_target.LAST SAVE EXCEPTIONS
INSERT INTO NMS.SIMDMSLNK
VALUES v_SIMDM_target (i);
COMMIT;
close dlinkdn_cur;
close dummydn_cur;
IF v_TECH =3 THEN
close simcusrsor_3G;
ELSE
close simcusrsor_4G;
END IF;
DBMS_OUTPUT.PUT_LINE ( 'SIM & DUMMY MSISDN LINKING PROCESS IS COMPLETED SUCCESSFULLY.');
EXCEPTION
WHEN NO_SIM_WITH_RANGE THEN
DBMS_OUTPUT.PUT_LINE ( 'THERE ARE NO SIMS AVAILABLE FOR TECHNOLOGY WITH GIVEN RANGE = '||v_SIMRANGE);
WHEN NO_DUMMY_DN_WITH_RANGE THEN
DBMS_OUTPUT.PUT_LINE ( 'THERE ARE NO DUMMY MSISDN AVAILABLE FOR GIVEN RANGE = '||v_MSISDN);
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('SQLCODE : ' || SQLCODE);
DBMS_OUTPUT.put_line ('ERROR MESSAGE : ' || SQLERRM);
ROLLBACK;
END create_SIMDMLNK;
procedure create_REALDN (v_MSISDN varchar2) is
v_DN_count integer;
v_count integer;
NO_DN_WITH_RANGE exception;
DN_WITH_PREPROVISION exception;
BEGIN
select count(DN_ID) into v_DN_count from NMS.SIMDMSLNK
where DN_NUM = v_MSISDN;
if v_DN_count > 0 then
raise DN_WITH_PREPROVISION;
end if;
select count(1) into v_count from
(select DN_ID from DIRECTORY_NUMBER
where DN_NUM = v_MSISDN
MINUS
select DN_ID from NMS.REALDN
where REAL_DN like v_MSISDN||'%');
if v_count = 0 then
raise NO_DN_WITH_RANGE;
end if;
insert into NMS.REALDN (DN_ID,REAL_DN,DN_TYPE,USER_LASTMOD,LAST_UPDATE,STATUS )
select DN_ID,DN_NUM ,DN_TYPE,v_user,v_date,'L' from Directory_number where DN_ID in
((select DN_ID from DIRECTORY_NUMBER
where DN_NUM = v_MSISDN and dn_type not in (1,2) and evcode is null
MINUS
select DN_ID from NMS.REALDN
where REAL_DN = v_MSISDN));
COMMIT;
DBMS_OUTPUT.PUT_LINE ( 'REAL DIRECTORY NUMBER CREATION PROCESS IS COMPLETED SUCCESSFULLY.');
EXCEPTION
WHEN DN_WITH_PREPROVISION THEN
DBMS_OUTPUT.PUT_LINE ( 'MSISDN RANGE IS WITH PREPROVISION POOL ' || v_MSISDN);
WHEN NO_DN_WITH_RANGE THEN
DBMS_OUTPUT.PUT_LINE ( 'NO DN AVAILABLE WITH RANGE ' || v_MSISDN);
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('SQLCODE : ' || SQLCODE);
DBMS_OUTPUT.put_line ('ERROR MESSAGE : ' || SQLERRM);
ROLLBACK;
END create_REALDN;
procedure create_FREESIM (v_SIMRANGE varchar2) is
v_SIM_count integer;
v_count integer;
NO_SIM_WITH_RANGE exception;
BEGIN
select count(1) into v_count from
(select SM_SERIALNUM from storage_medium
where SM_SERIALNUM = v_SIMRANGE and SM_STATUS='r' and SMC_ID in (3)
minus
(select SM_SERIALNUM from NMS. SIMDMSLNK where SM_SERIALNUM = v_SIMRANGE
union
select SM_SERIALNUM from NMS.SIMULNK where SM_SERIALNUM = v_SIMRANGE));
if v_count = 0 then
raise NO_SIM_WITH_RANGE;
end if;
insert into NMS.SIMULNK (SM_SERIALNUM,SM_ID, USER_LASTMOD, LAST_UPDATE, STATUS)
select SM_SERIALNUM,sm_id,v_user,v_date,'U' from storage_medium where SM_SERIALNUM in
(select SM_SERIALNUM from storage_medium
where SM_SERIALNUM = v_SIMRANGE and SM_STATUS='r' and SMC_ID in (3)
minus
(select SM_SERIALNUM from NMS. SIMDMSLNK where SM_SERIALNUM = v_SIMRANGE
union
select SM_SERIALNUM from NMS.SIMULNK where SM_SERIALNUM = v_SIMRANGE));
COMMIT;
DBMS_OUTPUT.PUT_LINE ( 'FREE 4G SIM CREATION PROCESS IS COMPLETED SUCCESSFULLY.');
EXCEPTION
WHEN NO_SIM_WITH_RANGE THEN
DBMS_OUTPUT.PUT_LINE ( 'SIM NOT AVAILABLE WITH RANGE ' ||v_SIMRANGE ||'TO INCLUDE IN FREE SIM POOL.');
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('SQLCODE : ' || SQLCODE);
DBMS_OUTPUT.put_line ('ERROR MESSAGE : ' || SQLERRM);
ROLLBACK;
END create_FREESIM;
procedure dummy_dlink is
v_count integer;
NO_ACTIVE exception;
begin
select count(distinct cd.CD_SM_NUM) into v_count from CONTR_DEVICES cd, contract_all coa , NMS.SIMDMSLNK SIMD
where coa.CH_STATUS='a' and COA.CO_ID=cd.co_id and SIMD.SM_serialnum=cd.cd_SM_NUM and SIMD.STATUS <> 'D';
If v_count =0 then
raise NO_ACTIVE;
else
update NMS.SIMDMSLNK set status='D' , Last_update=v_date where status <> 'D' and SM_SERIALNUM in
(select distinct CD_SM_NUM from CONTR_DEVICES cd, contract_all coa
where coa.CH_STATUS='a' and COA.CO_ID=cd.co_id);
COMMIT;
DBMS_OUTPUT.PUT_LINE ( v_count ||' DUMMY MSISDN D-LINKED');
end if;
EXCEPTION
when NO_ACTIVE then
NULL;
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ('SQLCODE : ' || SQLCODE);
DBMS_OUTPUT.put_line ('ERROR MESSAGE : ' || SQLERRM);
end dummy_dlink;
procedure update_port_kind
is
v_port_id number;
v_smc_id number;
v_sm_id number;
v_vend_code varchar2 (10);
BEGIN
FOR cur
IN (SELECT port_id, sm_id
FROM port
WHERE TRUNC (PORT_STATUSMODDAT) = TRUNC (SYSDATE)
AND port_kind IS NULL and port_status = 'r')
LOOP
SELECT smc_id
INTO v_smc_id
FROM storage_medium
WHERE sm_id = cur.sm_id;
SELECT vendcode
INTO v_vend_code
FROM storage_medium
WHERE sm_id = cur.sm_id;
IF v_smc_id IN (1, 2) AND v_vend_code = 'DZ'
THEN
UPDATE port
SET port_kind = 1
WHERE port_id = cur.port_id;
ELSIF v_smc_id IN (1, 2) AND v_vend_code = 'MORPHO'
THEN
UPDATE port
SET port_kind = 2
WHERE port_id = cur.port_id;
ELSIF v_smc_id IN (1, 2) AND v_vend_code = 'GD'
THEN
UPDATE port
SET port_kind = 3
WHERE port_id = cur.port_id;
ELSIF v_smc_id = 3 AND v_vend_code = 'DZ'
THEN
UPDATE port
SET port_kind = 4
WHERE port_id = cur.port_id;
ELSIF v_smc_id = 3 AND v_vend_code = 'ORN'
THEN
UPDATE port
SET port_kind = 5
WHERE port_id = cur.port_id;
ELSIF v_smc_id = 3 AND v_vend_code = 'MORPHO'
THEN
UPDATE port
SET port_kind = 6
WHERE port_id = cur.port_id;
END IF;
END LOOP;
COMMIT;
END;
END ResourceBindDistribute;
/
On running the above I am getting the following error :
ERROR line 112, col 12, ending_line 112, ending_col 20, Found 'EXCEPTION', Expecting: ( SELECT -or- END -or- $IF : AT BEGIN CASE CLOSE COMMIT CONTINUE CURSOR DAY DECLARE DELETE ELSE ELSIF EXECUTE EXIT FETCH FOR FORALL GOTO identifier IF INSERT LOCK LOOP MERGE MOD MODEL MULTISET NULL OPEN RAISE REM RETURN ROLLBACK SAVEPOINT SET SQL THE UPDATE WHILE WITH YEAR -or- <<”
In the section
-- Cusrsor opened Dlinked MSISDN, to assign first DLINKED MSISDN
You start your loop inside the begin section but end the loop after the exception.
If you start a if or loop inside a anonymous block you need to ensure that it is fully contained within it.

How to continue executing code (pl-sql), after capturing an exception

I need to continue with the code execution when exception has been captured.
But my code is exiting when the first exception appeariing, and .
I try simulate an error, for example when not exists the table from the cursor c1, and never run the second part of the code, where it opens c2.
Cursors c1 and c2 look an intermediate table with the names of the tables then processed in the procedure.
Could you give me any idea?
Thankyou in advanced.
Regards.
CREATE OR REPLACE PROCEDURE ISRREP.isr_retention
IS
v_count NUMBER ;--:= 50;
v_commit NUMBER := 50;
str_min VARCHAR2 (100);
str_del_day VARCHAR2 (150);
str_del_month VARCHAR2 (150);
str_lastdate VARCHAR2 (150);
v_lastdate DATE;
v_lastdate_fin DATE;
str_min_fin VARCHAR2 (100);
v_hay_registros NUMBER ; --:= 1;
errno NUMBER;
errmsg VARCHAR2 (255);
str_error VARCHAR2 (300);
str_upd_err VARCHAR2 (300);
v_table1 VARCHAR2 (50);
v_table2 VARCHAR2 (50);
v_table3 VARCHAR2 (50);
v_table4 VARCHAR2 (50);
v_table5 VARCHAR2 (50);
/* armo cursor */
CURSOR c1
IS
SELECT ID, activeflag, errormessage, tablename, retention,
retentionunit, lastdate
FROM isrfrequency_sacme
WHERE activeflag = 'ACTIVE'
AND retentionunit = 'Month'
ORDER BY tablename;
CURSOR c2
IS
SELECT ID, activeflag, errormessage, tablename, retention,
retentionunit, lastdate
FROM isrfrequency_sacme
WHERE activeflag = 'ACTIVE'
AND retentionunit = 'Day'
ORDER BY tablename;
r1 c1%ROWTYPE;
r2 c2%ROWTYPE;
BEGIN
OPEN c1;
LOOP
FETCH c1
INTO r1;
EXIT WHEN c1%NOTFOUND;
str_min := 'select min(utctime) from ' || r1.tablename; --Obtengo fecha y hora mas antigua
EXECUTE IMMEDIATE str_min INTO v_lastdate;
--Tablas con retencionunit Month
BEGIN
r1.retentionunit := 'Month';
str_del_month := 'delete '|| r1.tablename
|| ' where UTCTIME < ADD_MONTHS(sysdate,-' --|| ' where UTCTIME < trunc(ADD_MONTHS(sysdate,-'
|| r1.RETENTION
|| ')'; --Delete de registros que exceden la retencion
EXECUTE IMMEDIATE str_del_month;
v_table1 := r1.tablename;
v_hay_registros := SQL%ROWCOUNT;
IF v_hay_registros != 0
THEN
COMMIT;
END IF;
str_min_fin := 'select min(utctime) from ' || r1.tablename; --Obtengo nuevamente fecha y hora mas antigua
EXECUTE IMMEDIATE str_min_fin INTO v_lastdate_fin;
str_lastdate :=
'update ISRFREQUENCY_SACME set LASTDATE = TO_DATE('''
|| v_lastdate_fin
|| ''',''DD/MM/YYYY HH24:MI:SS'') where tablename = '''
|| r1.tablename
|| ''''; --Update en tabla ISRFREQUENCY_SACME con el ultimo valor leido
--DBMS_OUTPUT.put_line (str_lastdate);
EXECUTE IMMEDIATE str_lastdate;
--
-- EXEPCIONES PARCIALES
--
EXCEPTION
WHEN OTHERS
THEN
errno := SQLCODE;
errmsg := SQLERRM;
test_debug.p_test_debug_out ('ISR_RETENTION', errmsg, 'NO');
update isrfrequency_sacme set activeflag = 'ERROR', errormessage = errmsg where tablename = r1.tablename;
commit;
DBMS_OUTPUT.put_line ('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
DBMS_OUTPUT.put_line ( 'ERROR: '|| errmsg || '. ISRFREQUENCY_SACME.MESSAGGE');
DBMS_OUTPUT.put_line ('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
DBMS_OUTPUT.PUT_LINE('Se ha producido una excepción.');
DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Error message: ' || SQLERRM);
END;
END LOOP;
CLOSE c1;
--Tablas con retencionunit Day
OPEN c2;
LOOP
FETCH c2
INTO r2;
EXIT WHEN c2%NOTFOUND;
BEGIN
r1.retentionunit := 'Day';
str_del_day :=
'delete '
|| r2.tablename
|| ' where UTCTIME < trunc(sysdate-'
|| r2.RETENTION
|| ')'; --Delete de registros que exceden la retencion
EXECUTE IMMEDIATE str_del_day;
v_table2 := r2.tablename;
v_hay_registros := SQL%ROWCOUNT;
--v_hay_registros := v_count;
IF v_hay_registros != 0
--IF v_count != 0
THEN
COMMIT;
ELSE
END IF;
str_min_fin := 'select min(utctime) from ' || r2.tablename; --Obtengo nuevamente fecha y hora mas antigua
EXECUTE IMMEDIATE str_min_fin INTO v_lastdate_fin;
str_lastdate :=
'update ISRFREQUENCY_SACME set LASTDATE = TO_DATE('''
|| v_lastdate_fin
|| ''',''DD/MM/YYYY HH24:MI:SS'') where tablename = '''
|| r2.tablename
|| ''''; --Update en tabla ISRFREQUENCY_SACME con el ultimo valor UTCTIME, luego del delete
--DBMS_OUTPUT.put_line (str_lastdate);
EXECUTE IMMEDIATE str_lastdate;
--
-- EXEPCIONES PARCIALES
--
EXCEPTION
WHEN OTHERS
THEN
errno := SQLCODE;
errmsg := SQLERRM;
test_debug.p_test_debug_out ('ISR_RETENTION', errmsg, 'NO');
update isrfrequency_sacme set activeflag = 'ERROR', errormessage = errmsg where tablename = r2.tablename;
commit;
DBMS_OUTPUT.put_line ('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
DBMS_OUTPUT.put_line ( 'ERROR: '|| errmsg || '. ISRFREQUENCY_SACME.MESSAGGE');
DBMS_OUTPUT.put_line ('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
DBMS_OUTPUT.PUT_LINE('Se ha producido una excepción.');
DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Error message: ' || SQLERRM);
--NULL;
END;
END LOOP;
CLOSE c2;
--
-- EXEPCIONES FINALES
--
EXCEPTION
WHEN OTHERS
THEN
errno := SQLCODE;
errmsg := SQLERRM;
test_debug.p_test_debug_out ('ISR_RETENTION', errmsg, 'NO');
update isrfrequency_sacme set activeflag = 'ERROR', errormessage = errmsg where tablename = r1.tablename;
commit;
DBMS_OUTPUT.put_line ('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
DBMS_OUTPUT.put_line ( 'ERROR: '|| errmsg || '. ISRFREQUENCY_SACME.MESSAGGE');
DBMS_OUTPUT.put_line ('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
DBMS_OUTPUT.PUT_LINE('Se ha producido una excepción.');
DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Error message: ' || SQLERRM);
END;
/

Need help solving Pl/sql issue

I am trying to fill a database with random data, but the primary key needs to still be unique. I can fill the database with random data, but now I am trying to solve the primary key issue.
When running this code I get errors.
set SERVEROUTPUT on
create or replace
PROCEDURE fillDatabase(TableIn IN varchar2, Amount IN NUMBER) IS
columnData varchar2(50);
columnNR number(10);
str varchar2(500);
sqlStatement varchar2(500);
l_ran_time TIMESTAMP;
intlol NUMBER(38);
prmname varchar2(50);
prmtab varchar2(50);
prmkey number(10);
temp number(30);
tempstr varchar2(50);
lolnr number(10);
strq varchar2(50);
BEGIN
lolnr := 1;
select count(*) into columnNR
from user_tab_columns where table_name=TableIn;
FOR counter IN 1..Amount
LOOP
sqlStatement := 'insert into '|| TableIn ||' values (';
FOR counter2 IN 1..columnNR
LOOP
SELECT cols.table_name, cols.column_name into prmtab, prmname
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = TableIn
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
tempstr := 'select count(*) into temp from '|| prmtab;
dbms_output.put_line('test');
dbms_output.put_line(temp);
EXECUTE IMMEDIATE tempstr;
IF temp = 0
THEN
strq := 'SELECT max(' || prmname || ') into prmkey from '|| prmtab || ' order by '|| prmname;
dbms_output.put_line(strq);
EXECUTE IMMEDIATE strq;
END IF;
select dbms_random.value(0, 20) into intlol from dual;
select dbms_random.string('U', 20) into str from dual;
SELECT SYSDATE + dbms_random.value(0, SYSDATE - SYSDATE+1)
INTO l_ran_time
FROM dual;
Select DATA_TYPE INTO columnData
FROM user_tab_columns
WHERE table_name= TableIn
AND COLUMN_ID = counter2;
dbms_output.put_line(columnData);
CASE
WHEN columnData = 'VARCHAR2' THEN sqlStatement := sqlStatement ||''''|| str ||''', ';
WHEN columnData = 'NUMBER' THEN sqlStatement := sqlStatement || intlol ||', ';
WHEN columnData = 'TIMESTAMP(6)' THEN sqlStatement := sqlStatement ||''''|| l_ran_time ||''', ';
ELSE sqlStatement := sqlStatement || NULL || ', ';
END CASE;
END LOOP;
sqlStatement := SUBSTR(sqlStatement, 0, LENGTH(sqlStatement) -2);
sqlStatement := sqlStatement || ')';
dbms_output.put_line(sqlStatement);
EXECUTE IMMEDIATE sqlStatement;
END LOOP;
END fillDatabase;
Can you guys help me solve this?
use the pattern
str := 'select x from t where...';
execute immediate str into var;
instead of
str := 'select x into var from t where...';
execute immediate str;

how to call the stored procedure in oracle with the daily scheduled jobs?

I am new to the oracle job scripts. I wrote some purge procedure to clean all the old data and retain the last 3 months data... procedure is executed successfully. its working when im calling manually also. procedure is as follows:
CREATE OR REPLACE PROCEDURE Archive
IS
v_query varchar2(2048);
v_tablename VARCHAR2(50);
v_condition varchar2(50);
TYPE cur_typ IS REF CURSOR;
c cur_typ;
BEGIN
OPEN c for 'select tablename,columnname from pseb.purge_tables';
FETCH c INTO v_tablename,v_condition;
LOOP
EXIT WHEN c%NOTFOUND;
if(v_tablename ='cfw.DCTBLPERFCUMULATIVEMASTER') then
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB3MAINREG where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB4TODENERG where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERDFCUMULATIVEB5MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB6TODREG where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB7MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB8MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
else
begin
v_query:='delete FROM '|| v_tablename || ' WHERE ' || v_condition||' < sysdate-90';
execute immediate v_query;
end;
end if;
FETCH c INTO v_tablename,v_condition;
end LOOP;
close c;
END; --Procedure
my JOb script is as follows:
begin
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'purgeproc_automation',
job_type => 'STORED_PROCEDURE',
job_action => 'call pseb.archive();',
repeat_interval => 'FREQ=DAILY;INTERVAL=2', /* every other day */
auto_drop => false,
enabled => true,
comments => 'My new job');
end;
/
Job was created successfully, but the job status is failed, not succeed . Whats the reason behind it? it returns the following error:
ORA-06550: line 1, column 728:
PLS-00103: Encountered the symbol "PSEB" when expecting one of the following:
:= . ( # % ;
The symbol ":=" was substituted for "PSEB" to continue.
please guide me to solve this...
Omg, your code looks so complicated. Consider this simplification first:
CREATE OR REPLACE PROCEDURE Archive
IS
v_query varchar2(2048);
BEGIN
FOR REC IN (select tablename,columnname condition from pseb.purge_tables)
LOOP
if(rec.tablename ='cfw.DCTBLPERFCUMULATIVEMASTER') then
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB3MAINREG where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB4TODENERG where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERDFCUMULATIVEB5MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB6TODREG where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB7MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete from cfw.DCTBLPERFDCUMULATIVEB8MAXDEMAN where cumulativeid in (select cumulativeid FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
v_query:='delete FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
else
v_query:='delete FROM '|| rec.tablename || ' WHERE ' || rec.condition||' < sysdate-90';
execute immediate v_query;
end if;
END LOOP;
END; --Procedure
Alternative job definition by dbms_job.submit:
declare
jid number;
begin
dbms_job.submit(
JOB => jid,
WHAT => 'pseb.archive;',
NEXT_DATE => SYSDATE,
INTERVAL => 'sysdate +2');
end;
/
commit; -- <<--added commit here
A way to check job:
select * from user_jobs;
The easiest way to execute a stored procedure from scheduler job is by changing the job_type.
Rest you can use your own values. Try this and post back results.
Example:
job_type => 'STORED_PROCEDURE',
job_action => '"OWNER"."PROCEDURE_NAME"',
It seems like you are mixing two different ways of using create_job with create program. Please do change your script to something like this:
begin
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'purgeproc_automation',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN call pseb.archive(); END;',
repeat_interval => 'FREQ=DAILY;INTERVAL=2', /* every other day */
auto_drop => false,
enabled => true,
comments => 'My new job');
end;
/

Resources