How do I launch multiple sqlcmd windows from a batch file that all point to the same database? For example, when I run the .bat file I want it to spawn N number of windows based on a parameter that I pass into it (ex. 5). Each of these 5 windows should open on my desktop and all connect to the same database. That's what I want to do first. Once I have that working, I then want to have each of those 5 windows to run a distinct .sql script that performs inserts, queries, updates, deletes, calling stored procedures...essentially emulating a production environment to help us in debugging efforts (under a user load). I want to see the output of each .sql commend flying by in the sqlcmd window while it is being executed.
I found:
http://hammerora.sourceforge.net/
which is a GUI tool that is focused on TPC-C load testing, but it is not exactly what I want. I bring it up because it is a similar concept that I want to do only driven by batch files on a smaller scale (ex. 20 concurrent users max).
I created a system like this back in the late 90's for Oracle scalability testing but I've been out of the database business since then and can't remember how to do it and how different it would need to be to support SQL Server. So I know it is possible in Oracle, but just not sure about SQL Server given the command line tool and scripting capabilities.
Does anyone have any information about what it would take to make this work?
Ex. Create a launch3users.bat file that looks like:
sqlcmd -d MichaelTest -run this 1.sql file
Pause
sqlcmd -d MichaelTest -run this 2.sql file
Pause
sqlcmd -d MichaelTest -run this 3.sql file
Pause
where each of those would spawn a sqlcmd window and run the proper .sql script which could do DML operations or called stored procedures.
Thanks,
Michael
You simply add "start" to the beginning of commands.
start sqlcmd -d MichaelTest -i 1.sql
start sqlcmd -d MichaelTest -i 2.sql
start sqlcmd -d MichaelTest -i 3.sql
Related
My scheduled task (on Windows Server 2008 R2) is "successfully" running each morning, but the output of sqlcmd does not get updated in the output file. The log file changes to show that the batch file ran, but the output file remains the same.
When I run a .bat file with the lines of code below, it works just fine. The .csv file gets updated with the most recent query's results.
sqlcmd -S SERVERNAME -i my_query.sql -s "," -o c:\scripts\My_query\query_results.csv -W -h-1
ftp.exe -s:"c:\scripts\My_query\file_upload.ftp"
Useful Information:
I am running the scheduled task as my user which is a server admin and has access to the server in question
I have tried giving explicit access to the user for full control of all files and folders involved
I have tried running C:\Windows\System32\cmd.exe as the action with the argument /c C:\scripts\My_query\my_process.bat to make sure the entire process runs
Task scheduler is working fine for other tasks, but it has this same problem with five similar processes
Are the tasks configured to run whether the user is logged on or not? If so, it will never find your SQL file because the default working directory is SYSTEM32 when you configure a scheduled task that way. You will need to provide the full path to the SQL file.
I want Task Scheduler in Window run DailyJob.bat daily at a certain time, this file having content like this:
#echo off
DB2 CONNECT TO dbName USER usrName USING password
DB2 .........
DB2 ..........
Moreover, Task Scheduler will run this file by cmd.exe automatically, but cmd doesn't understand DB2 command. Please help me, thanks !
You need to call your db2 statements with the db2cmd command (http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0002036.html?cp=SSEPGG_10.5.0%2F3-6-2-6-35)
From CMD.exe
db2cmd -i -c db2 list node directory
There are a lot of related questions in the Web about this problem.
I am using PostgreSQL 8.4 on my Windows Server 2012 box. I have made a configuration with pgagent for job scheduling. I need to schedule an auto backup on a daily basis on my system using with job scheduling. I searched the net and created a batch file and located the file path in job scheduling.
If I run the batch file on my server then it works fine, but when I call it using scheduling in PostgreSQL then it goes in the running stage, but does not give any kind of result.
Below is the command I used for taking backup:
"c:\Program Files (86)\path\to\bin" pg_dump.exe -i -h hostname -U username -F c -b -v -f "backup\file\path\filename.backup" databasename
This command is working fine on the command prompt and when calling the batch file, but does not give any output from PostgreSQL job scheduling.
Does anyone have any idea about this kind of issue?
I am using a SQL Server database. I have these SQL queries:
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Is it possible to run these scripts using a batch file? The database is a remote database.
Thanks!
Save the commands in a .SQL file, ex: ClearTables.sql, say in your C:\temp folder.
Contents of C:\Temp\ClearTables.sql
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Then use sqlcmd to execute it as follows. Since you said the database is remote, use the following syntax (after updating for your server and database instance name).
sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql
For example, if your remote computer name is SQLSVRBOSTON1 and Database instance name is MyDB1, then the command would be.
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
Also note that -E specifies default authentication. If you have a user name and password to connect, use -U and -P switches.
You will execute all this by opening a CMD command window.
Using a Batch File.
If you want to save it in a batch file and double-click to run it, do it as follows.
Create, and save the ClearTables.bat like so.
echo off
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
set /p delExit=Press the ENTER key to exit...:
Then double-click it to run it. It will execute the commands and wait until you press a key to exit, so you can see the command output.
Check out SQLCMD command line tool that comes with SQL Server. http://technet.microsoft.com/en-us/library/ms162773.aspx
Use the SQLCMD utility.
http://technet.microsoft.com/en-us/library/ms162773.aspx
There is a connect statement that allows you to swing from database server A to server B in the same batch.
:Connect server_name[\instance_name] [-l timeout] [-U user_name [-P password]]
Connects to an instance of SQL Server. Also closes the current connection.
On the other hand, if you are familiar with PowerShell, you can programmatic do the same.
http://technet.microsoft.com/en-us/library/cc281954(v=sql.105).aspx
Different ways:
Using SQL Server Agent (If local instance)
schedule a job in sql server agent with a new step having type as "T-SQL" then run the job.
Using SQLCMD
To use SQLCMD refer http://technet.microsoft.com/en-us/library/ms162773.aspx
Using SQLPS
To use SQLPS refer http://technet.microsoft.com/en-us/library/cc280450.aspx
I would like to enhance an existing bat file to log execution duration to a SQL Server table. The current bat file has a single line that calls a command line utility.
I thought I would leverage something like this, SQL Statements in a Windows Batch File. Pseudo code:
StartTime = Now()
hyperioncommandlineshell.cmd /a:parm1 /b:parm2 /c:parm3
sqlcmd.exe -b -S myhost -E -d mydatabase -Q "Insert Into MyTable Values (Current_Timestamp, 'MyProcess', Now() - StartTime)" -W
Some questions:
The server that this bat file runs on doesn't have the SQL tools, and I see from this post that it does require an installation (you can't just copy over the sqlcmd.exe file). This will meet with resistance. Is there another way to execute a SQL statement from a batch file without having to install software?
I don't have experience with BAT files. Can someone provide guidance on how to get the duration of a process (like grabbing the start time, and calculating the difference at the end)?
I would probably try using another tool I'm more familiar with, but I'm trying to do this in bat so that the change only affects one existing object, and doesn't require additional objects.
Windows computers come with ODBC drivers already installed, so you likely have an ODBC driver for SQL Server. If so, then you might be able to get Microsoft's osql utility to run T-SQL statements from DOS. Here's the docs for it on MSDN:
http://msdn.microsoft.com/en-us/library/aa214012(v=SQL.80).aspx
It was designed for SQL Server 2000, so there may be some issues connecting to later versions of SQL Server, but it is worth a try. If it works, then you won't have to install anything special to connect to your SQL server (though you may need to create an ODBC data source name for the server...). From Windows Vista+, click Start and type ODBC to open the ODBC Data Source Editor.
Using SQLCMD will require that you install the Native Client, or at least SNAC (discussion thread: http://us.generation-nt.com/answer/how-install-only-sqlcmd-exe-utility-help-87134732.html) to simply run SQLCMD without installing the entire Native Client (though, SNAC still needs to be installed). I haven't heard of SNAC before, so that will take a bit of research. I assume installing anything will be met with the same resistance, so if you can overcome that resistance, installing the Native Client is probably your best bet.
As for the elapsed time. You can use %DATE% %TIME% to get the current date/time. So you could use something like the following to capture the start time, run your process and then capture the end time -- posting them all to the database:
set StartTime=%DATE% %TIME%
hyperioncommandlineshell.cmd /a:parm1 /b:parm2 /c:parm3
set EndTime=%DATE% %TIME%
sqlcmd.exe -b -S myhost -E -d mydatabase -Q "Insert Into MyTable Values ('%StartTime%', 'MyProcess', '%EndTime%')" -W
You won't be able to do the StartTime - EndTime computation with DOS itself, but you can store both the start and end times in the table an use SQL to do it.
The format of %DATE% and %TIME% are based on the format that the machine is setup to use. You can type echo %DATE% %TIME% at a DOS prompt to see how it is formatted for you. You will likely have to store these values in varchar fields since the format may not automatically convert to a datetime value. If it does automatically convert, then you could do the computation in the SQL statement from DOS, like this:
sqlcmd.exe -b -S myhost -E -d mydatabase -Q "Insert Into MyTable Values ('%EndTime%' - '%StartTime%', 'MyProcess')" -W
(FYI - I used your pseudo-code for all examples, so nothing is tested.)