I have a function that returns a table of data. I created a query that runs the function and spools the output to a file. I need to know if there is a way to loop this and automate this output for many files.
set heading off
spool testfile.csv
select * from table(function(location));
spool off;
This code successfully leaves me with a csv file of the table. What I would like to do is loop it and feed it a list of locations and have it print me a file for each. When I tried to implement a loop I ran into an issue where my spool path gives me an error if there is a begin statement in front of it. This is concerning because if I wanted to use a function or procedure I would need to use a begin and end.
Related
i want to save the result of a query using spool command.
But i got the query and its output in the result file (result.txt)
spool "result.txt"
select * from table;
spool off
how can i remove the query from the file result.txt?
You need to add the following set command:
set termout off
If you store the complete script in a script file (for instance getData.sql) and then run it from SQL Plus with the following command you will have the results wanted.
#getData.sql
Note that typing these commands directly in SQL Plus will cause the select statement to be in the file, even with the set parameter.
Is it possible to export the results of a query (a select statement with multiple joins) to an Excel spreadsheet without having to manually export the data, so the script runs the query and also executes the transfer to Excel?
You can use this code in SQL*Plus and Oracle SQL Developer.
Create a .sql file. (Create a text file and change the extension to .sql)
Put the following code in the file:
set heading off
spool excel_1.xls
select
first_name||chr(9)||last_name
from
employees;
spool off
Since I have made use of employees table from HR schema, connect hr schema.
Execute the script file
#<filename>.sql
you can use any number of columns but each should be separated as shown in above example.
I have created a database and have my tables, but I need to implement some data into it, to avoid writing out 5000+ entries I have decided to download 'dummy data (fake)' for names, numbers, address, etc.
I have tried using "BULK INSERT" but SQL does not accept this, is there any other way of implementing the fake data?
SQL Plus can only interpret commands given by console or read from script. You have to create script with set of insert commands and run it from SQL Plus. I used this approach more times, it's not as difficult.
Write your script like this:
declare
procedure change_data(your_data_in_parameters) is
begin
set_of_our_commands_to_handle_one_record;
end;
begin
change_data(your_data_from_CSV_row1);
change_data(your_data_from_CSV_row2);
...
commit;
end;
Open your CSV in some spreadsheet editor and add new formula column which collects change_data rows.
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
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;