I'm trying to set the output of a sqlcmd query to a variable in a batch file.
Here's my query:
sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1"
This gives me exactly what I would expect and what I want:
-----------
10
<1 rows affected>
However, when I try to set it to a variable, I used this script:
for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1"')
do set ColumnVar=%%a
echo %ColumnVar%
pause
This gives me this result instead: <1 rows affected> I'm guessing this is because the loop is setting the variable to the last line. So is there a way I could use tokens and delims to parse out the 10 instead?
Try turning on NOCOUNT:
for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "SET NOCOUNT ON; select max(Column1)+1 from Table1"') do set ColumnVar=%%a
echo %ColumnVar%
pause
Related
I have a very simple batch file that runs a SQL query file and copies the results to a printer. The problem is it prints even if the results of the query is 0 rows. How can I only print if there is data?
cd stock
sqlcmd -i testquery.sql -S localhost -U User -P password -o testresults.txt
copy testresults.txt \\printserver\share
sqlcmd -i ems_update.sql -S localhost -U User -P password
del c:\stock\testresults.txt
exit
Ok, this is what I have now:
cd c:\stock
sqlcmd -i testquery.sql -S localhost -U User -P password -o testresults.txt
#find /c /i "0 rows" "C:\stock\testresults.txt" > NUL
if %ERRORLEVEL% EQU 1 (
pause
) else (
copy c:\stock\testresults.txt \\printserver\share)
pause
sqlcmd -i ems_update.sql -S localhost -U User -P password
I cannot get the copy command to run now. What am I missing??
thanks
Take a file that you know has 0 data.for /f "delims=" %%A in ('type nodata.txt') do set nodata=%%AThenfor /f "delims=" %%A in ('type testresults.txt') do set test_result=%%AThenif not %nodata%==%test_result% copy testresults.txt \printserver\shareThis will only work if the same exact no data file is produced, like no timestamps.or
use find which returns %errorlevel%==1 if it does not find a phrase in file and 0 if it does.
Just an idea - you can check if size of the file is 0. In Bash it would be something like this:
if [ ! -s ./testresults.txt ] ; then
rm ./testresults.txt
# or do something else
else
copy testresults.txt \\printserver\share
fi
I am new to Batch.
My code:
#ECHO OFF
SET colu=
SET sn=
SET /P colu= Enter column name:
SET /P sn= Enter ID :
sqlcmd -U USER -P PWORD -S SERVER -d DBNAME-i sqlSCRIPT.sql -o LOG.txt -v delete=%colu% d_id=%%I
pause
SET /P sn= Enter ID :
sqlcmd -U USER -P PWORD -S SERVER -d DBNAME-i sqlSCRIPT.sql -o LOG.txt -v delete=%colu% d_id=%%I
pause
SET /P sn= Enter ID :
sqlcmd -U USER -P PWORD -S SERVER -d DBNAME-i sqlSCRIPT.sql -o LOG.txt -v delete=%colu% d_id=%%I
The code works and does what I need it to do which is delete records from the db but I was hoping that there was a more efficient way to write it.
How would I implement a while loop in batch that will keep asking the user to enter the id until they press the end key? Also I realize that having pause in between sqlcmd is highly inconvenient.
How about something like this:
#ECHO OFF
SETLOCAL
SET /P "colu=Enter column name:"
:Prompt
REM Clear any existing values.
SET "sn="
ECHO Enter a blank value to stop the operation.
SET /P "sn=Enter ID:"
REM Check for exist conditions.
IF "%colu%"=="" GOTO :EOF
IF "%sn%"=="" GOTO :EOF
REM If we get here, data was entered for both
sqlcmd -U USER -P PWORD -S SERVER -d DBNAME-i sqlSCRIPT.sql -o LOG.txt -v delete=%colu% d_id=%sn%
ECHO.
ECHO.
REM Ask again.
GOTO Prompt
:EOF
ENDLOCAL
This will keep prompting until you enter a blank value for either colu or sn. Also, I noticed you are using %%I in your SQL statement - I think you meant to use %sn% instead.
I'm using SQLCMD to get the count of rows in a table, but I also want to be aware if the query hits an error.
The sqlcmd I'm using looks like this:
sqlcmd -S %server% -U %user% -P %pass% -b -Q "select count(*) from %table%"
If it works, it will return:
-----------
10205
(1 rows affected)
(Note, there is a blank line above the ------- for the column name I'm not specifying.)
If I pass in a table that doesn't exist, I get the following response:
Msg 208, Level 16, State 1, Server devServer, Line 1
Invalid object name 'dbo.no_table'.
Since I have the -b flag, I can check ERRORLEVEL for a value (in this case, 1).
To store the count variable, I've been using the following line:
for /F %%i in ('sqlcmd -S %server% -U %user% -P %pass% -b -Q "select count(*) from %table%" ^| findstr /r "[^(][0-9]"') do SET /a rec_count=%%i
After the for, %errorlevel% returns 0. Even inside the do, errorlevel is 0.
Is there any simple way to run sqlcmd, store the count if there is not an error, and print both lines if there is an error?
Commands that are executed by FOR /F are implicitly executed via a new CMD session. For example, with for /f %a in ('echo hello') do ..., the command that is executed becomes C:\Windows\system32\cmd.exe /c echo hello.
Your command is properly setting the ERRORLEVEL, but then the value is lost as soon as the child CMD session terminates and control is returned to your batch script.
So the /b option is not really doing any good for you, and can be dropped.
You can suppress the header info by adding the -h -1 option.
You can suppress the (1 rows affected) message by prefixing your command with set nocount on;
You can add the -r 1 option to cause error messages to appear on stderr instead of stdout. This will prevent FOR /F from processing any error, and the error message will appear on the screen instead.
You can clear the rec_count variable before you execute the command. Then it will remain undefined if there was an error, else it will contain the count if there was no error.
set "rec_count="
for /f %%A in (
'sqlcmd -S %server% -U %user% -P %pass% -h -1 -r 1 -Q "set nocount on;select count(*) from %table%"'
) do set "rec_count=%%A"
if not defined rec_count echo There was an error!
One other thing you might consider is using environment variables recognized by SQLCMD for your server, username, and password. Then you won't have to use the -S, -U, or -P options. This is especially handy if your batch script runs many SQLCMD commands.
set "sqlcmdServer=YourServer"
set "sqlcmdUser=YourUserName"
set "sqlcmdPassword=YourPassword"
set "rec_count="
for /f %%A in (
'sqlcmd -h -1 -r 1 -Q "set nocount on;select count(*) from %table%"'
) do set "rec_count=%%A"
if not defined rec_count echo There was an error!
The reason errorlevel does not seem to be getting set is because the for command is executing successfully, regardless of how the code that it loops through executes. So you can only interact with the errorlevel that is set by the sqlcmd command on the same line (inside the for loop brackets).
You should be able to use || (double pipe) after the sqlcmd command. Any code after || will only run if the previous command fails. Example:
notACommand || echo test
Will return "test". While the following will output only "a command":
echo a command || echo test
I can't test it, but something like the following should work for you:
for /F "EOL=(" %%i in ('sqlcmd -S %server% -U %user% -P %pass% -b -Q "select count(*) from %table%" ^|^| echo fail') do (
SET rec_count=%%i
)
if "%rec_count%"=="fail" echo SQL command failed
If the output is exactly as you say, then you should not need the findstr command - just set ( open bracket as an EOL character in the for loop, so you effectively drop the "(1 rows affected)" line. You will probably want to use the variables differently, but this is just one way you can tell if the sqlcmd command has failed or not.
As for outputting the error - a bad solution is to run the same sqlcmd command again. Something like the following:
set command=sqlcmd -S %server% -U %user% -P %pass% -b -Q "select count(*) from %table%"
for /F "EOL=(" %%i in ('%command% ^|^| echo fail') do SET rec_count=%%i
if "%rec_count%"=="fail" (%command%) else echo rec_count is %rec_count%
Note that I removed the /a switch when setting the rec_count variable, because it can now be set as a word.
How should i include the output of a findstr which retrieves me the name of the stored proc in sqlcmd?
#ECHO OFF
FOR %%f IN ( *.sql) DO (
var xyz = echo %%f | findstr "^test*"
sqlcmd -d databaseX -S server -e -i xyz
)
where xyz will evaluate to testStoredProc1.sql, testStoredProc2.sql and so on.
These stored procs have update/insert and select statement
How can i capture the value of xyz and pipe it to sqlcmd?
#echo off
for %%f in (test*.sql) do (
sqlcmd -d databaseX -S server -e -i %%f
)
NOT TESTED!
For further information check FOR /F , SET ,DelayedExpansion
I'm trying to get the value of a SQL statement when I run it in a DOS batch file ...
sqlcmd -E -S DEVSERVER -Q "SELECT COUNT(1) as [CaseCount] FROM Cases"
I'm not after the error level as in this stackoverflow question, rather I'm after the actual count returned from the database, so I can do some additional logic.
You can easily save the result of the execution into a text file, either by using the -o sqlcmd flag or by using the standard > redirector. You can then format this file by removing the column header (flag -h) and removing the rowcount from SQL Server (SET NOCOUNT ON).
The following script will generate a file result.txt with only the value of COUNT(1) and a line break:
SQLCMD -E -S devserver -Q "SET NOCOUNT ON; SELECT COUNT(1) FROM Cases" -h -1
> result.txt
And then read the value back with ...
set /p value=< result.txt
echo %value%
You can avoid an extra/temporary file, by calling sqlcmd.exe using the FOR batch command:
for /F usebackq %%i in (`sqlcmd -E -S "devserver,4711" -h-1 -Q "SET NOCOUNT ON; SELECT COUNT(1) ..."`) do (
set count=%%i
)
if not defined count (
echo Failed to execute SQL statement 1>&2
) else (
echo %count%
)
Some notes:
If called from an interactive CMD.EXE session, i.e. the command line, use %i instead of %%i
The -h-1 option tells sqlcmd.exe to suppress the column headers, so the output is really only one line of text.
I used a fictional port 4711 with the server, to show that you need to put the whole "server,port" specification in double quotes. Otherwise, due to CMD's command line parsing rules, sqlcmd would see server and port as to distinct arguments and fail.
The answer from #GerardoLima was 90% of the way there. You additionally need to evaluate the input value with set /a to strip the leading whitespace.
This is not required with the answer from #Christian.K
SQLCMD -E -S devserver -Q "SET NOCOUNT ON; SELECT COUNT(1) FROM Cases" -h -1 > result.txt
set /p raw=< result.txt
echo '%raw%'
set /a value=%raw%
echo '%value%'
My output was
' 0'
'0'