Does SQLDeveloper support executing scripts? - oracle

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;
/

Related

Spooling to dynamic location using SQL Developer (SQLcl)

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

How to execute oracle query that was saved in a .txt file

I have too long oracle query that I saved in .txt file. and I am using Toad to execute that..
some it is too difficult to open such large code file and execute the code..
Is there any query that I can execute and give path to file that execute the code placed in that file.
For example something like that:
Execute code C:/My Code/code1.txt;
If you have a TOAD locally installed, you can use
#C:/My Code/code1.txt
This works from an editor tab in Toad 12.10.0.30 with the slashes going either direction and pressing F5 or "execute as script". The file can end in .txt or .sql but it is better to use .sql extension as it represents what the file contains.
It also works when using other programs such as sqlplus or sqlDeveloper

'vi' is not recognized as an internal or external command

When I give edit command in sqlplus, I'm receiving the below error:
I searched on the net and I think, I need to update the environment variable.
Current path value is:
E:\app\sasinghc\product\11.2.0\dbhome_1\bin;D:\app\sasinghc\product\11.2.0\dbhome_1\bin;C:\Program
Files (x86)\RSA SecurID Token Common;C:\Program Files\RSA SecurID
Token
Common;C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program
Files\TortoiseSVN\bin;C:\Program Files (x86)\Microsoft SQL
Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL
Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL
Server\100\DTS\Binn\
I updated this value to
C:\Windows\System32
but it got even worse, I was unable to open sqlplus, so I rollback the changes.
Now, my sqlplus is working but still I'm able to open afiedt.buf file.
Can you please suggest on this?
ED[IT] command run external editor defined by _EDITOR variable. You can view all variable values with DEF[INE] command. I think you must reset _EDITOR value to some INSTALLED external editor, as Notepad or Wordpad. Use DEF[INE] _EDITOR command, for example: def _editor = notepad
By default a Windows installation of Oracle uses Notepad as the SQL*Plus editor. You seem to have changed that to vi, possibly accidentally.
You can see the current setting with:
SQL> define _editor
which will presumably just show 'vi'. To change it back to Notepad you would do:
SQL> define _editor = "Notepad"
or use the full path. If you have vim installed you can give the full path to that as well, since it doesn't seem to be in your path variable.
You can read more about DEFINE and more specicially the EDITOR value in the SQL*Plus documentation.
You may be picking this change up automatically via a login.sql or glogin.sql profile script - perhaps copied from a Linux/UNIX environment or a PC with Gnu tool installed, etc. If you find and edit that file you can make that change to your preferred editor automatic - whenever SQL*Plus runs it will invoke that login script and set things up for you. It appears it is also currently setting your SQL prompt, so you need to change the file, not replace it completely.
Read more about SQL*Plus configuration via profile scripts.

Make sqlcmd work with batch separators

Background information:
I am trying to set up a batch job for syncing stored procedures between different sql servers. I did export all stored procedures into seperate .sql files into my folder. Whenever I want to create a new stored procedure or change it for those multiple servers, I test it on 1 server until I got it the way i want it. After that I export it into my folder. To then deploy it to the other servers, I simply execute a batch - that's it.
I've almost figured out my batch the way i want. The only problematic thing is, that sqlcmd seems not to like the standard batch separator 'GO' ('Incorrect syntax near 'GO'.'). So i've changed my default batch separator to 'RUN', replaced each 'GO' command with 'RUN' in my .sql files and added -c RUN to my sqlcmd command.
However it then tells me 'Incorrect syntax near 'RUN'.'.
For testing purposes I just tried it out with 1 shrinked sql file, looking like this:
USE [MyDatabase]
GO
Even that did not work. How can I make it work?
Try leaving out GO or RUN from these scripts. You can change database context without them.
You should be fine to do something like this:
USE MyDatabase;
--First query
USE MyOtherDatabase;
--Second query

SQL Plus change current directory

How does one change the current directory in SQL Plus under windows.
I am trying to write a script with several "# filename" commands.
I know that one can open a script with the File --> Open command, which will change the current directory, but I am looking for a way to do this automatically unattended.
Resolution
Based on Plasmer's response, I set the SQLPATH environment variable in Windows, and got something that's good enough for me. I did not try to set it with the HOST command (I doubt that it will work).
Pourquoi Litytestdata's answer is a good one, but will not work for me (the directories are too far apart). And of course Guy's answer that it cannot be done is also correct. I will vote these two up, and accept Plasmer's answer.
Here is what I do.
Define a variable to help you out:
define dir=C:\MySYSTEM\PTR190\Tests\Test1
#&dir\myTest1.sql
You can't cd in SQL*Plus (you can cd using the host command, but since it is a child process, the setting won't persist in your parent process).
I don't think that you can change the directory in SQL*Plus.
Instead of changing directory, you can use ##filename, which reads in another script whose location is relative to the directory the current script is running in. For example, if you have two scripts
C:\Foo\Bar\script1.sql
C:\Foo\Bar\Baz\script2.sql
then script1.sql can run script2.sql if it contains the line
##Baz\script2.sql
See this for more info about ##.
Could you use the SQLPATH environment variable to tell sqlplus where to look for the scripts you are trying to run? I believe you could use HOST to set SQLPATH in the script too.
There could potentially be problems if two scripts have the same name and both directories are in the SQLPATH.
I don't think you can!
/home/export/user1 $ sqlplus /
> #script1.sql
> HOST CD /home/export/user2
> #script2.sql
script2.sql has to be in /home/export/user1.
You either use the full path, or exit the script and start sqlplus again from the right directory.
#!/bin/bash
oraenv .
cd /home/export/user1
sqlplus / #script1.sql
cd /home/export/user2
sqlplus / #script2.sql
(something like that - doing this from memory!)
With Oracle's new SQLcl there is a cd command now and accompanying pwd.
SQLcl can be downloaded here: http://www.oracle.com/technetwork/developer-tools/sqlcl/overview/index.html
Here's a quick example:
SQL>pwd
/Users/klrice/
NOT_SAFE>!ls *.sql
db_awr.sql emp.sql img.sql jeff.sql orclcode.sql test.sql
db_info.sql fn.sql iot.sql login.sql rmoug.sql
SQL>cd sql
SQL>!ls *.sql
003.sql demo_worksheet_name.sql poll_so_stats.sql
1.sql dual.sql print_updates.sql
SQL>
Have you tried creating a windows shortcut for sql plus and set the working directory?
I think that the SQLPATH environment variable is the best way for this - if you have multiple paths, enter them separated by semi-colons (;). Keep in mind that if there are script files named the same in among the directories, the first one encountered (by order the paths are entered) will be executed, the second one will be ignored.
Years later i had the same problem. My solution is the creation of a temporary batchfile and another instance of sqlplus:
In first SQL-Script:
:
set echo off
spool sqlsub_tmp.bat
prompt cd /D D:\some\dir
prompt sqlplus user/passwd#tnsname #second_script.sql
spool off
host sqlsub_tmp.bat
host del sqlsub_tmp.bat
:
Note that "second_script.sql" needs an "exit" statement at end if you want to return to the first one..
for me shelling-out does the job because it gives you possibility to run [a|any] command on the shell:
http://www.dba-oracle.com/t_display_current_directory_sqlplus.htm
in short see the current directory:
!pwd
change it
!cd /path/you/want

Resources