Oracle Data Backup - oracle

I'm using Oracle 11g. I wanted to move certain records from a table to a flat file. Is there are any utility to do the same like sqlldr. I created unix scripts to do the same but it was not doing the purpose. Please Help

You Can use UTL_FILE package for loading all the table data to flat file. Here is the sample code for that
DECLARE
p_file util_file.file_type;
l_table your_table_name.ROWTYPE;
l_delimited VARCHAR2(1) := '|';
BEGIN
p_file := utl_file.Fopen('<file_path>', '<file_name>', 'W');
FOR l_table IN (SELECT *
FROM your_table_name) LOOP
utl_file.Putline(p_file, l_table.col1
||l_delimited
||l_table.col2
||l_delimited
||l_table.col3
||l_delimited
||l_table.col4
||Chr(10));
END LOOP;
utl_file.Fclose_all();
END;
Check Oracle documentation on the package

Related

How to Convert Oracle Stored Procedure Output to CSV file

I have seen many examples where you can convert a SQL Query output to a CSV file by calling it from a stored procedure as shown in PL/SQL code below. However, in my case, I have a stored procedure called Management with 3 input parameters. Once these inputs are confirmed, the stored procedure extracts the result with 10 columns based on the code in the PL/SQL.
My aim is to convert this extract from the Management stored procedure to a CSV. The examples I found so far are all to do with converting SQL query only to CSV by calling it from a stored procedure.
I'm trying to avoid writing the code to convert the output to CSV in the Management stored procedure by calling it from another stored procedure. Is this possible?
The Management stored procedure is called by the SQL developer and the values are returned by applying a select query from two tables and applying multiple cases and if statements.
An example will be much appreciated.
CREATE OR REPLACE PROCEDURE export_to_csv
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_emp
IS
SELECT empno,
ename,
deptno,
sal,
comm
FROM emp;
BEGIN
v_file :=
UTL_FILE.fopen ('CSVDIR',
'empdata.csv',
'w',
1000);
-- if you do not want heading then remove below two lines
v_string := 'Emp Code, Emp Name, Dept, Salary, Commission';
UTL_FILE.put_line (v_file, v_string);
FOR cur IN c_emp
LOOP
v_string :=
cur.empno
|| ','
|| cur.ename
|| ','
|| cur.deptno
|| ','
|| cur.sal
|| ','
|| cur.comm;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
EXCEPTION
WHEN OTHERS
THEN
IF UTL_FILE.is_open (v_file)
THEN
UTL_FILE.fclose (v_file);
END IF;
END;
The management SP will return values and you will use those values to create the CSV file with this new SP (export_to_csv)? If this is the case then
Create one Package Spec and define 2 SPs (Management and export_to_csv).
Then in the Package body, copy the logic of Management sp and at the end of Management SP, call your new SP (export_to_csv).
If you already have Package then directly use that package instead of creating the new one.

how to export data from around 300 tables in ORACLE DB to csv or txt files

Is there any possibility to export data from around 300 tables within single schema with millions of records to CSV or TXT using any PL/SQL procedure?
What do you propose, which is fastest way to do it? For the moment I do not need to import these exported files to any other schema...
I tried with Toad manually exporting table by table...
you can try following steps.
write a loop to get the table names
use cursors to fetch the data from each table
use SYS.UTL_FILE utilities to write the data to files in any required format.
This is a very high level solution. But i am sure it will work.
I have created a utility by which you can generate PL/SQL procedures to export data from a table. It will take the following parameters, table name, column names, directory name and the delimiter. You can generate 50 procedures for 50 tables in no time to export data from Oracle. Check this link Generate PL/SQL Procedure to export data into CSV
I managed to dynamically go through all tables and get column names and write to a file. I am struggling into part how to fetch data rows from tables dynamically when execute immediate query? how should I save data rows and than fetch it and write to files?
Here is the code:
DECLARE p_table VARCHAR2 (100);
l_file UTL_FILE.FILE_TYPE;
l_string VARCHAR2 (10000);
query_string VARCHAR2 (4000);
BEGIN
FOR tab IN (SELECT *
FROM dba_tables
WHERE owner = 'XYZ' AND table_name LIKE 'XYZ%')
LOOP
p_table := tab.table_name;
l_file :=
UTL_FILE.FOPEN ('my_path',
tab.table_name || '.txt',
'w',
10000);
l_string := NULL;
FOR col_he IN (SELECT *
FROM dba_tab_columns
WHERE owner = 'DWHCO' AND table_name = p_table)
LOOP
CASE
WHEN l_string IS NULL
THEN
l_string := col_he.column_name;
ELSE
l_string := l_string || ',' || col_he.column_name;
END CASE;
END LOOP;
UTL_FILE.PUT_LINE (l_file, l_string); --Printng table columns
query_string := 'select ' || l_string || ' from DWHCO.' || p_table
--Execute immediate query_string into ??????????;
--??????
UTL_FILE.FCLOSE (l_file); END LOOP;END;
The Data Dump procedure is helpful for programmatically exporting many tables to simple formats like CSV.
First, install the package using the above link. The below code creates a directory, cycles through tables, and exports each table as CSV.
create or replace directory temp_dir as 'C:\temp';
begin
for tables in
(
select
owner||'_'||table_name||'.csv' file_name,
'select * from "'||owner||'"."'||table_name||'"' v_sql
from dba_tables
where owner = 'XYZ'
and table_name like 'XYZ%'
order by 1
) loop
data_dump
(
query_in => tables.v_sql,
file_in => tables.file_name,
directory_in => 'TEMP_DIR',
delimiter_in => ',',
header_row_in => true
);
end loop;
end;
/

Oracle database loading data from text file to database using UTL_file and dbms

I am trying to upload data from text file to my table using UTL_FILE from SQL PLUS.
the text file name testb.txt contain data:
10,mike
20,bill
30,kim
and in my the table in the database called mytable contain columns (empno, empname)
I tried this code on SQL plus:
declare
filehand utl_file.file_type;
line varchar2(4000);
vempno varchar2(50);
vempname varchar2(50);
begin
filehand := utl_file.fopen('c:\users\myuser\desktop', 'testb.txt', 'r');
loop
utl_file.get_line(filehand, line);
if line is not null then
vempno := substr(line, 1, instr(line, ',')-1);
vempname := substr(line, instr(line, ',')+1, length(line));
dbms_output.put_line(vempno);
dbms_output.put_line(vempname);
insert into mytable (empno, empname) values (vempno, vempname);
commit;
else
exit;
end if;
end loop;
commit;
utl_file.fclose(filehand);
exception
when others then
null;
end:
/
when I run this code I got PL/SQL procedure successfully completed.
but when I see my table nothing change and the data not loaded from the text file to my table.
???
whats wrong in my code and what to do to upload the data from the text file to my table using UTL_file and dbms ??
Please guys help me I really need it.
Thank you very much

Importing .csv file into a Oracle Forms application

I've got a question how to import a .csv file into a Oracle Forms application.
We are using Oracle Forms 11g on a Oracle 12c Database.
Now we want to import a .csv file with the Forms applicationso our customers can import this file and write the data into the database.
My plan is to create an application where the user can import a .csv file with a filechooser. The data from the .csv will be read and an output shows the user the data in this application. Then the user should be able to save it into the database through a button.
I've tried several searches but haven't found the right solution for this kind of problem. The only solutions I've found were a direct import of a .csv file into a database but not through Oracle Forms
Is it even possible to load .csv files in Oracle Forms?
If anyone has a good solution or anything else that might be helpfull i would be thankfull for that.
I've found a suitable solution for me now. Maybe I could do it better but this helps me so far.
I've found the following Blog: http://tfathy.blogspot.de/2009/03/reading-from-file.html
The Code works 100% by copy&paste and helps me from now on to complete the task.
Maybe it might help anyone else too.
Here is the solutioncode:
Reading From File
March 19, 2009
--------------------------------------------------
Declare
vfilename varchar2(500);
in_file Client_Text_IO.File_Type;
linebuf VARCHAR2(1800);
BEGIN
vfilename := client_get_file_name('c:/temp/', File_Filter=>'Comma Dialimeted Files (*.csv)|*.csv|');
in_file := client_Text_IO.Fopen(vfilename, 'r');
GO_BLOCK('Emp');
FIRST_RECORD;
LOOP
Client_Text_IO.Get_Line(in_file, linebuf);
p_output_line(linebuf);
Client_Text_IO.New_Line;
Next_record;
END LOOP;
FIRST_RECORD;
EXCEPTION
WHEN no_data_found THEN
Client_Text_IO.Put_Line('Closing the file...');
Client_Text_IO.Fclose(in_file);
END;
-------------------------------------------------------
PROCEDURE p_output_line(p_line varchar2) IS
vLINE VARCHAR2(4000);
vVALUE VARCHAR2(1000);
vCOMMA_COUNT NUMBER;
vREPORT_DATE DATE;
BEGIN
vLINE := p_line;
vCOMMA_COUNT := LENGTH(vLINE)- LENGTH(REPLACE(vLINE,',','')); -- COUNT THE NUMBER OF COMMAS
FOR I IN 1.. vCOMMA_COUNT+1 LOOP
vVALUE := SUBSTR(vLINE,1,INSTR(vLINE,',')-1); -- IF vLINE = 123,ABC,9877 THEN VVALUE WILL BE 123
IF vVALUE IS NULL THEN
vVALUE := vLINE;
END IF;
vLINE := SUBSTR(vLINE,INSTR(vLINE,',')+1) ; -- CHANGE 123,ABC,9877 TO BE ABC,9877
IF I = 1 THEN
:DATA.BMK_NAME := vVALUE;
ELSIF I = 2 THEN
vREPORT_DATE := last_day(to_date(vVALUE,'dd-mm-yyyy'));
:DATA.REPORT_DATE := vREPORT_DATE;
ELSIF I = 3 THEN
:DATA.BMK_RETURN := to_number(vVALUE);
END IF;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
MESSAGE('Please Check the data type is appropriate on you excel file');
MESSAGE('Please Check the data type is appropriate on you excel file');
END;
-----------------------------------------------------------------------
-- notes
1- you must install webutil version 106 or later
2- make sure that you attached and compiled the webutill.pll scucessfuly
You can just use webutil to show a filechooser to select the file and upload it to the application server. If you use a shared directory between the application server and the db server you can use an external table to show the input of the file to the user. And then after the button you just insert the data from the external table in another table.

How to store sysdate value in a variable in triggers in oracle?

I have a trigger written as below:
create or replace
TRIGGER impt_downloadproc
before delete ON A
declare
storedate nvarchar2(80);
storetime nvarchar2(80);
sequel string(2000);
BEGIN
storedate := to_char(sysdate,'YYYYMMDD');
storetime := to_char(sysdate,'HH24MISS');
sequel:='create table B_'||storedate||'_'||storetime||' as select * from ipcsdd_download_process';
execute immediate sequel;
END;
What I am trying to do in my trigger is : Before someone/something deletes record(s) from A table,
create a backup table B_yyyymmdd_hhmmss and backup the records to this table.
But I get error in line :storedate := to_char(sysdate,'YYYYMMDD');
I dont understand what is the problem.
I would expect a different error- you cannot commit in a trigger.
If you really want to then you'll need to use an autonomous transaction (which is mostly not a good idea)
So, your code should look something like this:
create or replace
TRIGGER impt_downloadproc
before delete ON A
declare
pragma autonomous_transaction; -- see this line
storedate nvarchar2(80);
storetime nvarchar2(80);
sequel string(2000);
BEGIN
storedate := to_char(sysdate,'YYYYMMDD');
storetime := to_char(sysdate,'HH24MISS');
sequel:='create table B_'||storedate||'_'||storetime||' as select * from ipcsdd_download_process';
execute immediate sequel;
END;
here is an example

Resources