Oracle SQL Developer how to dynamicly run sql scripts - sql

I have the following challange;
I would like to execute a batch of *.sql files on one database. The sql files are assumed to be named in ascending order of their execution sequence. So the main sql script should do a 'dir /s *.sql', then start each of the found scripts in order.
Is this possible ?
Below is something I found for SQL Server, but I want something similar for Oracle SQL Developer.
http://pradeep1210.wordpress.com/2012/03/15/executing-a-set-of-sql-script-files-sql-on-a-group-of-sql-server-databases/
Thanks in advance.
Raymond

Create a folder for eg:Batch_Files in you local machine ,which will contain all the sql script that you want to execute ,
Then open you sql developer .Create a file called batch.sql in your Batch_Files folder .
In Batch.sql add the sql files that you want to execute in sequence.
#file1.sql
#file2.sql
:
:
#fileN.sql
These files contains the code that you need to run in sequence .This is a very basic example.You can do various changes according to your need ,you can add anonymous block to print something after execution of files .I have not tested this is SQL-DEVELOPER ,but i think this will surely work for you .

Related

How to log values of variables during a sql script execution

I have a complex sql script which I am running using db2 +v -txf sqls/connection.sql This script is part of a unix service which is running lot of other scripts as well. The script is querying a temporary session table so I cannot run the script manually (since the table is gone by that time). I want to be able to run the script as part of the service but would like to log the values of variables being calculated in the sql file. For eg: The sql script as the following line timestampdiff(1, char(max(END_TS) - min(START_TS))) as ELAPSED_TIME, I would like to know the values of END_TS and START_TS.
What I have tried:
I tried adding -v to the the db2 command and it printed the entire sql being executed but not the exact values.
If the Db2-server runs on Linux/unix/windows, you can use set serveroutput on; along with call dbms_output.PUT_LINE('......'); which lets you see what you logged as output when the script ends.
dbms_output.put_line docs.
The dbms_output module contains other useful services, and people familiar with Oracle will recognize it.
If the Db2-server runs on a Z/OS or i-series you should tag your question as db2-zos or db2-400 , because the answer can depend on the platform.

Using an Oracle SQL*Plus file with an Oracle Database [Intellij]

I am trying to run many SQL scripts that are contained within a few directories for an Oracle database. My approach was to use an Oracle SQL*Plus file that could simply run each file (or directory if possible) at once. However, when I run the SQLPlus file using the default intellij run configuration, I notice that it doesn't properly call any of my scripts and then, proceeds to change the SQL Dialect of the file to just Oracle.
Is this approach even possible? I have tried changing the dialects of all the scripts to be PLUS but, with no success. I've used all the methods I have seen: #, ##, SQL > and :r to call my scripts but, everything seems to fail and then, change the dialect of the SQLPlus file. However, running the sql statements themselves inside the SQLPlus file works fine, and does not change the dialect.

SQL Server PRINT and Messages export to a .txt file

When executing IF/THEN queries in SQL Server I am using a Print Statement to let the user/myself know what has happened.
Also when I run a query SQL Server mentions how many rows have been affected.
After searching I have only come across MySQL functions that allow PRINT statements to be exported via SELECT into OUTFILE. Does SQL Server have a way to send the PRINT statements and or messages to a .txt file for logging?
If you run your script as a SQL Agent job, you can specify Output file on the Advanced settings of a step.
You don't have to use SQL Agent as a scheduler, you can run the job manually, or use sp_start_job.

Export into xml file using "FOR XML AUTO,TYPE, ELEMENTS"

When I execute this code:
select client.*
from client
FOR XML AUTO,TYPE, ELEMENT
as a query to the database, I get the output as desired, but I need to save it manually.
Is there a way to save it automatically?
And is it possible to schedule this procedure?
I'm kinda new to SQL so take it easy when you anwser please!
Thanks!
Create a DTS, or if you have a newer version of SQL Server, SSIS package to perform your query and save the results to a file.
Use the SQL Server Agent to schedule a job which runs the package when required.

No way to execute SQL script from SQL Server Query Manager like #{file.sql} oracle sqlplus syntax?

Like the title says, in oracle you can issue the following command in SQL*Plus:
SQL> select something from anothertable; #sql
SQL> #{/home/me/somescript.sql}; #load sql from file and execute it
SQL> do something else in script; #other sql
Without having to file->open the sql script to load it to the UI.
Is there an equivalent in SQL Server Query Manager? I've stumbled upon many situation where i could have used it but i couldn't be able to find a way to accomplish it.
You're not really comparing like for like Tools here.
The equivalent tool to SQL*Plus in SQL Server is the SQLCMD Utility.
In particular you will be interested in the -i switch as this allows you to provide a .sql file as input.
Edit:
In response to your comment, you could look to use the system stored procedure xp_cmdshell to launch a prompt form within a T-SQL batch that allows you to use SQLCMD. Not the most elegant solution in my opinion but it should work.
If using latter versions of SQL Server (2005 & 2008), see if the :r command in SQLCMD works for you:
:r <filename>
Parses additional Transact-SQL statements and sqlcmd commands from the file specified by <filename> into the statement cache.
If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the line that follows :r.
From sqlcmd Utility
Use isql utility http://msdn.microsoft.com/en-us/library/aa214007(SQL.80).aspx
issql ... -iinputfile
There is also SQLS*Plus tool that you can use to execute scripts within a script