This is my scenario.
I have Test1.sql, Test2.sql and Test3.sql files with create, drop, insert, etc statements. The number of files could increase in the future.
I wanted to run all of them in a single script so I created a master script Master.sql, which has the below
##Test1.sql
##Test2.sql
##Test3.sql
I run it as #"Path to the script\master.sql" using SQL plus command prompt on my windows and if I wanted to run it through SQL developer, I just open the master.sql file and run it. So far so good…
Now, I have to run the master.sql in various environments with different user and schema names but the user and schema names are hard coded in the scripts. I want to be able to replace all the occurrences of the hard coded names with parameters and pass them just once on Master script execution. Both from the command prompt and using SQL developer. How do I do it? Please advice.
Have a look at the documentation of the SQL*PLUS User Guide and Reference.
From the manual:
You can bypass the prompts for values associated with substitution
variables by passing values to parameters in a script through the
START command. You do this by placing an ampersand (&) followed by a
numeral in the script in place of a substitution variable. Each time
you run this script, START replaces each &1 in the file with the first
value (called an argument) after START filename, then replaces each &2
with the second value, and so forth. For example, you could include
the following commands in a script called MYFILE:
SELECT * FROM EMP_DETAILS_VIEW
WHERE JOB_ID='&1'
AND SALARY='&2';
In the following START command, SQL*Plus would substitute PU_CLERK for
&1 and 3100 for &2 in the script MYFILE:
START MYFILE PU_CLERK 3100
When you use arguments with the START command, SQL*Plus DEFINEs each
parameter in the script with the value of the appropriate argument.
Related
I have a hive script which takes parameters in the form of --hivevar
var_name="value" from beeline and the substitution works well within hive queries.
Within this hive script, I wanted to run a shell script with a variable passed as a parameter.
I tried
!sh /home/tempscript {variable_name}
But the script does not take the substituted value, rather it passes the string '{variable-name}' to the shell script.
Any way to get around this?
I have a small sql query(which uses Wbexport utility) that I need to run daily. I want to automate this task so I am thinking to write a batch file and schedule it using windows task scheduler. Could someone tell me how to run a sql query/.sql file from command line in SQL Workbench/J?
This is documented in the manual
You need to put the query you want to run in a .sql script and then pass the name of the script on the commandline:
java -jar sqlworkbench.jar -script=run_export.sql -profile=...
or
SQLWorkbench64.exe -script=run_export.sql -profile=...
There are various ways to define the connection, either through the -profile parameter or by specifying the complete connection information. When using the profile parameter you need to make sure that the profiles are stored in the default location or specify the location where the profiles are stored.
how to run multiple queries via DOS batch file, which will create plsql packages?
I'm that far:
This is my 'driver'
#Echo Off
sqlplus [login details] #C:\runPackages.sql
EXIT
and in file runPackages.sql I have:
#C:\SqlPackage1.sql
#C:\SqlPackage2.sql
#C:\SqlPackage3.sql
Its seems, that this is working for simple Sql Queries, but its not creating packages...
Thanks for any advice.
"Well it gives me number... Just that. Like 69, or 6800. Don't know,
what does that mean."
What that means is you haven't terminated your CREATE PACKAGE scripts properly. That is SQL*Plus's way off telling you it expects you to enter another line of code. And that's why they don't run from the driver script either.
After the end; you need a new line and a slash to execute the statement. Like this
create or replace package package1 as
....
end package1;
/
Finish off each CREATE statement with the slash and your scripts will run without further input.
The teaching from this is, we need to test our individual program units first before we integrate them at a higher level.
What is the difference between these two command line used to extract tables from a database into one that can be used by mysql ?
C:> mysql -u user -p PASS database_name < ms.sql
And
mysql> source ms.sql ;
I used to do with the former and the database created contained all information but it didn't work. the second worked fine.
Second in the first case setting default character set is examplified but I found none in the homepage of the mysql an example for the second case. I am thankful for any help available.
Both of the commands can be referred as Batch Commands. I am pointing out the difference between them below.
First Command
mysql -u user -p PASS database_name < ms.sql
The above command is executing two commands at a time. One is to loggin to MySQL and other one is, passing the script file to execute using OS I/O Operator '<'.
After execution of this command it will display the sql result of the script and comes back to command prompt.(comes out of SQL
Prompt)
It is necessary to keep USE DB_name command in the begening of file to execute the script
This way is useful when you want to execute a big script without logging into mysql typically most often used.
Second Command
mysql> source ms.sql;
The above command is generally an SQL Command which will execute the script present in sql file.
It is used if you are already in MYSQL Prompt. After executing the script it will return back to Mysql prompt only
You may also use this command like executing the shell script something like mysql> ./filename
For more information please refer MySql Reference Link: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
Suppose I have wrote script Table_ABC.sql which creates table ABC. I have created many such scripts for each of required tables. Now i want to write a script that call all of these script files in a sequence so basically I want another script file createTables.sql. Mysql provides option to execute a script file from "mysql" shell application but could find some command like exec c:/myscripts/mytable.sql. Please tell me if there is any command that can be written in sql script itself to call other one in latest mysql versions or alternative for same.
Thanks
You can use source command. So your script will be something like:
use your_db;
source script/s1.sql;
source script/s2.sql;
-- so on, so forth