PL/SQL UTL_FILE read trouble on anonymous block - oracle

DECLARE
data_line VARCHAR2(200); -- Data line read from input file
data_file UTL_FILE.FILE_TYPE; -- Data file handle
my_dir VARCHAR2(250); -- Directory containing the data file
my_filename VARCHAR2(50);
BEGIN
my_dir := 'c:\temp';
my_filename := 'Lab4AData.dat';
my_file := UTL_FILE.FOPEN(my_dir, my_filename, 'r');
LOOP;
UTL_FILE.GET_LINE(data_file, data_line);
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('Finished');
exit;
END LOOP;
END;
/
The problem is I cannot even get this anonymous block of code started. To start, I'm just trying to open my data file and read it, then build from there. But I can't even get the file open.
SQL Developer Error Report starts right off with
Error starting at line 5 in command:
DECLARE
then repeats the block of code and adds this:
ORA-06550: line 12, column 8:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
The symbol "exit" was substituted for ";" to continue.
ORA-06550: line 15, column 3:
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with <an identifier>
<a double-quoted delimited-i
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

Try the following:
DECLARE
data_line VARCHAR2(200); -- Data line read from input file
data_file UTL_FILE.FILE_TYPE; -- Data file handle
my_dir VARCHAR2(250); -- Directory containing the data file
my_filename VARCHAR2(50);
BEGIN
my_dir := 'c:\temp';
my_filename := 'Lab4AData.dat';
data_file := UTL_FILE.FOPEN(my_dir, my_filename, 'r');
LOOP
UTL_FILE.GET_LINE(data_file, data_line);
-- add code to do something with data_line here
END LOOP;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('Finished');
UTL_FILE.FCLOSE(data_file);
END;
#ShannonSeverance's comments about using directory objects with UTL_FILE.FOPEN are appropriate, except in the instance where your DBA has not embraced their use and insists on sticking with the "tried and true" INIT.ORA parameter UTL_FILE_DIR. Don't ask me how I know... :-)
Share and enjoy.

Related

how to use refcursor?

I am trying to run an explain plan using a wrapper, but getting compilation error. Can you please help troubleshoot this error.
set serveroutput on
var my_cur refcursor;
begin
:my_cur:=sys.run_xplan('select * from hcc.Tab_A');
end;
/print my_cur
Error:
begin
:my_cur:=sys.run_xplan('select * from hcc.Tab_A');
end;
/print my_cur
Error report -
ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "/" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<an identifier> <a double-quoted delimited-identifier>
<a bind variable> << continue close current delete fetch lock
insert open rollback savepoint set sql execute commit forall
merge pipe purge
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.

ORA-06561 while executing PL/SQL procedure

I have this procedure:
create or replace
PROCEDURE P_P2
IS
v_str varchar2(1000);
v_file_name varchar2(1000);
BEGIN
P_P1(v_str, 'EXPORT_CSV',v_file_name);
v_str := 'select * from H----)';
v_file_name := 'H_'||to_char(sysdate,'DD-MM-YYYY')||'.csv';
END;
I am getting error "ORA-06561: given statement is not supported by package DBMS_SQL" when I execute it:
Error starting at line : 165 in command -
exec P_P1
Error report -
ORA-06561: given statement is not supported by package DBMS_SQL
ORA-06512: at "SYS.DBMS_SQL", line 1120
ORA-06512: at "BIDB.P_P1", line 40
ORA-06512: at "BIDB.P_P2", line 7
ORA-06512: at line 1
06561. 00000 - "given statement is not supported by package DBMS_SQL"
*Cause: Attempting to parse an unsupported statement using procedure
PARSE provided by package DBMS_SQL.
*Action: Only statements which begin with SELECT, DELETE, INSERT, UPDATE,
LOCK, BEGIN, DECLARE or << (PL/SQL label delimiter) are supported.
I cannot see why. What am I doing wrong?
You haven't shown what P_P1 is doing, but from what you have shown, your P_P2 procedure may just be calling it too early; you have a call to P_P1 which uses v_str as a parameter, but it's null at that point - you set it after the call that uses it.
That means that somewhere in P_P1 you are probably ending up doing the equivalent of:
dbms_sql.parse(c, null, dbms_sql.native);
which throws the error you are seeing. You're doing the same with v_filename, which will presumably cause other issues.
So for a start, swap those lines around so the procedure is called last:
create or replace
PROCEDURE P_P2
IS
v_str varchar2(1000);
v_file_name varchar2(1000);
BEGIN
v_str := 'select * from H----)';
v_file_name := 'H_'||to_char(sysdate,'DD-MM-YYYY')||'.csv';
-- call this AFTER populating the variables
P_P1(v_str, 'EXPORT_CSV',v_file_name);
END;
/
You may have other problems of course - the table name looks odd, both because of the dashes and the closing parenthesis; but you may have just hidden the real name oddly. You seem to have changed the procedure names too (though inconsistently, as your exec seems to be calling the wrong one...).

PL/SQL For & When Error

This is my code,
Declare
For num IN 1..10 LOOP
Continue When Mod(num,2)!=0;
DBMS_OUTPUT.PUT_LINE(num);
END LOOP;
END;
/
I am getting the following error:
SQL> # E:\dbms\f7.sql
For num IN 1..10 LOOP
*
ERROR at line 2:
ORA-06550: line 2, column 1:
PLS-00103: Encountered the symbol "FOR" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The symbol "begin" was substituted for "FOR" to continue.
ORA-06550: line 3, column 10:
PLS-00103: Encountered the symbol "WHEN" when expecting one of the following:
:= . ( # % ;
Please someone give me a working code so I can execute !!!
You don't need the declare block since you aren't declaring any variables, but you're missing the begin keyword:
BEGIN -- Here
FOR num IN 1..10 LOOP
Continue When Mod(num,2)!=0;
DBMS_OUTPUT.PUT_LINE(num);
END LOOP;
END;
/

Warning: Package Body created with compilation errors

Please check my package and procedures.
My package:
create or replace package transaction1 as
procedure enter_transaction(acc number, kind varchar2, amount number);
procedure apply_transaction;
end;
/
This is my body:
create or replace package body transaction1 as
procedure enter_transaction(acc in number, kind in varchar2, amount in number)
is
begin
end;
procedure apply_transaction
is
begin
end;
end;
/
What is the warning? Why?
If you see a warning: Errors: check compiler log
then run show errors command and you will see the error log :
7/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
14/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
In case of your package body Oracle complains because BEGIN END blocks don't contain any commands.
In Oracle the BEGIN-END block must contain at least one command. Could be NULL, if you don't want to run anything (and don't forget to place a semicolon after NULL command):
PROCEDURE ......
IS
BEGIN
NULL;
END;

Error is occurring during execution of procedure in oracle

I am getting the error during executing the below procedure.
CREATE OR REPLACE PROCEDURE P_SUMIT (P_FEED IN FEED.FEED_ID%TYPE, P_OPCO_ID IN FEED.OPCO_ID%TYPE)
AS
BEGIN
DECLARE V_PCF_PATTERN FEED.PCF_PATTERN%TYPE;
DECLARE CURSOR C_FEED FOR SELECT PCF_PATTERN FROM FEED WHERE FEED_ID=P_FEED AND OPCO_ID=P_OPCO_ID;
OPEN C_FEED;
LOOP
FETCH C_FFED INTO V_PCF_PATTERN;
EXIT WHEN C_FEED%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_PCF_PATTERN);
END LOOP;
CLOSE C_FEED;
END;
Error logs :
3/1 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior external language
The symbol "begin" was substituted for "DECLARE" to continue.
4/1 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
Please assist.
DECLARE statements should be after AS keyword.
CREATE OR REPLACE PROCEDURE P_SUMIT (P_FEED IN FEED.FEED_ID%TYPE, P_OPCO_ID IN FEED.OPCO_ID%TYPE)
AS
V_PCF_PATTERN FEED.PCF_PATTERN%TYPE;
CURSOR C_FEED IS SELECT PCF_PATTERN FROM FEED WHERE FEED_ID=P_FEED AND OPCO_ID=P_OPCO_ID;
BEGIN
OPEN C_FEED;
LOOP
FETCH C_FEED INTO V_PCF_PATTERN;
EXIT WHEN C_FEED%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_PCF_PATTERN);
END LOOP;
CLOSE C_FEED;
END P_SUMIT;
/
There's no need for an explicit cursor here, or for parameters to be prefixed with "p_". Implicit cursors are quicker and more robust to write, and perform better.
create or replace procedure p_sumit (
feed_id in feed.feed_id%type,
opco_id in feed.opco_id%type)
as
begin
for c_feed in (
select pcf_pattern
from feed
where feed_id = p_sumit.feed_id and
opco_id = p_sumit.opco_id)
loop
dbms_output.put_line(c_feed.pcf_pattern);
end loop;
end;

Resources