PL/SQL code coming with compilation error - plsql

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.

Related

"ORA-01843: not a valid month" error

I am experiencing this error when trying to run the procedure from the codes.
"ORA-01843: not a valid month ORA-06512: at "NOSTAS_OWNER.INSERT_EXPORTMATRIX", line 68 ORA-06512: at line 1"
I'm passing the date as "DD/MM/YYYY" string. Not sure what caused the error. When running it in SQL Developer, it gives this error
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "NOSTAS_OWNER.INSERT_EXPORTMATRIX", line 68
ORA-06512: at line 17
create or replace PROCEDURE INSERT_EXPORTMATRIX
(
keyClass export_matrix.EXM_KEY_CLASS%type,
keyClassValue export_matrix.EXM_KEYCLASS_VALUE%type,
changedClass export_matrix.EXM_CHANGED_CLASS%type,
changedClassValue export_matrix.EXM_CHANGEDCLASS_VALUE%type,
dtTo IN VARCHAR2,
dtFrom IN VARCHAR2,
-- dtTo export_matrix.EXM_DATEFROM%type,
-- dtFrom EXPORT_MATRIX.EXM_DATETO%type,
vresult OUT NUMBER
) AS
iskeyClass number;
--today varchar2(50);
--today date;
fvvECCN number;
fvvALNR number;
BEGIN
iskeyClass := 0;
fvvECCN := 0;
fvvALNR := 0;
SAVEPOINT start_tran;
SELECT count(*) into iskeyClass FROM EXPORT_MATRIX
WHERE EXM_KEY_CLASS=keyClass and EXM_KEYCLASS_VALUE=keyClassValue;
-- SELECT to_date(SYSDATE,'DD/MM/YYYY') into today FROM DUAL;
SELECT count(*) into fvvECCN FROM FIELDS_VALUE_VALIDATION WHERE
--FVV_FAVL_NAME = 'ECCN' and FVV_VALUE = keyClassValue and (dtTo > trunc(sysdate));
FVV_FAVL_NAME = 'ECCN' and FVV_VALUE = keyClassValue and (sysdate between dtFrom and dtTo) or dtTo is null;
SELECT count(*) into fvvALNR FROM FIELDS_VALUE_VALIDATION WHERE
FVV_FAVL_NAME = 'ALNR' and FVV_VALUE = changedClassValue and (sysdate between dtFrom and dtTo) or dtTo is null;
BEGIN
-- if (iskeyClass = 1 and (dtTo < trunc(sysdate))) or iskeyClass = 0 then
if (iskeyClass = 1 and (dtTo between DtFrom and sysdate)) or iskeyClass = 0 then
Insert into EXPORT_MATRIX (EXM_KEY_CLASS, EXM_KEYCLASS_VALUE, EXM_CHANGED_CLASS,
EXM_CHANGEDCLASS_VALUE, EXM_DATEFROM)
values (keyClass,keyClassValue,changedClass,changedClassValue,trunc(sysdate));
if fvvECCN = 0 then
Insert into FIELDS_VALUE_VALIDATION (FVV_FAVL_NAME,FVV_VALUE,FVV_DATEFROM)
values ('ECCN',keyClassValue,trunc(sysdate));
end if;
if fvvALNR = 0 then
Insert into FIELDS_VALUE_VALIDATION (FVV_FAVL_NAME,FVV_VALUE,FVV_DATEFROM)
values ('ALNR',changedClassValue,trunc(sysdate));
end if;
end if;
-- if keyClass = 1 and (dtTo > trunc(sysdate)) then
if keyClass = 1 and (sysdate between dtFrom and dtTo) or dtTo is null then
update EXPORT_MATRIX set
EXM_DATETO=to_date(dtTo)
WHERE EXM_KEY_CLASS=keyClass and EXM_KEYCLASS_VALUE=keyClassValue;
end if;
END;
commit;
vresult := 1;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO start_tran;
RAISE;
END INSERT_EXPORTMATRIX;
You should add a mask when you convert a string to date:
to_date(dtTo,'DD/MM/YYYY')
Moreover, you should also convert string to date when you comparing with sysdate, so:
sysdate between to_date(dtFrom,'DD/MM/YYYY') and to_date(dtTo,'DD/MM/YYYY')

CREATE OR REPLACE PROCEDURE proc_video_search

Create a procedure called proc_video_search to search for a video and display the name, copy ID, format, and status of the video’s copies. In addition, the checkout dates and due dates are also displayed for unreturned copies. The damaged copies (Status = 'D') are excluded in your output. Sort your output by the video name (Name) and then the copy ID (CopyID).
$ CREATE OR REPLACE PROCEDURE proc_video_search (
p_VideoName VARCHAR2,
p_FormatName VARCHAR2 DEFAULT NULL) as
v_Count NUMBER;
v_TotalCopies NUMBER; v_Avalb NUMBER;v_FormatName VARCHAR2(100);
v_VideoName VARCHAR2(100); v_CopyID VARCHAR2(100);v_DueDate DATE;
v_Status VARCHAR2(100); v_CheckoutDate DATE;
CURSOR asdf IS
SELECT T_VIDEO.Name, T_COPY.CopyID, Status,T_VIDEO_FORMAT.NAME
FROM T_VIDEO
INNER JOIN T_COPY ON T_VIDEO.VideoID = T_COPY.VideoID
INNER JOIN T_VIDEO_FORMAT ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status !='D' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) || '%'
OR UPPER(T_VIDEO_FORMAT.NAME)= UPPER(p_FormatName)
ORDER BY T_VIDEO.Name, T_COPY.CopyID;
BEGIN
SELECT COUNT(*)
INTO v_Count
FROM T_VIDEO
WHERE UPPER(T_VIDEO.Name) like '%' || UPPER(p_VideoName) || '%' ;
IF v_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('**** '||v_Count|| ' results found for ' ||
p_VideoName||'. *****');
RETURN;
END IF;
SELECT count(T_COPY.CopyID) INTO v_TotalCopies
FROM T_COPY INNER JOIN T_VIDEO ON T_COPY.VideoID = T_VIDEO.VideoID
INNER JOIN T_VIDEO_FORMA ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status !='D' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) ||'%'
OR UPPER(T_VIDEO_FORMAT.NAME)=UPPER(p_FormatName);
SELECT count(T_COPY.CopyID)INTO v_Avalb FROM T_COPY
INNER JOIN T_VIDEO ON T_COPY.VideoID = T_VIDEO.VideoID
INNER JOIN T_VIDEO_FORMAT ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status ='A' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) ||'%'
OR UPPER(T_VIDEO_FORMAT.NAME)=UPPER(p_FormatName);
IF v_TotalCopies >=0 THEN
IF p_FormatName IS NULL THEN
DBMS_OUTPUT.PUT_LINE(v_TotalCopies||' results found for '||
p_VideoName||' . (Available copies:'|| v_Avalb|| ')' );
ELSE
DBMS_OUTPUT.PUT_LINE(v_TotalCopies||' results found for '||
p_VideoName||'('|| p_FormatName||') . (Available copies:'||
v_Avalb|| ')' );
end if;
OPEN asdf;
LOOP
FETCH asdf INTO v_VideoName, v_CopyID, v_Status,
v_FormatName ; exit when asdf%NOTFOUND ;
SELECT COUNT(CheckoutDate)
INTO v_Count FROM T_RENTAL WHERE CopyID = v_CopyID;
IF v_Count = 1 THEN
SELECT CheckoutDate,DueDate
INTO v_CheckoutDate,v_DueDate
FROM T_RENTAL
WHERE CopyID = v_CopyID;
end if;
DBMS_OUTPUT.PUT_LINE(RPAD('-', 53, '-'));
DBMS_OUTPUT.PUT_LINE(RPAD('Name:',30) || RPAD(v_VideoName,15));
DBMS_OUTPUT.PUT_LINE(RPAD('CopyID:',30) || RPAD(v_CopyID,15));
DBMS_OUTPUT.PUT_LINE(RPAD('Format:',30) ||
RPAD(v_FormatName,15));
IF v_Status = 'A' THEN v_Status := 'Available';END IF;
IF v_Status = 'R' THEN v_Status := 'Rented'; END IF;
DBMS_OUTPUT.PUT_LINE(RPAD('Status:',30) || RPAD(v_Status,15));
IF v_Status ='Available' THEN
DBMS_OUTPUT.PUT_LINE(RPAD('CheckoutDate:',30)
||'****************************');
DBMS_OUTPUT.PUT_LINE(RPAD('DueDate:',30)
||'****************************');
ELSE
DBMS_OUTPUT.PUT_LINE(RPAD('CheckoutDate:',30)
||RPAD(TO_CHAR(v_CheckoutDate, 'DD-MON-YYYY'),15));
DBMS_OUTPUT.PUT_LINE(RPAD('DueDate:',30) ||RPAD(TO_CHAR(
v_DueDate, 'DD-MON-YYYY'),15));
END IF; END LOOP; CLOSE asdf; END IF; END proc_video_search ;
EXEC proc_video_search('ANOTHER', 'DVD')`
enter image description here
The problem is with this line of your cursor asdf
OR UPPER(T_VIDEO_FORMAT.NAME)= UPPER(p_FormatName)
Because it says OR, the query can choose to ignore this criteria if it evaluates to false. That is why you are getting results with all the formats; it ignores the filtering.
You have to wrap the OR statement in another AND clause, like so
WHERE Status !='D'
AND UPPER(T_VIDEO.Name) like '%' || UPPER(p_VideoName) || '%'
AND ( /* OR clause here */ )
And then you can handle the case of p_formatName being null or not.

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;

SYS.DBMS_DDL.WRAP not allowing pragma and intctx

I am using the below procedure to wrap the PL/SQL Code.
declare
l_source DBMS_SQL.VARCHAR2A;
l_wrap DBMS_SQL.VARCHAR2A;
l_wrap1 clob;
typ_ibt utl_file.file_type;
cnt number := 0;
v_directory varchar2(400) := 'd:\ftpedi\eqpm\eqpm_hold\';
cursor cur_name_get is
select distinct name object_name,type object_type
from user_source
where type = 'PROCEDURE'
and name = 'PROCESS_TIME_INSERT';
cursor cut_text_get ( p_type in varchar2 , p_name in varchar2 ) is
select replace(text,chr(10),'') text
from user_source
where type = p_type
and name = p_name;
begin
for i in cur_name_get
loop
l_source.delete;l_wrap.delete;
open cut_text_get ( i.object_type,i.object_name );
fetch cut_text_get bulk collect into l_source;
close cut_text_get;
l_source (1) := 'CREATE OR REPLACE ' || l_source (1);
l_wrap := SYS.DBMS_DDL.WRAP(ddl => l_source,
lb => 1,
ub => l_source.count);
for i in 1..l_wrap.count
loop
if i = 1
then
l_wrap1 := l_wrap(i);
else
l_wrap1 := l_wrap1 || l_wrap(i);
end if;
insert into ibt_global_inter_transfer ( git_process_id,git_c_1)
values ( 3004, l_wrap1 );
end loop;
end loop;
exception when others
then
dbms_output.put_line('sqlerrm '||sqlerrm||dbms_utility.format_error_backtrace);
end;
The above procedures warps the normal procedure, but not allowing the special character like 'PRAGMA'.
The below is the sample procedure, which is not wrapping.
CREATE OR REPLACE
PROCEDURE xml_insert ( p_in_xml in xmltype ,p_status out varchar2,p_message out varchar2) is
intctx DBMS_XMLSTORE.ctxtype;
rows number;
begin
p_status := 'S';
p_message := 'Success';
intctx := Dbms_xmlstore.newcontext('IBT_GLOBAL_INTER_TRANSFER');
dbms_xmlstore.clearupdatecolumnlist(intctx);
dbms_xmlstore.setupdatecolumn(intCtx,'GIT_PROCESS_ID');
dbms_xmlstore.setupdatecolumn(intCtx,'GIT_SESSION_ID');
rows := Dbms_xmlstore.insertxml(intctx,p_in_xml);
dbms_xmlstore.closecontext(intctx);
exception when others
then
p_status := 'R';
p_message := sqlerrm||dbms_utility.format_error_backtrace;
return;
end;
Could anyone help?
Update
(I misunderstood the problem. I thought you meant the wrapped code wasn't created, now I understand the real problem is that the wrapped output does not compile.)
Remove the replace, there needs to be white space between some of the lines.
Replace:
--select replace(text,chr(10),'') text
with:
select text
Procedure:
CREATE OR REPLACE
PROCEDURE xml_insert ( p_in_xml in xmltype ,p_status out varchar2,p_message out varchar2) is
intctx DBMS_XMLSTORE.ctxtype;
rows number;
pragma autonomous_transaction; --ADDED
begin
p_status := 'S';
p_message := 'Success';
intctx := Dbms_xmlstore.newcontext('IBT_GLOBAL_INTER_TRANSFER');
dbms_xmlstore.clearupdatecolumnlist(intctx);
dbms_xmlstore.setupdatecolumn(intCtx,'GIT_PROCESS_ID');
dbms_xmlstore.setupdatecolumn(intCtx,'GIT_SESSION_ID');
rows := Dbms_xmlstore.insertxml(intctx,p_in_xml);
dbms_xmlstore.closecontext(intctx);
exception when others
then
p_status := 'R';
p_message := sqlerrm||dbms_utility.format_error_backtrace;
return;
end;
/
PL/SQL Block wrapping code:
declare
l_source DBMS_SQL.VARCHAR2A;
l_wrap DBMS_SQL.VARCHAR2A;
l_wrap1 clob;
typ_ibt utl_file.file_type;
cnt number := 0;
v_directory varchar2(400) := 'd:\ftpedi\eqpm\eqpm_hold\';
cursor cur_name_get is
select distinct name object_name,type object_type
from user_source
where type = 'PROCEDURE'
and name = 'XML_INSERT'; --CHANGED
cursor cut_text_get ( p_type in varchar2 , p_name in varchar2 ) is
--select replace(text,chr(10),'') text --WOOPS!
select text
from user_source
where type = p_type
and name = p_name;
begin
for i in cur_name_get
loop
l_source.delete;l_wrap.delete;
open cut_text_get ( i.object_type,i.object_name );
fetch cut_text_get bulk collect into l_source;
close cut_text_get;
l_source (1) := 'CREATE OR REPLACE ' || l_source (1);
l_wrap := SYS.DBMS_DDL.WRAP(ddl => l_source,
lb => 1,
ub => l_source.count);
for i in 1..l_wrap.count
loop
if i = 1
then
l_wrap1 := l_wrap(i);
else
l_wrap1 := l_wrap1 || l_wrap(i);
end if;
--insert into ibt_global_inter_transfer ( git_process_id,git_c_1)
--values ( 3004, l_wrap1 );
dbms_output.put_line(l_wrap1);
end loop;
end loop;
exception when others
then
dbms_output.put_line('sqlerrm '||sqlerrm||dbms_utility.format_error_backtrace);
end;
/

TOAD Oracle SQL Return Row from UDF

have a function below that returns a cursor. However I need it to return just one row. I have read on google about returning the data as a %rowtype but I do not seem to be able to get it to work.
The reason I don't want a cursor returned is that when I select the function one cell is returned saying (cursor) instead of displaying one row with 4 columns of data.
create or replace function udf
(
i_ptf_code in varchar2
,i_begin_date in date
,i_end_date in date
)
return sys_refcursor
is
portf_code varchar2(50);
end_date date;
chain_linked_ptf_net float;
chain_linked_ptf_gross float;
chain_linked_bmk float;
end_mv float;
v_cursor sys_refcursor;
cursor c_return_series is
select p.portf_code
,gpr.end_date
,gpr.end_market_value
,gpr.portf_perf_gross as gross_ret
,gpr.portf_perf_net as net_ret
,(exp(sum(ln(1 + gpr.portf_perf_gross)) over (partition by p.portf_code order by end_date)) - 1) as chain_linked_ptf_gross
,(exp(sum(ln(1 + gpr.portf_perf_net)) over (partition by p.portf_code order by end_date)) - 1) as chain_linked_ptf_net
,(exp(sum(ln(1 + gpr.bmk_perf)) over (partition by p.portf_code order by end_date)) - 1) as chain_linked_bmk
from portfolio_returns gpr
inner join portfolio p on p.portf_id = gpr.portf_id and p.is_composite != 2 --2 means composite
where p.portf_code = i_ptf_code
and gpr.end_date between i_begin_date and i_end_date;
begin
select i_ptf_code, i_end_date into portf_code, end_date from dual;
for c1line in c_return_series loop
if c1line.end_date = i_end_date then
select c1line.chain_linked_ptf_gross
,c1line.chain_linked_ptf_net
,c1line.chain_linked_bmk
,c1line.end_market_value
into chain_linked_ptf_gross
,chain_linked_ptf_net
,chain_linked_bmk
,end_mv
from dual;
end if;
end loop;
open v_cursor for
select portf_code as portf_code
,end_date as end_date
,chain_linked_ptf_gross * 100 as period_ptf_gross
,chain_linked_ptf_net * 100 as period_ptf_net
,chain_linked_bmk * 100 as period_bmk
,end_mv * 100 as period_mv
from dual;
return v_cursor;
close v_cursor;
end;
/
I am a little lost but I think this is what you need:
[...]
open v_cursor for
select portf_code as portf_code
,end_date as end_date
,chain_linked_ptf_gross * 100 as period_ptf_gross
,chain_linked_ptf_net * 100 as period_ptf_net
,chain_linked_bmk * 100 as period_bmk
,end_mv * 100 as period_mv
from dual;
return v_cursor;
-- close v_cursor; -- not needed
end;
-
DECLARE
l_rc SYS_REFCURSOR;
l_portf_code VARCHAR2(30);
l_end_date VARCHAR2(30);
l_period_ptf_gross NUMBER;
l_period_ptf_net NUMBER;
l_period_bmk NUMBER;
l_period_mv NUMBER;
BEGIN
l_rc := udf; -- This returns an open cursor
fetch v_rc into l_portf_code, l_end_date, l_period_ptf_gross, l_period_ptf_net, l_period_bmk, l_period_mv;
DBMS_OUTPUT.PUT_LINE
(
l_portf_code || ' ' ||
l_end_date || ' ' ||
l_period_ptf_gross || ' ' ||
l_period_ptf_net || ' ' ||
l_period_bmk || ' ' ||
l_period_mv
);
CLOSE v_rc;
END;

Resources