External File Writing Not Completed - pascal

When I write to an external file in Pascal the external file is not written completely even though the coding was flawless.
procedure save;
{Menyimpan data penerbangan ke dalam file eksternal}
begin
write('> ');
write('nama file: ');
readln(namafisik);
assign(d,namafisik);
{$I-};
rewrite(d);
{$I+};
if (IOresult=0) then
begin
for i:=1 to Neff do
writejadwal(i);
end;
end;
procedure writejadwal(i:integer);
{Menuliskan jadwal ke file eksternal pada indeks ke-i}
begin
write(d,T.KodePenerbangan[i]);
write(d,' | ');
write(d,T.KotaKeberangkatan[i]);
write(d,' | ');
write(d,T.KotaKedatangan[i]);
write(d,' | ');
if ((T.TKeberangkatan[i].Day) < 10 ) then
begin
x:=T.TKeberangkatan[i].Day;
str(x,y);
s:=y;
write(d,'0'+s);
end
else
begin
write(d,T.TKeberangkatan[i].Day);
end;
write(d,':');
if ((T.TKeberangkatan[i].Month) < 10 ) then
begin
x:=T.TKeberangkatan[i].Month;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKeberangkatan[i].Month);
end;
write(d,':');
write(d,T.TKeberangkatan[i].Year);
write(d,' | ');
if ((T.TKeberangkatan[i].Hour) < 10 ) then
begin
x:=T.TKeberangkatan[i].Hour;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKeberangkatan[i].Hour);
end;
write(d,':');
if ((T.TKeberangkatan[i].Minute) < 10 ) then
begin
x:=T.TKeberangkatan[i].Minute;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKeberangkatan[i].Minute);
end;
write(d,' | ');
if ((T.TKedatangan[i].Day) < 10 ) then
begin
x:=T.TKedatangan[i].Day;
str(x,y);
s:=y;
write(d,'0'+s);
end
else
begin
write(d,T.TKedatangan[i].Day);
end;
write(d,':');
if ((T.TKedatangan[i].Month) < 10 ) then
begin
x:=T.TKedatangan[i].Month;
str(x,y);
s:=y;
write(d,'0'+s);
end
else
begin
write(d,T.TKedatangan[i].Month);
end;
write(d,':');
write(d,T.TKedatangan[i].Year);
write(d,' | ');
if ((T.TKedatangan[i].Hour) < 10 ) then
begin
x:=T.TKedatangan[i].Hour;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKedatangan[i].Hour);
end;
write(d,':');
if ((T.TKedatangan[i].Minute) < 10 ) then
begin
x:=T.TKedatangan[i].Minute;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKedatangan[i].Minute);
end;
write(d,' | ');
if (T.TKedatanganImplementasi[i].Day=0) then
writeln(d,'__:__:____ | __:__ | __:__:____ | __:__')
else
{MULAI}
begin
if ((T.TKeberangkatanImplementasi[i].Day) < 10 ) then
begin
x:=T.TKeberangkatanImplementasi[i].Day;
str(x,y);
s:=y;
write(d,'0'+s);
end
else
begin
write(d,T.TKeberangkatanImplementasi[i].Day);
end;
write(d,':');
if ((T.TKeberangkatanImplementasi[i].Month) < 10 ) then
begin
x:=T.TKeberangkatanImplementasi[i].Month;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKeberangkatanImplementasi[i].Month);
end;
write(d,':');
write(d,T.TKeberangkatanImplementasi[i].Year);
write(d,' | ');
if ((T.TKeberangkatanImplementasi[i].Hour) < 10 ) then
begin
x:=T.TKeberangkatanImplementasi[i].Hour;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKeberangkatanImplementasi[i].Hour);
end;
write(d,':');
if ((T.TKeberangkatanImplementasi[i].Minute) < 10 ) then
begin
x:=T.TKeberangkatanImplementasi[i].Minute;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKeberangkatanImplementasi[i].Minute);
end;
write(d,' | ');
if ((T.TKedatanganImplementasi[i].Day) < 10 ) then
begin
x:=T.TKedatanganImplementasi[i].Day;
str(x,y);
s:=y;
write(d,'0'+s);
end
else
begin
write(d,T.TKedatanganImplementasi[i].Day);
end;
write(d,':');
if ((T.TKedatanganImplementasi[i].Month) < 10 ) then
begin
x:=T.TKedatanganImplementasi[i].Month;
str(x,y);
s:=y;
write(d,'0'+s);
end
else
begin
write(d,T.TKedatanganImplementasi[i].Month);
end;
write(d,':');
write(d,T.TKedatanganImplementasi[i].Year);
write(d,' | ');
if ((T.TKedatanganImplementasi[i].Hour) < 10 ) then
begin
x:=T.TKedatanganImplementasi[i].Hour;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKedatanganImplementasi[i].Hour);
end;
write(d,':');
if ((T.TKedatanganImplementasi[i].Minute) < 10 ) then
begin
x:=T.TKedatanganImplementasi[i].Minute;
str(x,y);
write(d,'0'+y);
end
else
begin
write(d,T.TKedatanganImplementasi[i].Minute);
end;
writeln(d);
end;
{SELESAI}
end;
By the way jadwal is Indonesian for English

The coding wasn't quite flawless. In particular, you don't seem to be closing the file.

The problem is that you don't close or flush. Most languages try to minimize calling the OS for write, and do that by writing only if enough bytes have been accumulated. (128 bytes standard, but this can be increased with settextbuf). Probably your file is a multiple of 128bytes.
flushing or closing writes out the remains of the buffer.

Related

DBMS output not printing

Hi I want to write a ISBN validation in PLSql. But dbms_output.put_line doesnt output anything. I dont have any errors but dbms_output is yellow marked with the warning:
Unable to resolve symbol 'dbms_output'
Inspection info: This inspection performs unresolved SQL references check.
. Im working in DataGrip and yes I have enabled SYS.dbms_output.
DECLARE
v_isbn VARCHAR(13) := 9783161484100;
BEGIN
isbn_validation(v_isbn);
end;
CREATE OR REPLACE PROCEDURE isbn_validation
(
p_isbn VARCHAR
)
IS
v_isbn VARCHAR(13);
v_checksum INTEGER := 0;
BEGIN
if LENGTH(p_isbn) = 10 THEN
v_isbn := CONCAT('978',p_isbn);
v_checksum := isbn13_checksum(v_isbn);
if(v_checksum = -1)THEN dbms_output.put_line(v_isbn || ' is a invalid isbn');
else
v_isbn:=CONCAT(v_isbn,CAST(v_checksum AS CHAR));
isbn_validation(v_isbn);
dbms_output.put_line(v_isbn || ' is a valid isbn');
end if;
elsif LENGTH(v_isbn) = 13 THEN
v_checksum := isbn13_checksum(v_isbn);
if(v_checksum = -1)THEN dbms_output.put_line(v_isbn || ' is a invalid isbn');
else
v_isbn:=CONCAT(v_isbn,CAST(v_checksum AS CHAR));
isbn_validation(v_isbn);
dbms_output.put_line(v_isbn || ' is a valid isbn');
end if;
end if;
end;
CREATE OR REPLACE FUNCTION isbn13_checksum
(
v_isbn VARCHAR
)
RETURN INTEGER
IS
v_checksum INTEGER := 0;
v_remainder INTEGER := 0;
BEGIN
FOR i IN 1..12 LOOP
if MOD(i,2) = 1 THEN
if SUBSTR(v_isbn,i) = 'X' THEN
v_checksum := v_checksum + 10;
else v_checksum := v_checksum + CAST(SUBSTR(v_isbn,i) AS INTEGER);
end if;
else
if SUBSTR(v_isbn,i) = 'X' THEN
v_checksum := v_checksum + (10 * 3);
else v_checksum := v_checksum + (CAST(SUBSTR(v_isbn,i) AS INTEGER) * 3);
end if;
end if;
end loop;
v_remainder := mod(v_checksum, 10);
if v_remainder = 0 THEN RETURN -1;
elsif(10-v_remainder)=10 THEN RETURN 0;
else RETURN (10 - v_remainder);
end if;
end;
Try explicitly enabling DBMS_OUTPUT using dbms_output.enable
DECLARE
v_isbn VARCHAR(13) := 9783161484100;
BEGIN
dbms_output.enable;
isbn_validation(v_isbn);
end;
Execute SET SERVEROUTPUT ON and run your code again.
Might be this bug: https://youtrack.jetbrains.com/issue/DBE-4702
Please, use the latest version.

Dump table containing CLOB to csv using PL/SQL proc

I have assembled a procedure to dump a query containing CLOB columns to a csv file.
It seems to be working fine until I encounter a query containing dates.
ORA-00932: inconsistent datatypes: expected CLOB got DATE
Is there a way to dynamically convert those dates to some default string format to be able to use the procedure as it is now. Or how can I refactor it if necessary?
create or replace
PROCEDURE export_query_csv(
p_query IN VARCHAR2,
p_filename IN VARCHAR2)
IS
l_separator VARCHAR2 (10 CHAR) := ';';
l_dir VARCHAR2 (128 CHAR) := 'MY_DIR';
l_output utl_file.file_type;
l_theCursor INTEGER DEFAULT dbms_sql.open_cursor;
l_columnValue CLOB;
l_status INTEGER;
l_colCnt NUMBER DEFAULT 0;
l_cnt NUMBER DEFAULT 0;
l_descTbl dbms_sql.desc_tab;
l_substrVal VARCHAR2(4000) ;
l_offset NUMBER :=1;
l_amount NUMBER := 3000;
l_clobLen NUMBER :=0;
BEGIN
EXECUTE IMMEDIATE 'alter session set nls_date_format = ''dd-mon-yyyy hh24:mi:ss''';
l_output := utl_file.fopen(l_dir, p_filename, 'wb');
dbms_sql.parse(l_theCursor, p_query, dbms_sql.native);
FOR i IN 1 .. 1000
LOOP
BEGIN
dbms_sql.define_column(l_theCursor, i, l_columnValue);
l_colCnt := i;
EXCEPTION
WHEN OTHERS THEN
IF ( SQLCODE = -1007 ) THEN
EXIT;
ELSE
RAISE;
END IF;
END;
END LOOP;
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
FOR i IN 1 .. l_colCnt
LOOP
utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));
utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_descTbl(i).col_name));
utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));
IF i < l_colCnt THEN
utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_separator));
END IF;
END LOOP;
utl_file.put_raw(l_output,utl_raw.cast_to_raw(chr(13) || chr(10)));
l_status := dbms_sql.execute(l_theCursor);
LOOP
EXIT WHEN (dbms_sql.fetch_rows(l_theCursor) <= 0);
FOR i IN 1 .. l_colCnt
LOOP
dbms_sql.column_value(l_theCursor, i, l_columnValue);
l_clobLen := dbms_lob.getlength(l_columnValue);
WHILE l_offset <= l_clobLen
LOOP
l_substrVal := dbms_lob.substr(l_columnValue,l_amount,l_offset);
utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));
utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_substrVal));
utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));
l_offset:=l_offset+l_amount;
END LOOP;
l_offset := 1;
IF i < l_colCnt THEN
utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_separator));
END IF;
END LOOP;
utl_file.put_raw(l_output,utl_raw.cast_to_raw(chr(13) || chr(10)));
l_cnt := l_cnt + 1;
END LOOP;
dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output);
END;
Found it myself, following this pattern:
-- Define columns
FOR i IN 1 .. colcnt LOOP
IF desctab(i).col_type = 2 THEN
DBMS_SQL.DEFINE_COLUMN(curid, i, numvar);
ELSIF desctab(i).col_type = 12 THEN
DBMS_SQL.DEFINE_COLUMN(curid, i, datevar);
......
ELSE
DBMS_SQL.DEFINE_COLUMN(curid, i, namevar);
END IF;
END LOOP;

How to continue when no_data_found exception raised?

I have a value that i dont know exactly in which column it is inserted. I can determine that since this table columns are named as C1....C99. I am going to find the column using the following code. But when the no data found exception is called, I cant start my loop again.
DECLARE
i number:=1;
k boolean := true;
output number;
stmt varchar(500);
BEGIN
<<repeat>>
while(k)
loop
DBMS_OUTPUT.PUT_LINE( 'c column -->'||i );
EXECUTE IMMEDIATE 'select 1 from X_TAB t where t.c'||i||'= ''NOTE_NFE'''
into output;
IF output = 1 then
DBMS_OUTPUT.PUT_LINE( 'c column'||i );
k:=false;
END IF;
i:=i+1;
IF i = 100 then
k:=false;
END IF;
END LOOP;
EXCEPTION
when no_data_found then
i:=i+1;
if i = 100 then
k:=false;
end if;
goto repeat;
END;
/
I think that moving your try catch to inside the loop will do the trick for you.
DECLARE
i number:=1;
k boolean := true;
output number;
stmt varchar(500);
BEGIN
<<repeat>>
while(k)
loop
DBMS_OUTPUT.PUT_LINE( 'c column -->'||i );
begin
EXECUTE IMMEDIATE 'select 1 from X_TAB t where t.c'||i||'= ''FISCAL_NOTE_NFE''' into output;
exception when no_data_found then
output := 0; --set a default value
dbms_output.put_line('no data found.');
end;
IF output = 1 then
DBMS_OUTPUT.PUT_LINE( 'c column'||i );
k:=false;
END IF;
i:=i+1;
IF i = 100 then
k:=false;
END IF;
END LOOP;
END;
/
You can use:
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM X_TAB
WHERE dynamic_column_name = 'FISCAL_NOTE_NFE'
)
THEN 1
ELSE 0
END
FROM DUAL
It will never throw the exception as it will always return exactly one row.
You can use it like:
DECLARE
i INT := 0;
output INT;
BEGIN
FOR k IN 1 .. 100 LOOP
DBMS_OUTPUT.PUT_LINE( 'c column -->'||k );
EXECUTE IMMEDIATE 'SELECT CASE WHEN EXISTS(SELECT 1 FROM X_TAB WHERE c'||k||' = ''FISCAL_NOTE_NFE'') THEN 1 ELSE 0 END FROM DUAL'
INTO output;
IF output = 1 THEN
DBMS_OUTPUT.PUT_LINE( 'c column'||k );
i := k;
EXIT;
END IF;
END LOOP;
-- Do something with i
END;
/
You can also use
select count(1) from X_TAB

Oracle Error "Ora-06502 PL/SQL: numaric or value error: invalid LOB locate"

I'm getting this Oracle error Ora-06502 PL/SQL: numeric or value error: invalid LOB locate when two users try to commit at the same time.
I actually tried hard to solve this problem but I couldn't find a solution.
Please see the code for more details.
PROCEDURE CHECK_POP_DATA_PCC(RETURN_STATUS IN OUT NUMBER) IS LAST BOOLEAN;
REC_NUMBER NUMBER;
MESS_CTR NUMBER;
NO_DETAIL EXCEPTION;
SWF_FAIL EXCEPTION;
USA_STATUS NUMBER;
TEXT_STATUS NUMBER;
FIRST_ROUND NUMBER;
OERR NUMBER;
FILE_NAME VARCHAR2(100);
FILE_NAME1 VARCHAR2(100);
FILE_NAME2 VARCHAR2(100);
FILE_DIR VARCHAR2(100);
PRINT_ERR EXCEPTION;
PRINT_ERR1 EXCEPTION;
MESS_TEXT VARCHAR2(1000);
CTR NUMBER;
LEN NUMBER;
SUB NUMBER;
OPEN_TYPE NUMBER;
J NUMBER;
STR1 VARCHAR2(100);
STR2 VARCHAR2(100);
SPC NUMBER;
ESCP VARCHAR2(512);
FIRST_SEQ NUMBER;
REM NUMBER;
CLOSE_TYPE NUMBER;
SWF_ADD VARCHAR2(100);
SWF_PASS VARCHAR2(100);
SWF_UNAME VARCHAR2(100);
COMP_FILE TEXT_IO.FILE_TYPE;
FIRST_FILE TEXT_IO.FILE_TYPE;
SECOND_FILE TEXT_IO.FILE_TYPE;
cur_hdl integer;
rows_p integer;
tmp_chr VARCHAR2(2);
SWF_FTP_COM VARCHAR2(100);
File_status NUMBER;
ret_sta NUMBER;
key_all_data CLOB;
RTGS_IPADD VARCHAR2(100);
RTGS_FLA NUMBER (1,0);
RTGS_UNAME VARCHAR2(100);
RTGS_PASS VARCHAR2(100);
IS_SEC NUMBER ;
CURSOR C2 IS
SELECT MESS_TYPE_CODE,
SEQ_NUM,
FROM_SWF_CODE,
TO_SWF_CODE,
PRTY_CODE
FROM SWF_MESS
WHERE BRA_CODE = :SYS.BRA_CODE
AND REF_TYPE = :SYS.REF_TYPE
AND REF_YEAR = :SYS.REF_YEAR
AND REF_NUM = :SYS.REF_NUM
AND NVL(FILE_NUM,0) = :SYS.FILE_NUM
AND MESS_STA_CODE NOT IN (1,
2,
3,
5,
6,
7,
8);
BEGIN RET_STA := 0;
SELECT SWF_IPADDRESS,
SWF_USERNAME,
SWF_PASSWORD,
nvl(SWF_EOL_CHR,'CHR(10)'),
FTP_COM,
RTGS_IPADDRESS,
RTGS_FLAG,
RTGS_USERNAME,
RTGS_PASSWORD,
IS_SECURE INTO SWF_ADD,
SWF_UNAME,
SWF_PASS,
:TIT.EOL_SWF,
SWF_FTP_COM,
RTGS_IPADD,
RTGS_FLA,
RTGS_UNAME,
RTGS_PASS,
IS_SEC ---SHAZA(BANK/27/4540)
FROM web_par
WHERE app_ipaddress = nvl(:GLOBAL.APP_IPADDRESS,'0.0.0.0') ;
/* *************************************** */ FIRST_ROUND := 0;
RETURN_STATUS := 0;
FIRST_SEQ := 0;
key_all_data := '';
IF :GLOBAL.BRA_BRA_CODE = :GLOBAL.BAN_HO_REG_CODE THEN GO_BLOCK('SYS');
FIRST_RECORD;
LAST := FALSE;
WHILE NOT LAST LOOP IF :SYS.FLAG = 1 THEN
FOR C2REC IN C2 LOOP A07SWF00(:SYS.BRA_CODE,:SYS.REF_TYPE,:SYS.REF_YEAR,:SYS.REF_NUM, C2REC.MESS_TYPE_CODE,C2REC.SEQ_NUM,RETURN_STATUS);
IF RETURN_STATUS = 0 THEN
COMMIT;
CHECK_FRM_STATS;
ELSE RAISE SWF_FAIL;
END IF;
END LOOP;
END IF;
IF :SYSTEM.LAST_RECORD = 'TRUE' THEN LAST := TRUE;
ELSE NEXT_RECORD;
END IF;
END LOOP;
END IF;
/*****************************************************/ GO_BLOCK('SYS');
FIRST_RECORD;
LAST := FALSE;
WHILE NOT LAST LOOP IF :SYS.FLAG = 1 THEN key_all_data := '';
FIRST_ROUND := 0;
SELECT SWF_SEQ.NEXTVAL INTO :KEY.SWF_SEQ
FROM DUAL;
IF FIRST_SEQ = 0 THEN :KEY.FROM_SWF_SEQ := :KEY.SWF_SEQ;
FIRST_SEQ := 1;
END IF;
FOR C2REC IN C2 LOOP /*******************************/
SELECT COUNT(*) INTO MESS_CTR
FROM SWF_DETL
WHERE BRA_CODE = :SYS.BRA_CODE
AND REF_TYPE = :SYS.REF_TYPE
AND REF_YEAR = :SYS.REF_YEAR
AND REF_NUM = :SYS.REF_NUM
AND MESS_TYPE_CODE = C2REC.MESS_TYPE_CODE
AND SEQ_NUM = C2REC.SEQ_NUM;
IF MESS_CTR > 0 THEN CHECK_MESS_USA(:SYS.BRA_CODE,:SYS.REF_TYPE,:SYS.REF_YEAR, :SYS.REF_NUM,C2REC.MESS_TYPE_CODE,C2REC.SEQ_NUM, USA_STATUS);
IF USA_STATUS = 0 THEN CHECK_MESS_TEXT(C2REC.MESS_TYPE_CODE,C2REC.SEQ_NUM, C2REC.FROM_SWF_CODE,C2REC.TO_SWF_CODE, C2REC.PRTY_CODE,TEXT_STATUS);
IF TEXT_STATUS = 0 THEN A07SWF60 ( :sys.BRA_CODE, :sys.REF_TYPE, :sys.REF_YEAR, :sys.REF_NUM, c2rec.MESS_TYPE_CODE, c2rec.SEQ_NUM, :global.WST_TELL_ID, :key.SWF_SEQ, :GLOBAL.CLI_BANK_DATE, :SYS.ALL_DATA, RETURN_STATUS);
IF RETURN_STATUS <> 0 THEN RETURN_STATUS := -SQLCODE;
RETURN;
END IF;
UPDATE SWF_MESS
SET RTGS_FLAG = :SYS.RTGS_FLAG
WHERE BRA_CODE = :SYS.BRA_CODE
AND REF_TYPE = :SYS.REF_TYPE
AND REF_YEAR = :SYS.REF_YEAR
AND REF_NUM = :SYS.REF_NUM
AND MESS_TYPE_CODE = C2REC.MESS_TYPE_CODE
AND SEQ_NUM = C2REC.SEQ_NUM;
IF FIRST_ROUND = 0 THEN dbms_lob.createtemporary(key_all_data, TRUE);
dbms_lob.open(key_all_data, 1);
dbms_lob.append(key_all_data,:SYS.ALL_DATA);
FIRST_ROUND := 1;
ELSE ESCP := '';
IF :SYS.PRE_LEN <> 0 THEN REM := :SYS.PRE_LEN/512;
IF (REM - TRUNC(REM)) <> 0 THEN SPC := 512 - (:SYS.PRE_LEN - (TRUNC(REM) * 512));
FOR I IN 1..SPC LOOP ESCP := ESCP||CHR(32);
END LOOP;
END IF;
ELSE ESCP := '';
END IF;
/*********************************************/ dbms_lob.append(key_all_data,ESCP);
dbms_lob.append(key_all_data,:SYS.ALL_DATA);
END IF;
ELSE RETURN_STATUS := TEXT_STATUS;
RAISE SWF_FAIL;
END IF;
ELSE RETURN_STATUS := USA_STATUS;
RAISE SWF_FAIL;
END IF;
ELSE RAISE NO_DETAIL;
END IF;
END LOOP;
IF :TIT.INP_SECU_CODE = 2 THEN FILE_NAME := 'OUT'||LPAD(TO_CHAR(:KEY.SWF_SEQ),5,'0')||'.ABI';
ELSE --alliance
FILE_NAME := 'OUT'||LPAD(TO_CHAR(:KEY.SWF_SEQ),5,'0')||'.MSG';
END IF;
-- FILE_DIR := '/u/oracle/dev/spool';
--LEN := NVL(LENGTH(key_all_data), 0);
LEN := NVL(dbms_lob.getlength(key_all_data),0);--
CTR := LEN / 1000 ;
IF CTR <= 1 THEN CTR := 1;
SUB := LEN;
ELSE IF (CTR - TRUNC(CTR)) <> 0 THEN CTR := TRUNC(CTR) + 1;
END IF ;
SUB := 1000;
END IF;
OPEN_TYPE := 1;
J := 1;
IF :tit.eol_swf <> 'CHR(13)||CHR(10)' THEN BEGIN key_all_data := replace(key_all_data,chr(13)||chr(10),chr(substr(:tit.eol_swf,5,2)));
exception WHEN others THEN NULL;
END;
END IF;
comp_file := TEXT_IO.FOPEN(NAME_IN('GLOBAL.PRINT_PATH')||FILE_NAME, 'W');
FOR I IN 1..CTR LOOP MESS_TEXT := dbms_lob.substr(key_all_data,1000,j);--
TEXT_IO.PUT (comp_file, mess_text);
OPEN_TYPE := 2;
IF return_status = 0 THEN J := J + 1000;
ELSIF RETURN_STATUS IN(1196,
1197,
1198,
1199) THEN RAISE PRINT_ERR;
ELSE RAISE PRINT_ERR1;
END IF ;
END LOOP;
/************** CLOSING THE FILE *****************************/ IF :SYS.LEN <> 0 THEN REM := :SYS.LEN/512;
IF (REM - TRUNC(REM)) <> 0 THEN SPC := 512 - (:SYS.LEN - (TRUNC(REM) * 512));
CLOSE_TYPE := 1;
ELSE CLOSE_TYPE := 3;
END IF;
ELSE CLOSE_TYPE := 3;
END IF;
OERR := 0;
IF CLOSE_TYPE = 1 THEN
FOR I IN 1..SPC LOOP ESCP := CHR(32);
Text_IO.PUT(COMP_FILE,ESCP);
END LOOP;
END IF;
TEXT_IO.FCLOSE(comp_file);
RETURN_STATUS := OERR;
IF OERR <> 0 THEN IF OERR = 1199 THEN RAISE PRINT_ERR;
ELSE RAISE PRINT_ERR1;
END IF ;
END IF;
IF NVL(RTGS_FLA,0)=2
AND :SYS.RTGS_FLAG=1 THEN send_swf_mess(FILE_NAME, RTGS_IPADD, RTGS_UNAME, RTGS_PASS, SWF_FTP_COM,IS_SEC, RET_STA);
ELSE send_swf_mess(FILE_NAME, SWF_ADD, SWF_UNAME, SWF_PASS, SWF_FTP_COM, IS_SEC, RET_STA);
END IF;
:SYS.LEN := 0;
GO_BLOCK('SYS');
END IF;
IF :SYSTEM.LAST_RECORD = 'TRUE' THEN LAST := TRUE;
ELSE NEXT_RECORD;
END IF;
END LOOP;
dbms_lob.close(key_all_data);
FIRST_RECORD;
:KEY.TO_SWF_SEQ := :KEY.SWF_SEQ;
EXCEPTION WHEN FORM_TRIGGER_FAILURE THEN RAISE FORM_TRIGGER_FAILURE ;
WHEN NO_DETAIL THEN :TIT.COMMIT := 1;
ROLLDATA;
RETURN_STATUS := 374;
:GLOBAL.TAB_ENT := '0374';
DISPLAY_MSG;
RETURN;
WHEN SWF_FAIL THEN :TIT.COMMIT := 1;
ROLLDATA;
display_err(return_status);
RETURN;
WHEN PRINT_ERR THEN ROLLDATA;
:global.tab_ent :=return_status ;
display_msg ;
:TIT.COMMIT := 1 ;
WHEN PRINT_ERR1 THEN ROLLDATA;
display_err(return_status);
:TIT.COMMIT := 1 ;
WHEN OTHERS THEN :TIT.COMMIT := 1;
ROLLDATA;
RETURN_STATUS := -SQLCODE;
display_err(return_status);
RETURN;
END;
This is a screenshot of the error message that displays to one of the users whilst the other user sees Operation successful.
You will get that error if your C2 cursor doesn't find anything; the CLOB is never opened because you don't go into the loop, but you still do the dbms_lob.close, and that will throw that ORA-22275 error:
DECLARE
key_all_data CLOB;
BEGIN
dbms_lob.close(key_all_data);
END;
/
ORA-06502: PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275
You don't really need to open your temporary CLOB, which means you don't need to close it either; but you should free it either way. So you can remove the line:
dbms_lob.open(key_all_data, 1);
and change:
dbms_lob.close(key_all_data);
to:
if dbms_lob.istemporary(key_all_data) = 1 then
dbms_lob.freetemporary(key_all_data);
end if;
If you want to keep the open then test the close part too:
if dbms_lob.istemporary(key_all_data) = 1 then
if dbms_lob.isopen(key_all_data) = 1 then
dbms_lob.close(key_all_data);
end if;
dbms_lob.freetemporary(key_all_data);
end if;
You might think that if you you have created the temporary CLOB and opened it then it will be open and therefore can be closed; but this:
key_all_data := replace(...);
... is replacing one temporary CLOB with another, which is not explicitly open. You can look at the istemporary and isopen values to see what is happening. You could look at dbms_lob.fragment_replace etc. instead, or skip the open/close and don't worry about it...
It isn't clear if one call to this procedure is changing what the next call sees in the cursor (since the update doesn't seem to do anything) but the form or something this calls might be doing more work that does.
There are probably lots of other issues and comments - exception when others then null jumps out as a really bad idea - but they're getting a bit off-topic.
I think that you are trying append null values to clob variable.
declare
key_all_data clob := 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
v varchar2(10);
begin
begin
dbms_lob.append(key_all_data,v); -- exception
exception when others then
dbms_output.put_line(sqlerrm);
end;
for rec in ( select empty_clob() c from dual) loop -- run - ok
dbms_lob.append(key_all_data,rec.c);
end loop;
begin
for rec in ( select to_clob(null) c from dual) loop -- exception
dbms_lob.append(key_all_data,rec.c);
end loop;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end;

make function in oracle

if i have this code.
CREATE OR REPLACE FUNCTION checkprima (K NUMBER)
RETURN NUMBER
IS
I NUMBER;
hasil NUMBER;
output NUMBER;
BEGIN
DBMS_OUTPUT.PUT('2 ');
FOR I IN 2..K
LOOP
IF MOD(I,2)>0 THEN
IF I > 7 THEN
IF MOD(I,3)>0 AND MOD(I,5)>0 AND MOD(I,7)>0 THEN
hasil:=DBMS_OUTPUT.PUT(TO_CHAR(I) || ' ');
END IF;
ELSE
hasil:=DBMS_OUTPUT.PUT(TO_CHAR(I) || ' ');
END IF;
END IF;
END LOOP;
output:=hasil;
RETURN output;
END checkprima;
/
example
if i input select checkprima('20').
i want the output is like this
2 3 5 7 11 13 17 19
CREATE OR REPLACE PROCEDURE tes (K IN NUMBER) IS
BEGIN
DBMS_OUTPUT.PUT('2 ');
FOR I IN 2..K LOOP
IF MOD(I,2)>0 THEN
IF I > 7 THEN
IF MOD(I,3)>0 AND MOD(I,5)>0 AND MOD(I,7)>0 THEN
DBMS_OUTPUT.PUT(TO_CHAR(I) || ' ');
END IF;
ELSE
DBMS_OUTPUT.PUT(TO_CHAR(I) || ' ');
END IF;
END IF;
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END tes;
Call the procedure --> exec tes(20);

Resources