BEGIN
dbms_output.put_line('Welcome to PL/SQL');
END;
/
I have this code in sample.sql file.
How to run sample.sql?
You can run it using SQL*Plus (using your username/password and the name of your instance):
sqlplus username/password#instance # sample.sql
Another way would be to download free SQL Developer from Oracle, open and execute the file there.
Note that the text will not be displayed per default, you need to enable the output before.
Put the following line into your file as first line:
SET SERVEROUTPUT ON
Related
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 want to source a sql file located in "C:\Users\Administrator\Desktop\malintha.sql" location. I used following command in sql plus
SQL> #C:\Users\Administrator\Desktop\malintha.sql
But it gives me
Unable to open file error.
How to do this correctly ?
You are doing it correctly but the file either isn't there or you have no permissions.
Double check the path
Ensure you are running the SQLPlus session in a command window with appropriate privs (perhaps Run As Administrator)
Try executing from a regular user directory instead of Administrator
I just tested a SQL file with:
SQL> #C:\users\msmith\desktop\test.sql
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
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;
/