Procedure to create .csv ouput [duplicate] - sql

This question already has answers here:
Procedure to export table to multiple csv files
(2 answers)
Closed 9 years ago.
please help me to create a procedure to export data to specified path from oracle database as .csv file take study names from another table.
QUERY : select * from enrollment where study_name = 'FTY67' ;
I have another table(studies) in same database with all studynames.
is there any way to create procedure that will take study names from studies table and repeat this procedure to create .csv files for all studies?
read some articles in internet but not found anything related to this.
please help.

You should look into the spool and set command. Since the database is usually running on a remote server, it can not write files to your local computer. In order to achieve this yu must write an sql where you disable certain characteristics in the terminal, and then spool the result into a file that you can access.
Something like this might get you started:
set term off colsep ";" pause off
spool myfile
select * from x;
spool off
exit
For an overview of the options you can use with SET refer to the oracle documentation here: http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm
With the proper set commands you are able to create the CSV file.
The above set commands are just a few you might need, but you will probably need addtional parameters to make your CSV usable.
Best to write this in a file.sql and run it using sqlplus:
sqlplus user#db #file

Related

Extract table procedure Oracle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is it possible to create any procedure to extract tables from Oracle database?
Something like i could select which table and which fields that i want to extract, so finally create a CSV file of table. In future i will need for oracle ERP too, but at this time just for database.
I'm working in a audit project, so field consultants needs constantly extract tables from clients, and all this is by hand-made (table per table, script per script, there is so many waste time). So this way i'm trying to automated these extractions.
Thank you!
I can just give you the idea.
Create a procedure which accepts the Table name and comma-separated column names as two input parameters with a third out parameter which will be used to pass the status of the procedure at the end to the calling user.
CREATE PROCEDURE FETCH_TABLE_DATA( P_IN_TABLE_NAME IN VARCHAR2,
P_IN_CS_COLUMN_NAMES IN VARCHAR2,
P_OUT_MESSAGE OUT VARCHAR2)
You can now create the cursor using dynamic query and use something like:
'SELECT ' || COLUMN_NAMES_COMMA_SEPRATED || ' FROM ' || TABLE_NAME
and using UTL_FILE, you can put this all data to the text file.
In case of success, you can assign the name of the file to the P_OUT_MESSAGE or in case of failure, You can show the error message by assigning error message to P_OUT_MESSAGE
Search all the keywords used in the answer to prepare the procedure by yourself.
Hope, It will give you proper direction.
Cheers!!
If you use SQL Developer or Toad for Oracle, you can simply export a query on a table or the whole table to csv.
Oracle SQL Developer:
This is by far the easiest way for someone not familiar with complex PL/SQL stored procedures to export data.
One way to do this is to create a Report in SQL Developer (see this tutorial at Oracle for more information on this feature) and export the results
Once the report is created and ran, right click on the results grid and choose “Export” from the context menu
In the next screen select CSV as the format and enter in the filename.csv and location
You can also export a single table by:
Right-click the table name in the object tree view.
Select Export.
Select CSV. The Export Data window shows up.
Click Format tab.
Select Format as: CSV
Enter a file name and location.
Click Columns tab.
Check the columns you wish to export
Click Where tab and add any criteria needed to filter the data
Click Apply.
PL/SQL:
The best way to export data via PL/SQL is to create a stored procedure that uses the UTL_FILE command.
For a great example stored procedure for this, please see this article at oracle.com. Note that there is a recommended change to the original stored procedure located in the comments (properly handles the quoting of data with reserved CSV characters).
Now you are ready to upload the new CSV File to SpatialKey.

Query for finding all occurrences of a string in a database

I'm trying to find a specific string on my database. I'm currently using FlameRobin to open the FDB file, but this software doesn't seems to have a properly feature for this task.
I tried the following SQL query but i didn't work:
SELECT
*
FROM
*
WHERE
* LIKE '126278'
After all, what is the best solution to do that? Thanks in advance.
You can't do such thing. But you can convert your FDB file to a text file like CSV so you can search for your string in all the tables/files at the same time.
1. Download a database converter
First step you need a software to convert you databse file. I recommend using Full Convert to do it. Just get the free trial and download it. It is really easy to use and it will export each table in a different CSV file.
2. Find your string in multiple files at the same time
For that task you can use the Find in files feature of Notepad++ to search the string in all CSV files located at the same folder.
3. Open the desired table on FlameRobin
When Notepad++ highlight the string, it shows in what file it is located and the number of the line. Full Convert saves each CSV with the same name as the original table, so you can find it easily whatever database manager software you are using.
Here is Firebird documentation: https://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25.html
You need to read about
Stored Procedures of "selectable" kind,
execute statement command, including for execute statement variant
system tables, having "relation" in names.
Then in your SP you do enumerate all the tables, then you do enumerate all the columns in those tables, then for every of them you run a usual
select 'tablename', 'columnname', columnname
from tablename
where columnname containing '12345'
over every field of every table.
But practically speaking, it most probably would be better to avoid SQL commands and just to extract ALL the database into a long SQL script and open that script in Notepad (or any other text editor) and there search for the string you need.

How to create excel export using sql query? [duplicate]

This question already has answers here:
Export SQL query data to Excel
(5 answers)
Closed 8 years ago.
I need help in export data from table to excel file using sql query.
and with header and footer.
I used master..xp_cmdshell with passing db name, table name, and path where I store excel file any buddy can solve this. and any other way like query through export data to excel.
I must say I want to solve problem using query only. because I require only query.
If you need column headers the best way to export to a CSV file (which EXCEL will open) is by using sqlcmd. The syntax is something like:
c:\>sqlcmd -S servername -i c:\path\yourqueryfilename.sql -o C:\OutputPath\AFilename.csv -s","
See http://msdn.microsoft.com/en-us/library/ms162773.aspx to find the complete command line options available for SQLCMD.

how to save the data in csv file and then send that to a folder using sql?

I am using oracle 11g with plsql developer. My requirement is that the data retrieval should be automatic. That means when query a data in oracle table , the data should be convert into .csv file and then it should send that to some specific folder.
Example:
Assume that student_sub1 table have all students subject1 data.when i write a query like
select * from student_sub1 where student=1, the student1 data should convert into .csv file and then the .csv file should go to student1 folder.
Hence I am requesting you to suggest me how write a sql code to convert the data into .csv file and to create a folder to send that file using oracle.
Your inputs helps me alot.
see if this post helps. That solution works when you are running the query from sql developer.
EDIT: Btw, if you want this to be in a script. Then I think you can run it using spool command to output the results to a file. Something like this.
set echo off
set feedback off
Spool student1.csv;
Select id||','||name||','||grade from student1;
Spool off;

Oracle Table millions of row, pull and save into file

I have oracle table that holds more than 30 million records, I need to pull all that data into file and store it.. Can anyone suggest me what will be easiest way to do that and what kind of file do I have to use to store all that data. If there is a way I can put data into multiple files that will work too.. You can tell me manual or programatical method. Thank you
Ask Tom has an answer http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::p11_question_id:88212348059
Also, you could do it in SQL*Plus by doing a select with spool on (from a shell script)
sqlplus /nolog <<EOF
conn /as sysdba
set pagesize 0 heading off feedback off verify off echo off trimspool on
spool test.log
select 'dsmc inc "'||file_name||'">>hot_WISDOM_$BCKNAME.log' from dba_data_files
where rownum<5;
spool off;
exit
EOF
More info on the spool stuff at http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:25323432223677
That page is where I found that snippet above.
You will want to replace the connection details with your username / pw.
Starting with Oracle 10g you can create a new table with ORGANIZATION EXTERNAL and INSERT records from the original table into it. See this example from the Oracle 10gR2 documentation.
One of the easiest formats to store data in is comma seperated value (.csv). You can define your delimiter to be any character (comma is a default) as long as you know the delimiter when you are parsing it. (For instance, the ~ character would be a good decision). Usually this format can easily be opened in a spreadsheet program like Excel. It can also be easily parsed to be re-inserted into a database of your choosing.
To export from oracle, you can do something like:
spool backup.csv;
select column1||','||column2||','|| ... from table;
spool off;
After exporting, here is a resource for working with csv files in perl:
http://perlmeme.org/tutorials/parsing_csv.html
Apache commons has a great library for java:
http://commons.apache.org/sandbox/csv/apidocs/org/apache/commons/csv/CSVParser.html