Using path relative to SQL script location in that script? - sql

I have a sql script in
dir:\some\path\script.sql
This script is executed via SqlDeveloper and spools some data into
dir:\some\path\data\tablename.csv
Now, I have to store the script in a .sql file because if I execute it from SqlDeveloper directly, spool writes the query itself to the file which I do not want.
But I also do not want to hard-code paths because everyone in my team has their GIT repo whereever they want, so the scenario I'd like to achieve is that my sql script uses relative paths from it's location to write the csv files.
Currently the script.sql looks something like this:
SET COLSEP ";"
spool dir:\some\path\data\table1.csv
select /*csv*/ * from table1;
spool off;
and I want to look it something like this
SET COLSEP ";"
spool current_dir\data\table1.csv
select /*csv*/ * from table1;
spool off;
So that when using the script, the path has to be typed only once, when calling the script.

More of a workaround than a solution to the problem, but for now I just created a scriptRunner.sql file in the same directory as the script.sql which simply runs the script (#script.sql).
When opening this file, it seems like SqlDevelopers working directory is set to the directory where both .sql files are in and this property is extended to the called script, therefore I can simply use
spool data\table1.csv

Related

sql query appears in the outpout of spool command(file.txt)

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.

SQL Plus SP2-0606

Using SQL tool, I want to create a text file that has a VARCHAR records.
Using SQL Developer I have successfully created the output text file by calling a script. But based on my research, the TRIMSPOOl command is currently not supported by SQL Developer, making my output to have trailing spaces.
Here is the code in the script:
spool on
set echo off
set verify off
set feedback off
set heading off
set trimspool on
set termout off
spool C:\SQLFiles\Output.txt;
select cust_name || cust_addr as Cust_Details
from customer_db;
spool off;
I now have the SQL Plus 11g, and I'm trying to run the script i created in SQL Developer. I'm getting an SP2-0606 error, saying:
SP2-0606: Cannot create SPOOL file "on.LST:
Based on the research I did, it is because of the Spool command, and I don't have the right to access the default folder??..
Can you help please on what setup I should change do to make the desired result in SQL Plus.
You have to put the file name in double quotes - I guess SQLPlus uses C-style escape sequences, giving the backslash \ a special meaning. Also, you should remove the semicolon ';':
So if you replace
spool C:\SQLFiles\Output.txt;
with
spool "C:\SQLFiles\Output.txt"
it should work as expected (if C:\SQLFiles exists and is writable for your account).
UPDATE
As #LalitKumarB has pointed out, spool works perfectly fine without the double quotes. The real problem is elsewhere:
spool on
This tells SQLPlus to create an output file on in the current directory. So you'll get this error message if you don't have access to the directory you're starting SQLPlus from.
You are getting this error because of permission issues. Either open your command prompt window in administrator mode or save your file to a location where admin permission is not required.
From the docs,
SP2-0606 Cannot create file file_name
Cause: The STORE command was unable to create the specified file. There may be insufficient disk space, too many open files, or
read-only protection on the output directory.
Action: Check that there is sufficient disk space and that the protection on the directory allows file creation.
My test case shows there is nothing wrong with the spool command. I don't think there is any need of quotes at all.
For example,
Client: 12c
OS : Windows 7
SQL> spool D:\text.txt
SQL> select ename||to_char(empno) from emp;
ENAME||TO_CHAR(EMPNO)
--------------------------------------------------
SMITH7369
ALLEN7499
WARD7521
JONES7566
MARTIN7654
BLAKE7698
CLARK7782
SCOTT7788
KING7839
TURNER7844
ADAMS7876
ENAME||TO_CHAR(EMPNO)
--------------------------------------------------
JAMES7900
FORD7902
MILLER7934
14 rows selected.
SQL> spool off;
And the spool file looks like:
So it works without any double-quotation marks. I guess you need to really look at the directory you are using to spool.
this error is just because of , not having "WRITE" privileges on directory where you are creating the spool file.
Solution
give or ask to give the permission to you who is owner of that directory, if its not owned by you using chmod. for ex- full permission to all chmod 777 <file/directory name>
When you get this error check for spaces. For example, below you see both single quotes (') but the first one has a space after it and the second one doesn't.
SP2-0606: Cannot create SPOOL file ' E:\SQL CLASS\New folder\sql class prt'
SQL> spool 'E:\SQL CLASS\New folder\sql class prt'

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

Log file avoid dumping sql query data in it.. c shell

I have sql script "example.sql":
SPOOL &1
Select '<.TR>'||'<.TD align="left">'||column_name||'<./TD>'||'<.TR>' from table1;
spool off
which dumps it contents to cshell script "getdata.csh" this is how i get data from sql script to csh script
sqlplus $ORA_UID/$ORA_PSWD #${SQL}example.sql ${DATA}${ext}
My problem is when I run my job to do this it dumbs all data extracted from sql query in log file too.. How can I avoid dumbing data into log files. What do I have to do in this code to do so? thank you
Look here for information about your issue: sqlplus spool
Essentially, you will need to add these two settings:
set echo off
set termout off