Spooling to dynamic location using SQL Developer (SQLcl) - windows

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

Related

Cant able to run sqlplus in windows batch script || ORA-28040: No matching authentication protocol exception

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.

SQL*PLUS - SET LINESIZE doesn't have any effect in my script while it's spooling

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.

scheduling script from sql developer

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.

Oracle spool location

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

Does SQLDeveloper support executing scripts?

I was trying to follow some instructions today, and it starts with the comment
REM In SQLPlus I manually copy in each line and execute it.
That's nice, I don't have SQLPlus, I have SQLDeveloper. The lines that were pasted in were of the type:
#\\server\dir\dir\dir\commandfile1.txt;
COMMIT;
...etc.
It didn't like it when I tried that in a SQL window. I opened up and pasted in the commands by hand, and it wasn't happy with that either. (Did I mention that I'm not so good with this application nor Oracle, but that everyone else was out today?) The files there started with code like:
rem
set echo on
rem
execute procedure_name ('parameter1', 'parameter2');
A co-worker did have SQLPlus, and together we got it resolved. But, is there a way for me to do this with SQLDeveloper, so I'm not stuck if he's out too?
To run scripts in SQL Developer:
#"\Path\Scriptname.sql"
(You only need the quotes if there are any spaces)
You can set a default Path: Tools menu > Preferences > Database > Worksheet > Select default path to look for scripts
I was looking through the help files and found how to do it in SQL Developer Concepts and Usage->Using the SQL Worksheet->Script Runner.
Basically, you have to precede the file name with an #. For example #C:\MyScript\Script.sql.
You can then run a batch of them this way. Note that the command doesn't seem to like spaces in the file path.
For each file you need to run, find it and drop it into SQLDeveloper. Run the script (F5) and then commit (F11). This will work for some scripts, but not all.
SQL Developer these days comes with another tool called sqlcl. This is a bit like SQLPlus but is actually using some bits from SQL Developer to give a compatible command line/scripting type interface.
You would be able to use it to execute sqlplus style commands without fighting the extras of the SQL Developer style GUI which can get confusing.
Look for it under wherever SQL Developer is sitting. If you don't have it there, you can download it and deploy it into your sqldeveloper folder.
you can do it using sqlcl in the same way how you would do it using SQL PLUS, from command line:
sqlcl user/password#host:port:sid #file.sql
file should be in the same directory where sqlcl is.
This solution is the best option if you are trying to execute a lot of instruction on sql file.
This would do it:
begin
procedure_name ('parameter1', 'parameter2');
end;
/

Resources