I know I can run multiple queries saved in file with GO statement between them and save the result into file with spool command
but this will save the whole result into a single file
how can I export each query result into a dedicated file ?
use multiple spool statements between each SELECT
eg, spool c:\filenme.txt
Related
I'm using PL/SQL Developer version 11.0.6.1776
The following script, creates a folder named 'test' on the specified path and a subfolder within it, with the name of the table that is currently spooling to stored its metadata in that folder:
set pagesize 0
set long 90000
set linesize 10000
SET TERMOUT OFF
spool out.sql
select 'host mkdir ''C:\Users\personal\test'''||';'||chr(13)||chr(10)|| --Creating the new folder
'host mkdir '||''''||'C:\Users\afpuchn\test\'||REPLACE(table_name, '$', '_')||''''||';'||chr(13)||chr(10)|| --Creating a subfolder
'spool C:\Users\personal\test\'||REPLACE(table_name, '$', '_')||'\'||REPLACE(table_name, '$', '_')||'.txt'||chr(13)||chr(10)||
'SELECT DBMS_METADATA.GET_DDL'||chr(13)||chr(10)||
'(''TABLE'','''||table_name||''',''MTO_TABL'') '||chr(13)||chr(10)||
'FROM DUAL;'||chr(13)||chr(10)|| --Extracting metadata
'spool off' as cmd
FROM MTTO_TAB_EXIST_ALL tea
WHERE tea.MRK_DEL_PERM = 'Y'
AND tea.OWNER_NAME = 'MTO_TABL'
AND ROWNUM < = 5;
spool off
#OUT.SQL
exit
Whenever I run the script, the name I want to give to the file being stored in the created folder is incomplete or with the specified file type incomplete.
Here is an example of the script that returns:
host mkdir 'C:\Users\personal\test';
host mkdir 'C:\Users\personal\test\ED_EXTR_CSV_PRIM_DEA_PUBLI';
spool C:\Users\afpuchn\test\ED_EXTR_CSV_PRIM_DEA_PUBLI\ED_EXTR_CSV_PRIM_DEA_PU --The file name is incomplete
SELECT DBMS_METADATA.GET_DDL
('TABLE','ED_EXTR_CSV_PRIM_DEA_PUBLI','MTO_TABL')
FROM DUAL;
spool off
Because of this, the script cannot be stored in the given path even if I put a SET LINESIZE who doesn't works. Does my version of PL / SQL have any relation with this?
set linesize is not supported by the PL/SQL Developer command window, according to the manual. If you want to create and run SQL*Plus scripts, I strongly recommend you use the actual SQL*Plus from the command line, not an imitation.
I'm a big fan of using PL/SQL Developer for writing programs and worksheets, but it's not a good environment for scripting. Neither is SQL*Plus, but SQL*Plus is at least simple and compatible across a wide variety of platforms, which is why many DBAs use that tool to run scripts.
I apologize for possible duplicate, but I just cannot seem to get this working. I have an .sql file where I am trying to pass a parameter like "C:\Path\To" to adjust the location of spooling output, since I would like to run it from a Windows scheduler script. The print statement shows the correct path, let alone with a header like this:
ARGPATH
---------------------------------------------------------------------------- ----
C:\Path\To\output.txt
However, I cannot seem to get the spooling to work with this type of dynamic file location parameter. There are no errors when running the script beside the lack of output. I am using SQL Developer's CLI SQLcl ver 4.2, 12.1.0.2.0. Unfortunately I am stuck with this particular tool and have very little control over the development Environment in general. Instructions on how to just spool into the local folder (where the sql script resides) instead of some default location are most welcome as well. Thanks in advance!
set heading off
var argpath varchar2(100)
exec :argpath := Trim('&1'||'output.txt')
spool argpath;
print argpath;
select count (*) .../select statement/
spool off;
exit
Maybe I should add that when I declare the path explicitly, i.e.
spool C:\Path\To\output.txt
everything works just as expected.
You're overthinking it. Use substitution on its own and dont use binds the way you were trying to.
spool '&argpath\output.txt'
select 1 from dual;
spool off
I have created a script daily_status_22.sql as
set feedback off;
set pages 0;
set term off;
column dt new_value _dt;
column mn new_value _mn;
select 'C:\Users\rjen01\Desktop\'||to_char(sysdate,'MON')||'\' from dual;
host mkdir mn;
select to_char(sysdate,'ddMONyyyy_hh24mi')||'.csv' dt from dual;
spool &_mn &_dt;
select sysdate from dual;
spool off;
and i run
#C:\Users\rjen01\daily_status_22.sql
as script by pressing F5.
How can i use scheduler to schedule to run command
#C:\Users\rjen01\daily_status_22.sql
as a script and not as a query.
You can do a couple of things.
What you can't do is schedule SQL Developer (GUI) directly to start, connect, and run a .SQL file as a script.
However, we do have a CLI for SQL Developer, it's called SQLcl. Newer versions (4.1+) will have this binary (SQL.exe on windows) in the BIN directory.
You can call it from your OS .bat/.sh scripts just like you would with SQL*Plus. It has the ability to run anything SQL Developer can and also anything SQL*Plus can.
You can ALSO just run your script via SQL*Plus. You'll need to get an Oracle Client to run SQL*Plus. And you'll want to test the script in SQL*Plus first to make sure it works the way you expect since you developed it first in SQL Developer.
I am using Oracle11g. I have below two sql queries.
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Now how can i create a batch file to run this script By passing the credentials to connect to DB and delete using above queries?
Thanks!
Add the statements to a file, say "delete.sql".
To execute the statements in the file, type:
sqlplus username/password#tns_name_of_your_db #delete.sql
...or simply add that into a .BAT file.
PS. Remember to add a COMMIT; statement.
PS2. Make sure the last line of "delete.sql" is empty (blank line).
I'm using SQL Developer to spool the DDL for some packages, but I cannot identify the location where the server is spooling. Oracle is installed on AIX, but I don't know the user it's using to connect to the OS.
Basically, when I run:
spool test.lst
select 1 from dual;
spool off
I get a confirmation message in console
1
----------------------
1
and whenever I try to give a path, I'd get an error, probably because of user rights:
spool /tmp/test.lst
select 1 from dual;
spool off
Cannot create SPOOL file /tmp/test.lst
1
----------------------
1
So my question is where is test.lst?
Spool is a client activity, not a server one; the .lst file will be created on the machine that SQL Developer is on, not the server where the database it's connecting to resides.
Under Windows 7 and SQL Developer 3.1, by default for me that seems to store the .lst in %APPDATA%\Sql Developer\
You can spool to a specific directory, e.g. spool c:\windows\temp\test.lst, and if you have it set up can use something like spool \\<aix-server>\<dir>\test.lst. Paths vary according to your client OS, of course.
Under my LINUX installation the default spool directory is from where you are running sqlplus.
ie. If you run sqlplus from a directory where you do not have write permissions it will fail.
Try typing "!" or "host" at the sqlplus prompt to goto the OS.
pwd the directory end up in and I guess this is location of your spool.
(type exit to go back to sqlplus)
What the other guy said for the default location was true for oracle 10g but since I am using the latest oracle 21c(at the time of writing this answer) here is the solution that worked for me.
in case if you created a spool file lets say: spool on; spool tempfile;
then the default location would be at the place you installed the oracle db software which in my case is
D:\WINDOWS.X64_213000_db_home\bin
and then in the
bin
folder the file would be created as
tempfile.LST
and in order to open a
.lst
file just use the default notepad or any such editors to open your output
However here is a better suggestion by me:
type spool on; SPOOL C:/path/xyz.txt; #here change your directory,the pathname and file name for query file
and most importantly DO NOT FORGET TO TYPE
SPOOL OFF;
at the end of the query. In case if you forget then your file wont be created and you will lose all your progress of your saved output. and lemme give you another pro tip: and that is to not write the same spoolfile name otherwise it will be overwritten