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.
Related
I am using below batch script and want to execute a SQL query and save the data in CSV file
call sqlplus myuser/mypass#abcdb#"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xyz-scan.abc.com)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=abcdb)))" #C:\myfolder\myquery.sql
but it is giving me "ORA-28040: No matching authentication protocol exception" error when i run my batch script
I also tried with simple TNS (note: my tns location is "D:\app\iis_admin\product\11.1.0\client_1\network\admin\sample")
call sqlplus myuser/mypass#abcdb #C:\myfolder\myquery.sql
But again getting same error.
my myquery.sql contain below query
set feed off
set pagesize 0
spool C:\myfolder\myresult.csv
prompt sysdate
select to_char(sysdate, 'dd-mon-yyyy') from dual;
spool off
set head on
set feed 6
quit
I presume it might be about too large gap between versions you use.
screenshot suggests it is Oracle 8.0.6 (gosh, that's old! Dates in 1997)
text mentions product\11.1.0
Therefore, I'd upgrade the former so that it would be able to speak with the latter.
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'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
I'm trying to use sql*plus to control a small Windows command script.
Basically, I want to execute some PL/SQL (perhaps select from a view or table or execute a function) which shows me the status of some rows in the database, and then depending upon the state of the rows, perform some Windows commands.
My problem is how to get the results back into the command script.
sqlplus user/password#server #script.sql
IF <CONDITIONAL HERE BASED on script.sql results> GOTO :runprocess
REM log and email that process had to be skipped
EXIT
:runprocess
REM run various Windows service commands
I'd probably write the script (or the conditional, depending on the requirements) from the called script.sql itself.
For example, the following script.sql creates a .bat file windows_commands.bat:
set feedback off
set echo off
set trimspool on
set termout off
set serveroutput on size 100000 format wrapped
set lines 500
set pages 0
-- create the bat file to be executed later:
spool windows_commands.bat
declare
c number;
begin
select count(*) into c from dual;
-- depending on a conditional, write the stuff to be executed into the
-- bat file (windows_commands.bat)
if c = 1 then
dbms_output.put_line('#echo everthing ok with dual');
else
dbms_output.put_line('#echo something terribly wrong with dual');
end if;
end;
/
spool off
exit
You can then call script.sql from yet another .bat file like so:
#rem create oracle session, call script.sql
sqlplus %user%/%password%#%db% #script.sql
#rem script.sql has created windows_commands.bat.
#rem call this newly created bat file:
call windows_commands.bat
This is what I ended up using.
My .cmd script:
#ECHO OFF
ECHO Checking Oracle...
for /f %%i in ('sqlplus -s user/password#database #script.sql') do #set count=%%i
echo %count%
IF %count% GTR 0 GOTO :skipped
GOTO :runprocess
Where script.sql:
SELECT COUNT(*)
FROM table
WHERE criteria = 1;
exit
I would strongly encourage you to not use .bat files. You've got lots of other alternatives: C/C++ or VB, Windows scripting or Powershell, or even free downloads like Perl or Bash.
But here's one example of returning error codes in .bat files:
http://www.dbforums.com/oracle/1044496-sqlplus-return-value-help.html
But please do look at some of the links I gave above. Avoiding .bat files will make it easier for you, and make it easier to maintain in the future.
IMHO ...
I do something like this by creating a .bat file which does the windows stuff and calling sql scripts as needed. Use SQL to spool your results to a text file which you can read.
...dos commands here
sqlplus /nolog #C:\Dump\DropRecreateUsers.sql
sqlplus /nolog #C:\Dump\Cleanup.sql
...dos commands
In the sql use this command spool C:\yourResults.txt or for more sophisticated usages create a procedure, which, when called, writes the results to a text file using UTL_FILE
I encourage you to take a look at the two scripts included in the Oracle XE for backup and restore. These scripts have taught me a lot how to handle batch-scripting and Oracle on the Windows platform.
C:\oraclexe\app\oracle\product\11.2.0\server\bin\Backup.bat
C:\oraclexe\app\oracle\product\11.2.0\server\bin\Restore.bat
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> #f:\testa.txt