Log Batch File Status/Timings to SQL Server - sql-server

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.)

Related

SQLCMD in bat file can no longer query Database on Network Server

Hi I'm looking for some advice on what changes to look for on our server.
I have successfully been using a bat file on users desktops to query a stock database held on one of our network servers.
I have a few other bat files used for similar tasks and they have all of a sudden stopped working.
We have MS SQL Server 2012 on the database server, and using XP & Win 7 desktops. The DB is on SERVER2 and instance name MSSQL2012
The .bat file is like below
set /P sn= Enter Serial: %=%
sqlcmd -S SERVER2\MSSQL2012 -i C:\SerialCheck\SerialNumber.sql -o C:\SerialCheck\Output.txt -v serial=%sn%
and the SerialNumber.sql is like below
USE DATABASE
GO
select tblStock.SerialNumber,tblStock.StockD,tblStock.YearManufacture,tblStock.Price
FROM tblStock
where SerialNumber like '%$(sn)%'
GO
It was working fine and then all of a sudden stopped. I've done a lot of checks on the server to figure out what has happened but have got nowhere.
If I run my .bat on any desktop, nothing happens, and no output.txt file is created
If I run my .bat on the actual SERVER2 it works fine, so the issue must be firewall/access related but i'm getting nowhere
Can anyone suggest;
how i can produce some sort of error files to give me a better idea of what the problem is?
what changes to check for in relation to access
Thanks in advance
EDIT if I just enter "sqlcmd -S SERVER2\MSSQL2012" on a command prompt window I get an error that sqlcmd is not a recognised command, do I need to have some SQL program installed? it worked fire before so I think not?

Automate backup command not working in job scheduling

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?

Executing a stored procedure using Windows task Scheduler

I've been trying to set up a schedule to run a stored procedure every hour in Windows Task Scheduler (as I'm using SQL Express and can't install 3rd party tools) but after trying various methods such as running a .bat file from task scheduler, opening SqlCmd utility from task scheduler and passing either the command line syntax or a .sql script file I'm having no luck.
I know this can be done and therefore I'm sure it's something I've missed but if anyone can share their experience of this I'd very much appreciate it.
The following command is in the batch file...
sqlcmd -E -i"C:\Users\Administrator\Desktop\test.sql" -o"C:\Users\Administrator\Desktop\dump.txt"
Thanks a lot
If you are an admin on the sql instance (Since you are using SQLExpress I bet you are trying to do this on your own computer so there is a high chance your user is an admin of the sql instance) you should not use -E at all, just ignore it.
Second, specify the server even if you are working on local.
Start with a simple sql command like below:
sqlcmd.exe -S "." -d MY_DATABASE -Q "SELECT * FROM MY_TABLE"
Replace MY_DATABASE and MY_TABLE with your dbname and table name. Make sure you can run it from command line. It should return the data from your table. (Beware command line options are case-sensitive so -s is not same as -S)
Last, do not try to feed parameters through task scheduler. Put the command with all parameters in a .bat file and just run the batch from task scheduler.
I have recently had a similar issue and my experience may assist you. I was calling a small app i.e. EXE from a batch file. I was scheduling the batch file to run from the Windows Task Scheduler. The app was accessing the SQL data using Windows Authentication.
I could run the app directly i.e. click on the EXE to run it.
I could run the app from the batch file.
But if I tried to run the scheduled task it seemed to start but did nothing and posted no errors that I could find.
I found if I changed the app to run with SQL Authentication it could be run from the Task Scheduler.
I suspect there is something about the context of the Windows Authentication when it is run from Task Scheduler that is not recognised by SQL.

How do I launch multiple sqlcmd windows from a batch file?

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

SQL Server 2008 generate script wizard gives me a script that results in "unclosed quotation marks"

I have an SQL server 2008 database instance on one machine. Now I want to copy this database to another machine. I use the script wizard inside SQL Management Studio to generate a SQl-script with the schema and data. The script-file is rather big (around 17 GB).
Then I run the sql-script on the target machine it results in a :
Msg 105, Level 15, State 1 error with the message:
Unclosed quotation mark after the character string
I do understand the problem of what unclosed quotation marks mean. But I don't understand why the error happens. Isn't the script generator able to handle quotations inside text strings like...hello, what's up...correctly and create a script that will escape such characters?
Is their a limit on the length of text for the script wizard? Is this causing the problem.
I don't want to and I cannot open the script-file in a text editor (too large, text editor will crash) and manually fix the problems.
Do you have any ideas?
Solution for the SQL Server Import Issue
Pre-condition
In order to move the data from one SQL Server to another (e.g. from Production environment to Test environment) makes sense to use "Generate scripts" feature which is available in database options in SQL Server Management Studio. The result of this operation is text file with SQL commands that can be executed on another SQL Server. Usually these files are too big to execute them in SQL Server Management Studio, so we need to use sqlcmd command line utility from SQL Server installation package. In the most cases utility works smoothly and additional user actions are not necessary.
Issue description
In some rare cases the sqlcmd utility can fail with the import and raise the following error: "Unclosed quotation mark after the character string ..." which indicates that one of SQL queries has not been executed. This happens because sqlcmd works using stream processing, i.e. it reads some piece of data, processes it, reads next piece and so on. In some cases an input file can contain huge SQL instruction which size is bigger than the amount of the data that could be processed by sqlcmd at a time, so sqlcmd tries to execute broken SQL and fails.
Possible solutions
In order to fix this issue 2 approaches can be used:
The sqlcmd utility can accept the "-a" parameter which defines the maximum size of packet (piece of data) that will be used during processing. The maximum value is 32767, the default value is 4096, so it makes sense to always use this parameter with maximum value.
sqlcmd -i input.sql -a 32767 -o import_log.txt
If the first approach didn't help and issue still appears, there is another, more difficult solution:
Install the Cygwin
During the installation, after some standard screens, stop on the screen "Select packages"
In "Search" field, enter "sed", and in the tree below expand the "Base" category and choose version not less than 4.2.2 for installation
Complete installation
Note: "sed" is the Linux utility which allows stream-based file processing
After installation is completed, run "Cygwin64 Terminal" from the desktop. We will use it for next steps
Go to the directory where the SQL file generated by SQL Server Management Studio is located. You need to use Linux style slashes "/" instead of Windows style which is "\"
cd d:/temp
Change the encoding of the SQL file from UTF-16LE to UTF-8, because "sed" cannot process UTF-16LE, this conversion is safe for the data. The result will be a new file, that we will use in next step
iconv -f UTF-16LE -t UTF-8 input.sql > input_utf8.sql
Convert the new file, to have one SQL query in one batch. The result will be a new file, that we will use in next step
sed -e 's/^INSERT/GO\nINSERT/' input_utf8.sql > input_utf8_adapted.sql
Now the file "input_utf8_adapted.sql" should be processed by sqlcmd without any issues, so we can execute the following:
sqlcmd -i input_utf8_adapted.sql -a 32767 -o import_log.txt
After execution is done, please check import_log.txt to make sure that no errors appeared
I ended up on this question after trying to find a solution to a similar problem I had. I also needed to dump a DB (with data) via Generate scripts wizard and the resulting file was too big to be executed from SSMS. So I tried the sqlcmd but ended with the error
Sqlcmd: Error: Syntax error at line 10 near command '"' in file 'script.sql'.
It turned out the cause of the issue was a record containing data with jQuery syntax in it - $(".someclass"). It's because it is also a way how to insert a variable into sqlcmd.
The solution is to disable variable substitution by adding -x command line argument.
Not a direct answer to the question but to duck this issue you could use one of the following other methods of copying the database to the new location.
Copying Databases with Backup and Restore
Using Detach and Attach
Method 1 is usually preferable as it keeps the source DB online and detaching can cause information held in the master database about the source to be lost (e.g. full text enabled status)
EDIT: Just noted from your comment that you're running sqlcmd -S server\database -i script.sql. There is a -I switch that stands for "Enable Quoted Identifiers". Try to run the command with this switch.
Btw, to edit a large file, consider using a nice editor like Notepad++ or UltraEdit. I wouldn't use a workstation without em :)

Resources