UTL_FILE Error during writing data to file [duplicate] - oracle

This question already has answers here:
PLS-00201: identifier UTIL_FILE must be declared
(5 answers)
Closed 7 years ago.
Team,
Below is the sample code, while trying to write data from cursor to file -
I am currently using 11g Express Edition
CTEST is the directory created and empdata is the declaration variable.
SQL> declare
2 empData utl_file.file_type;
3 BEGIN
4 empData := UTL_FILE.FOPEN('CTEST','empdata.csv','W');
5 for emp IN ( select table_name from user_tables) LOOP
6 UTL_FILE.PUT_LINE(empData,emp.table_name);
7 END LOOP;
8 UTL_FILE.FCLOSE(empData);
9 end;
10 /
empData utl_file.file_type;
*
ERROR at line 2:
ORA-06550: line 2, column 13:
PLS-00201: identifier 'UTL_FILE' must be declared
ORA-06550: line 2, column 13:
PL/SQL: Item ignored
ORA-06550: line 4, column 6:
PLS-00320: the declaration of the type of this expression is incomplete or

Seems like you don't have the privileges on UTL_FILE,
from sys user run this command,
GRANT EXECUTE ON UTL_FILE TO <username> ;

Related

Why does dbms_sql.parse containing incorrect PL/SQL block with bind variables succeed unexpectedly?

The PL/SQL block below fails as expected:
SQL> declare
2 i int;
3 begin
4 i := dbms_sql.open_cursor;
5 dbms_sql.parse(i,'begin dontexist; dbms_output.put(''a''); end;',1);
6 dbms_sql.close_cursor(i);
7 end;
8 /
declare
*
FOUT in regel 1:
.ORA-06550: Regel 1, kolom 7:
PLS-00201: identifier 'DONTEXIST' must be declared.
ORA-06550: Regel 1, kolom 7:
PL/SQL: Statement ignored.
ORA-06512: in "SYS.DBMS_SQL", regel 1120
ORA-06512: in regel 5
Because I don't have a procedure called DONTEXIST. My question is then why does this next PL/SQL block complete successfully?
SQL> declare
2 i int;
3 begin
4 i := dbms_sql.open_cursor;
5 dbms_sql.parse(i,'begin dontexist; dbms_output.put(:a); end;',1);
6 dbms_sql.close_cursor(i);
7 end;
8 /
PL/SQL-procedure is geslaagd.
The difference is the use of the bind variable instead of a constant, but I'd like to know why this makes a difference.
This is Oracle 12.1.0.2
Looks like the parse is only syntactic for anon blocks with binds, and the full semantic check is deferred until execution.
Still, that's not a behaviour we want so Bug 26669757 raised.

error PLS-00201: identifier kurtwb must be declared [duplicate]

This question already has an answer here:
PLS-00201: identifier 'USER INPUT' must be declared
(1 answer)
Closed 10 months ago.
I am executing one PL/SQL program but i am getting the error PLS-00201: identifier 'KURTWB' must be declared
DECLARE
create_user dba_users%ROWTYPE;
model_id varchar2(20);
user_name varchar2(20);
BEGIN
model_id:=&model_id;
user_name:=&user_name;
Select * INTO create_user from dba_users where username=model_id;
EXECUTE IMMEDIATE 'create user user_name identified by password
user_name default tablespace create_user.default_tablespace
temporary tablespace create_user.temporary_tablespace profile
create_user.profile;';
END;
/
Below is the value entered by me
Enter value for model_id: kurtwb
old 6: model_id:=&model_id;
new 6: model_id:=kurtwb;
Enter value for user_name: rohit
old 7: user_name:=&user_name;
new 7: user_name:=rohit;
Below is the error that i am getting
ERROR at line 6:
ORA-06550: line 6, column 11:
PLS-00201: identifier 'KURTWB' must be declared
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
ORA-06550: line 7, column 12:
PLS-00201: identifier 'ROHIT' must be declared
ORA-06550: line 7, column 1:
PL/SQL: Statement ignored
You have entered kurtwbas a value of the substitution variable named &model_id.
SQLPLus (or Oracle-SQL-Developer`) substitutes this variable in this line:
BEGIN
model_id:=&model_id;
....
....
with the entered value kurtwb. After the substitution the code looks like this:
BEGIN
model_id:=kurtwb;
....
....
since kurtwb is not defined anywhere, you get PLS-00201: identifier 'KURTWB' must be declared error.
I guess you want to store kurtwb as a string in model_id variable, in this case you must use apostropher aroud the substitution variables:
BEGIN
model_id:='&model_id';
....
....

Why anonymous oracle stored procedure is working but named not

I'm trying to create a simple SP in the Oracle 11.2.0.4.0 which will execute a simple update statement like
update t set col1 = 1 where col2 = 2;
When i'm doing it in the anonymous stored procedure it works fine
begin
update t set col1 = 1 where col2 = 2;
end;
But if I'm trying to create a procedure
create or replace procedure p as
begin
update t set col1 = 1 where col2 = 2;
end;
And trying to run it, i'm gets a lot of errors:
ORA-06550: line 9, column 13:
PLS-00201: identifier 'SYS.DBMS_SQL' must be declared
ORA-06550: line 9, column 13:
PL/SQL: Item ignored
ORA-06550: line 16, column 13:
PLS-00201: identifier 'SYS.DBMS_LOB' must be declared
ORA-06550: line 16, column 13:
PL/SQL: Item ignored
Can any one explain how it happened, why sp is trying to get objects from SYS schema? Why it works in case of anonymous sp and not working in case of named SP?

How to call procedure that contain SYS_REFCURSOR [duplicate]

This question already has answers here:
How to test an Oracle Stored Procedure with RefCursor return type?
(6 answers)
Closed 5 years ago.
I create the procedure to output multi rows and columns.
create or replace PROCEDURE MYPROC(
C1 OUT SYS_REFCURSOR )
AS
BEGIN
OPEN C1 FOR SELECT * FROM A_TABLE;
END MYPROC;
There is no error when I compiled it.
But I Can't call my procedure as normal like
Exec MYPROC;
I've got this error.
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'MYPROC'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
But when I run without script, it displayed my expected result.
So please help me how can I execute this procedure?
Thank You.
Error is because you are not passing any parameter to the procedure when it expects 1.
You need define a refcursor variable and then pass the variable into the procedure and finally read it.
var cur refcursor;
exec MYPROC(c1 => :cur);
print cur;
or
var cur refcursor;
begin
MYPROC(c1 => :cur);
end;
/
print cur;

PL/SQL : i have a function but there is an error : "in a procedure,RETURN can not contain an expression"

Here is my code:
CREATE OR REPLACE FUNCTION customer_city_function(city_in IN VARCHAR2)
RETURN NUMBER
AS
number_cus NUMBER := 0;
CURSOR cus_cur IS
SELECT COUNT(*)
FROM customer
WHERE customer_city = city_in;
BEGIN
IF city_in IS NOT NULL THEN
OPEN cus_cur;
FETCH cus_cur INTO number_cus;
CLOSE cus_cur;
END IF;
RETURN number_cus;
END;
/
and here is warnings:
Error starting at line : 1 in command -
CREATE OR REPLACE FUNCTION customer_city_function(city_in IN VARCHAR2)
RETURN NUMBER
AS
number_cus NUMBER := 0
Error report -
SQL Command: functıon CUSTOMER_CITY_FUNCTION
Failed: Warning: executing is completed with a warning
Error starting at line : 5 in command -
CURSOR cur_cur IS
Error report -
Unknown Command
Error starting at line : 6 in command -
SELECT COUNT(*)
FROM costumer
WHERE customer_city=city_in
Error at Command Line : 8 Column : 25
Error report -
SQL Error: ORA-00904: "CITY_IN": undefined variable
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error starting at line : 9 in command -
BEGIN
IF city_in IS NOT NULL
THEN
OPEN cus_cur;
FETCH cus_cur INTO number_cus;
CLOSE cus_cur;
END IF;
RETURN (number_cus);
END;
Error report -
ORA-06550: row 2, column 6:
PLS-00201: 'CITY_IN' variable should been defined
ORA-06550: row 2, column 3:
PL/SQL: Statement ignored
ORA-06550: row 8, column 1:
PLS-00372: in a procedure, RETURN can not contain an expression
ORA-06550: row 8, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Where is my mistake? I can't find it, it doesn't make any sense.
(I translated this warning message from my language. I hope I did it right.)
I have just tried it in Command Window and it works. Why doensn't it work in Oracle SQL Developer sql worksheet?
There is nothing wrong with your posted code. The issue might be with your client or the way you are compiling the code.
As you have mentioned PL/SQL Developer in the tags, it might be possible that you have some extra characters in the SQL Worksheet and you are compiling the function as a script, thus the compiler finds it erroneous.
Here is a demo in SQL*Plus, and there is no error:
SQL> CREATE OR REPLACE FUNCTION customer_city_function(i_deptno IN number)
2 RETURN NUMBER
3 AS
4 number_cus NUMBER := 0;
5 CURSOR cus_cur IS
6 SELECT COUNT(*)
7 FROM emp
8 WHERE deptno=i_deptno;
9 BEGIN
10 IF i_deptno IS NOT NULL
11 THEN
12 OPEN cus_cur;
13 FETCH cus_cur INTO number_cus;
14 CLOSE cus_cur;
15 END IF;
16 RETURN number_cus;
17 END;
18 /
Function created.
SQL> sho err
No errors.
SQL> SELECT customer_city_function(10) FROM DUAL;
CUSTOMER_CITY_FUNCTION(10)
--------------------------
3
SQL>
The only difference in my code is that I have used EMP table instead of CUSTOMERS table and the input parameter is DEPTNO instead of CITY_IN. Rest everything is same and function compiles and executes without any errors.

Resources