Utl_File not generating file in the server path - database

I have executed a block of code which generates a simple text file using utl_file package
with a word 'test' and outputs the file to the location in server.
When i run the procedure it compiles successfully but the file is not
generated in the path.
set serveroutput on
declare
l_file utl_file.file_type;
l_dir varchar2(500):='WMS_IFILEOUT';
l_file_name varchar2(500):='test.txt';
begin
l_file :=utl_file.fopen(l_dir,l_file_name,'w',32767);
utl_file.put_line(l_file,'test123');
utl_file.fclose(l_file);
end;
The path and directory are available in the dba_directories
and read and write privileges are available on it.
I noticed that when i print
show parameter utl_file
then no values are displayed alongside to it.
Do I have to set this parameter in order to generate the files in the server path.
If so, can you please tell how to set it.
Thanks

I tried code you posted; the only modification was to rename directory).
SQL> DECLARE
2 l_file UTL_FILE.file_type;
3 l_dir VARCHAR2 (500) := 'DPDIR';
4 l_file_name VARCHAR2 (500) := 'test.txt';
5 BEGIN
6 l_file :=
7 UTL_FILE.fopen (l_dir,
8 l_file_name,
9 'w',
10 32767);
11 UTL_FILE.put_line (l_file, 'test123');
12 UTL_FILE.fclose (l_file);
13 END;
14 /
PL/SQL procedure successfully completed.
SQL>
Result: file is here:
So ... no, there's nothing else you should do. Everything you wrote seems to be just fine (from my point of view).
You said something about "show parameter utl_file" - what is that, exactly? UTL_FILE is a package, and you have to have EXECUTE privilege on it. You already have it, otherwise procedure wouldn't work at all.

Related

"ORA-01008: not all variables are mapped " Confused about this error message. How to enter bind variable as null to initiate the function?

I have a db function that populates data from active directory into a table in the database. It works fine without giving any errors.
Next step is to schedule a job in db so that it is run everyday automatically. When I do that, I get this error: ORA-01008: not all variables are mapped
The code I am using in the PL/SQL block is this:
DECLARE
v_Return VARCHAR2(200);
BEGIN
v_Return := PRE_JOB_FUNCTION();
:v_Return := v_Return;
END;
I think that the issue is that v_Return needs to be Null to begin the execution of this function but I am confused how to do that. Can someone please help?

Copying data from a text file to a CLOB in PL/SQL

I have used the following code to copy the text from a file to a CLOB. However it is giving me a PL/SQL numeric or value error at the position where writeappend is performed.
declare
l_fhandle utl_file.file_type;
l_clob CLOB;
l_buffer VARCHAR2(4096);
BEGIN
l_fhandle := utl_file.fopen('/data',
'FILE.TXT',
'R');
dbms_lob.createtemporary(l_clob, TRUE, DBMS_LOB.CALL);
LOOP
BEGIN
utl_file.get_line(L_FHANDLE, l_buffer);
dbms_output.put_line(l_buffer);
dbms_lob.writeappend(l_clob, length(l_buffer), l_buffer);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('Inside No data found');
INSERT INTO TAB_CLOB_FILE
(FILENAME, BODYCONT)
VALUES
('FILE', l_clob);
dbms_output.put_line('Inserted data into table');
EXIT;
END;
END LOOP;
END;
Please help me figure out what is wrong
Your problem is most likely here:
utl_file.fopen('/data', 'FILE.TXT','R');
The fist parameter is the name of an oracle directory OBJECT, not a physical path to a directory. From the Oracle docs, "Directory location of file. This string is a directory object name and is case sensitive. The default is uppercase. Read privileges must be granted on this directory object for the UTL_FILE user to run FOPEN." The incorrect call should have thrown an exception though.
You need to first create a directory object such as:
create directory MY_DIR as '/data';
Then change the fopen call to: utl_file.fopen('MY_DIR', 'FILE.TXT','R');
You can read about directory objects here.

utl_file.fopen without 'create directory ... as ...'

Hi, everybody.
I am new to PL/SQL and Oracle Databases.
I need to read/write file that exists on server so i'm using utl_file.fopen('/home/tmp/','text.txt','R') but Oracle shows error 'invalid directory path'.
Main problem is that i have only user privileges, so i cant use commands like create directory user_dir as '/home/temp/' or view utl_file_dir with just show parameter utl_file_dir;
I used this code to view utl_file_dir:
SQL> set serveroutput on;
SQL> Declare
2 Intval number;
3 Strval varchar2 (500);
4 Begin
5 If (dbms_utility.get_parameter_value('utl_file_dir', intval,strval)=0)
6 Then dbms_output.put_line('value ='||intval);
7 Else dbms_output.put_line('value = '||strval);
8 End if;
9 End;
10 /
and output was 'value = 0'.
I google'd much but didnt find any solution of this problem, so i'm asking help here.
To read file i used this code:
declare
f utl_file.file_type;
s varchar2(200);
begin
f := utl_file.fopen('/home/tmp/','text.txt','R');
loop
utl_file.get_line(f,s);
dbms_output.put_line(s);
end loop;
exception
when NO_DATA_FOUND then
utl_file.fclose(f);
end;
If you do not have permission to create the directory object (and assuming that the directory object does not already exist), you'll need to send a request to your DBA (or someone else that has the appropriate privileges) in order to create a directory for you and to grant you access to that directory.
utl_file_dir is an obsolete parameter that is much less flexible than directory objects and requires a reboot of the database to change-- unless you're using Oracle 8.1.x or you are dealing with a legacy process that was written back in the 8.1.x days and hasn't been updated to use directories, you ought to ignore it.

How do I get the result output from an SQL BACKUP command into a Delphi program?

The output of sql commands that is visible to users who interactively run SQL commands from SQL Server Management studio, is different than the output you get back from executing an ADO command or ADO query object.
USE [DBNAME]
BACKUP DATABASE [DBNAME] TO
DISK = 'C:\SqlBackup\Backup.mdf'
The successful completion output is like this:
Processed 465200 pages for database 'DBNAME', file 'filename' on file 2.
Processed 2 pages for database 'DBNAME', file 'filename_log' on file 2.
BACKUP DATABASE successfully processed 465202 pages in 90.595 seconds (40.116 MB/sec).
When I execute either a TADOCommand or TADOQuery with the CommandText or SQL set as above, I do not get any such output. How do I read this "secondary output" from the execution of an SQL command? I'm hoping that perhaps via some raw ADO operations I might be able to execute a command and get back the information above, for success, as well as any errors in performing an Sql backup.
Update: The answer below works better for me than my naive attempt, which did not work, using plain Delphi TADOCommand and TADOConnection classes:
create TADOCommand and TADOConnection.
execute command.
get info-messages back.
The problem I experienced in my own coding attempts, is that my first command is "use dbname" and the only recordset I traversed in my code, was the results of the "use dbname" command, not the second command I was executing. The accepted answer below traverses all recordsets that come back from executing the ADO command, and thus it works much better. Since I'm doing all this in a background thread, I actually think it's better to create the raw Com Objects anyways, and avoid any VCL entanglement in my thread. The code below could be a nice component if anybody is interested, let me know and I might make an open source "SQL Backup for Delphi" component.
Here is an example. I've tested it with D7 and MSSQL2000. And it adds to Memo1 all messages from server:
29 percent backed up.
58 percent backed up.
82 percent backed up.
98 percent backed up.
Processed 408 pages for database 'NorthWind', file 'Northwind' on file 1.
100 percent backed up.
Processed 1 pages for database 'NorthWind', file 'Northwind_log' on file 1.
BACKUP DATABASE successfully processed 409 pages in 0.124 seconds (26.962 MB/sec).
Also if it takes a long time consider to implement a WHILE loop not in the main thread.
uses AdoInt,ComObj;
.....
procedure TForm1.Button1Click(Sender: TObject);
var cmd : _Command;
Conn : _Connection;
RA : OleVariant;
rs :_RecordSet;
n : Integer;
begin
Memo1.Clear;
Conn := CreateComObject(CLASS_Connection) as _Connection;
Conn.ConnectionString := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NorthWind;Data Source=SQL_Server';
Conn.Open(Conn.ConnectionString,'','',Integer(adConnectUnspecified));
cmd := CreateComObject(CLASS_Command) as _Command;
cmd.CommandType := adCmdText;
cmd.Set_ActiveConnection(Conn);
cmd.CommandText := 'BACKUP DATABASE [NorthWind] TO DISK = N''c:\sql_backup\NorthWind'' WITH INIT , NOUNLOAD , NAME = N''NortWind backup'', NOSKIP , STATS = 10, NOFORMAT;';
rs:=cmd.Execute(RA,0,Integer(adCmdText));
while (rs<>nil) do
begin
for n:=0 to(Conn.Errors.Count-1)do begin
Memo1.Lines.Add(Conn.Errors.Item[n].Description);
end;
rs:=rs.NextRecordset(RA);
end;
cmd.Set_ActiveConnection(nil);
Conn.Close;
cmd := nil;
Conn := nil;
end;
I've found this thread (Russian) for stored procedure and correct it for BACKUP command.

Oracle Stored Procedure "Encountered the symbol... " error

After a looong research i have decided i have no idea what causes the following problem! I want to create a stored procedure that will increase the value of a column if that satisfied a statement.I can run the procedure as a anonymous block but i cant run it as a stored procedure!
Here is the stored procedure code:
1 create or replace
2 Procedure RaiseSalary
3 Is
4 Salary number;
5 Cursor RaiseCursor
6 Is
7 Select Salary_A From Person Where Salary_A<400 for update of Salary_A;
8 Begin
9 Open RaiseCursor;
10 Loop
11 Fetch RaiseCursor Into Salary;
12 Exit When RaiseCursor%Notfound;
13 Update Person Set Salary_A=(Salary + 200) Where Current Of RaiseCursor;
14 End Loop;
15 Close RaiseCursor;
16 Commit;
17 End;
I am getting an error at line 3 that says "Encountered the symbol "SALARY" when expecting one of the following:...." and i am getting the same error at line 5 at the "s" letter of the word Cursor ,but at this time it says ";" instead of "Salary". The compiler log shows "insufficient privileges" but i can't find a reasonable explanation for this.
I have tried to run the code as an anonymous block like this:
Declare
Salary number;
Cursor RaiseCursor
Is
Select Salary_A From Person Where Salary_A<400 for update of Salary_A;
Begin
Open RaiseCursor;
Loop
Fetch RaiseCursorInto Salary ;
Exit When RaiseCursor%Notfound;
Update Person Set Salary_A =(Salary + 200) Where Current Of RaiseCursor;
End Loop;
Close RaiseCursor;
Commit;
End;
.
run;
And works great!!!I am sure that i have the privileges to write stored procedures because i have created some successfully!
Am i doing something so wrong? I can't figure out what causes the problem, if you have any suggestions i would really appreciate that:)
EDIT: For some reason my procedure got wrong Schema that's why i had that problem! I only dropped the Procedure and create it again and it fixed!!!!
Thanks for your time anyway!
Let me just point out this would be vastly more efficient if you did it as:
UPDATE Person Set Salary_A=(Salary_A + 200)
Where Salary_A<400
No cursor, no looping. It would also be a lot simpler.

Resources