Executing a SQL Server Script from a batch file - sql-server-2008

I have a script that I need to execute using a batch file. Do I use SQLCMD in the batch file to run the script? Also, the script inserts data to a table in a database. How should I format the SQLCMD in the batch file so it knows what database it is suppose to work with?

First, save your query into an sql text file (text file with .sql extension). Make sure to add the USE statement at the beginning, which tells the server which database you want to work with. Using the example from MSDN:
USE AdventureWorks2008R2;
GO
SELECT p.FirstName + ' ' + p.LastName AS 'Employee Name',
a.AddressLine1, a.AddressLine2 , a.City, a.PostalCode
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON p.BusinessEntityID = e.BusinessEntityID
INNER JOIN Person.BusinessEntityAddress bea
ON bea.BusinessEntityID = e.BusinessEntityID
INNER JOIN Person.Address AS a
ON a.AddressID = bea.AddressID;
GO
Then in your batch file, you run SQLCMD and pass it the sql file (with path) as a parameter.
sqlcmd -S myServer\instanceName -i C:\myScript.sql
If you need to authenticate as well, you'll need to add in -U and -P parameters to your SQLCMD command.
Here's an MSDN article dealing with the sqlcmd utility with more details.

Use the -S switch to specify server and instance names, e.g. -S MyDbServer\Database1
SQLCMD documentation found here.

If you want to execute all .sql files (multiple sql scripts in a folder) for multiple database then create a batch file "RunScript-All.bat" with below content
echo "======Start - Running scripts for master database======="
Call RunScript-master.bat
echo "=======End - Running scripts for master database=========="
pause
echo "=====Start - Running scripts for model database========"
Call RunScript-model.bat
echo "=======End - Running scripts for master database=========="
pause
Definition for individual batch file for a specific database i.e. "RunScript-master.bat" can be written as per below
for %%G in (*.sql) do sqlcmd /S .\SQL2014 /U sa /P XXXXXXXXX /d master -i"%%G"
::pause
Create many files for different databases and call them from "RunScript-All.bat".
Now you will be all to run all sql scripts in many database by clicking on "RunScript-All.bat" batch file.

Related

Sqlcmd - run sql query script without cleartext password?

Hope you're having a great day thus far :)
I'm using this script to automate an sqlquery, so that another team can use these logs. However, they are not permitted to have the login credentials for this database, and my current script uses a cleartext password.
I suggested that we create a new database profile and limit the permissions of the profile, so that only the specific table can be accessed with read-only permissions. However, this isn't best practice, and my lead doesn't like the idea.
So, I'm trying to figure out a way to pass the login credentials through the script, without using cleartext.
Do you have any ideas?
This is the current format of the cmd I'm running in the batch file:
sqlcmd -S server.database.windows.net -U user#domain -P password -d DB_Name -i "c:\users\%USERNAME%\desktop\Blue Prism Audit Logs\eventdatetime24hr.sql" -o "c:\users\%USERNAME%\desktop\Blue Prism Audit Logs\Audit Logs\queryOut%DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%_%time:~0,2%%time:~3,2%.csv"
Expand the SQL Server Agent node and right click the Jobs node in SQL Server Agent and select 'New Job'
In the 'New Job' window enter the name of the job and a description on the 'General' tab.
Select 'Steps' on the left hand side of the window and click 'New' at the bottom.
In the 'Steps' window enter a step name and select the database you want the query to run against.
Paste in the T-SQL command you want to run into the Command window and click 'OK'.
Click on the 'Schedule' menu on the left of the New Job window and enter the schedule information (e.g. daily and a time).
Click 'OK' - and that should be it.
(There are of course other options you can add - but I would say that is the bare minimum you need to get a job set up and scheduled)
example tsql code with output
DECLARE #cmd sysname, #var sysname;
SET #var = 'Hello world';
SET #cmd = 'echo ' + #var + ' > var_out.txt';
EXEC master..xp_cmdshell #cmd;
more info here: https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/xp-cmdshell-transact-sql?view=sql-server-ver15
this will create a script that runs whenever you want it to and you set it up on the DB and then you can decide where you put the output i.e. in a target location for the other team to pick it up.

How to create a file name using MySQL or batch file where part of the file contents need to be in the name?

OK, here is what I am trying to.
Using MySQL command line, I am running a script and then saving out the data like this:
mysql -h DATABASE -u yyyy -pxxxx < E:/Step_2.sql > E:/OUTPUT_YYYY_QQ.csv
Now, either using MySQL or a Windows batch command, I want the YYYY and QQ to be a specific value. It isn't the current date though. In this example, I want the YYYY to be 2017 and the QQ to be Q4.
I have flexibility to either:
Create a new table in SQL with the values in it if needed. Then with help run a command to use those values as part of the file name.
Or I can place a file on the server somewhere to "reference" in order to grab the data and place it in the file name.
Any thoughts?
Here I have created a shell script called "test.sh", which will generate the file name dynamically, but all you need to convert this shell script into windows batch. It's an idea to you as per your comment.
test.sh
file_name=$(mysql -N -hlocalhost -uvivek -pvivek -e "select concat('OUTPUT_',year(date_column),'_Q',quarter(date_column),'.csv') file_name from table_name")
mysql -hlocalhost -uvivek -pvivek </home/vivekanand/vivek/stack/test.sql >/home/vivekanand/vivek/stack/$file_name
After running the script, I got file created as below with correct output of SQL file "test.sql"
-rw-rw-r-- 1 vivekanand vivekanand 14535 May 7 13:56 OUTPUT_2017_Q4.csv

Windows batch file - connect to remote MySQL database save resulting text Output

I normally work with PHP/MySQL. A client wants to send variables from a .bat file - to a remote MySQL - where I will then manipulate them for display etc. I do not know how to connect and send these variables from a bat file in Windows.
I have small .bat file on windows, that simply writes a few variables to a text file.
#echo off
#echo Data: > test.txt
#echo VAR_1=777 >> test.txt
#echo VAR_2=245.67 >> test.txt
The result of the .bat file is a text file test.txt created with various details in it.
I would like the .bat file commands to also:
1) connect to a remote MySQL database
connect -> '8580922.hostedresource.com'
2) save to a basic table on a remote MySQL database:
INSERT INTO `My_Database`.`My_Table` (
`VAR_1` ,
`VAR_2` ,
)
VALUES (
'777',
'245.67'
);
Is this possible?
Is so - how?
I don't have MySQL Installed and I'm not familiar with it but here is a crack at something to try, based on info from the linked page.
REM This needs to be set to the right path
set bin=C:\Program Files\MySQL\MySQL Server 5.6\bin
REM set the host name and db
SET DBHOST=8580922.hostedresource.com
SET DBNAME=MyDatabase
REM set the variables and the SQL
SET VAR_1=777
SET VAR_2=245.67
SET SQL="INSERT INTO `My_Database`.`My_Table` (`VAR_1`,`VAR_2`) VALUES ( '%VAR_1%',
'%VAR_2%');"
"%bin%/mysql" -e %SQL% --user=NAME_OF_USER --password=PASSWORD -h %DBHOST% %DBNAME%
PAUSE
Please try that and post back the resulting error message. There are many reasons that it won't work, but you need to try it to find out.
I'm not sure where test.txt comes into this but it would be a good idea export the whole SQL statement to a text file then use the correct MySQL command line switch to just run the file instead of generating the SQL inside the batch file.
There's a bit more here.
connecting to MySQL from the command line

Using Active Record structure.sql with Heroku

I have a Rails 4.2.5 application with a MySQL 5.6 database. This MySQL database has a number of foreign keys, views and functions. Schema.rb is designed to be database agnostic and therefore can't support the database specific commands necessary to modify these additional schema objects so the structure.sql functionality is provided.
http://edgeguides.rubyonrails.org/active_record_migrations.html#schema-dumping-and-you
Unfortunately, the built in structure dump tasks for MySQL do not include procedures, triggers or foreign keys. This is problematic for our team as we have to manually version control these "non standard" objects. Therefore I decided to find a solution that would allow management of the entire database schema using migrations. I landed upon this nice post by Pivotol Labs.
https://blog.pivotal.io/labs/labs/using-mysql-foreign-keys-procedures-and-triggers-with-rails
namespace :db do
namespace :structure do |schema|
schema[:dump].abandon
desc 'OVERWRITTEN - shell out to mysqldump'
task dump: :environment do
config = ActiveRecord::Base.configurations[Rails.env]
filename = "#{Rails.root}/db/structure.sql"
cmd = "mysqldump -u#{config['username']} -p#{config['password']} "
cmd += '-d --routines --triggers --skip-comments '
cmd += "#{config['database']} > db/structure.sql"
system cmd
File.open(filename, 'a') do |f|
f << ActiveRecord::Base.connection.dump_schema_information
end
end
end
desc 'load the development_structure file using mysql shell'
task load: :environment do
config = ActiveRecord::Base.configurations[Rails.env]
cmd = "mysql -u#{config['username']} -p#{config['password']} "
cmd += "#{config['database']} < db/structure.sql"
system cmd
end
end
namespace :test do |schema|
schema[:clone_structure].abandon
desc 'OVERWRITTEN - load the development_structure file using mysql shell'
task clone_structure: %w(db:structure:dump db:test:purge) do
config = ActiveRecord::Base.configurations['test']
cmd = "mysql -u#{config['username']} -p#{config['password']} "
cmd += "#{config['database']} < db/structure.sql"
system cmd
end
end
end
By making use of mysqldump from the shell I can generate a structure.sql file that contains all of the schema objects.
Currently my main problem is on Heroku I can't locate mysql dump. I installed this buildpack which provides the MySQL binaries.
https://github.com/gaumire/heroku-buildpack-mysql
However I get the error
mysqldump: not found
when running heroku run rake db:migrate.
As you can see I'm down quite the rabbit hole here. I suspect there's going to be a problem with Heroku's readonly file system anyway even if I can correctly locate mysqldump. Perhaps I should bypass non development environments in my overridden rake db:structure:dump task because structure.sql should contain a schema that's consistent across all my environments, so perhaps I can get away with not trying to write to it in production?
If anyone has managed to pull this off or has alternative approaches to managing a complete MySQL schema using Active Record migrations I'd appreciate your input.
You can troubleshoot this by running heroku run bash -a <myapp>, which will launch a bash shell in a one-off dyno with the same environment as you would get when running heroku run rake db:migrate.
Heroku's file systems are not read only, they're "ephemeral". You can create/change files in a dyno but those changes are lost when the dyno is terminated, so changes do not persist, so this approach should work, provided that you can locate the mysqldump binary.

Sqlcmd Batch Script

In my sqlserver project, there are many .sql files in different folders Like
C:\username\EtlcontrolDB\scrip1.sql.........script10.sql
C:\username\configuration\scrip1.sql.........script10.sql
C:\username\Think...........................
My client requirement is, above folders contained sql files execution through single batch script. and sql filenames and filepath input geting from .txt file and also C:\username this one will be different for each user,remains path is similor to all users,
also i want to execute some folders fully and some folders specific sql files only
Can anyone help me about this?
Thanks in advance...
There is no way to do this using sqlcmd alone.
You will have to write a batch script in a language such as python that invokes sqlcmd for every .sql file stored in a specified directory. Here is an example in Python (untested!):
import subprocess
from os import listdir
from os.path import isfile, join
def main():
onlyfiles = [ f for f in listdir('C:\username\EtlcontrolDB\') if isfile(join('C:\username\EtlcontrolDB\',f)) ]
for f in onlyfiles:
sqlcmd(f)
def sqlcmd( file ):
query = open(file).read()
sqlcommand = 'sqlcmd -Q ' + query + ' -S "Myserver" -U UserName -P Password'
subprocess.call(sqlcommand)
main()