Can we pass values of scripting variables to environment variables when we are witing codes in sqlcmd scripts or batch scripts and vice versa ?
Using batch variables in sqlcmd
a) Directly placing them in the command line. The cmd parser will expand the variables to the value as it is written in the line
set "lastName=Smith"
sqlcmd -Q "SELECT * From myTable Where LastName='%lastName%'"
b) Using declared variables in the sql query, and using the previous method to declare them in the sqlcmd command. If we have a query.sql filename containing
SELECT * From myTable Where LastName='$(LastName)'
Then it is possible to do the following call
set "lastName=Smith"
sqlcmd -i query.sql -v LastName="%lastName%"
Using output of sqlcmd in batch files
a) Send the output to a file (-o switch in sqlcmd) and then process it. See for /?
sqlcmd -i query.sql -o data.txt -h -1
for /f "tokens=*" %%a in (data.txt) do ....
b) Directly process the output of the command. Again with for /f command.
for /f "tokens=*" %%a in ('sqlcmd -i query.sql -h -1') do ....
In both cases, if what is needed is to assign value to a batch variable, you will have something like
for /f "... options ..." %%a in ('sqlcmd -i query.sql -h -1') do (
....
set "varname=%%a"
....
)
processing the output/file and assigning the data to the variables.
Related
I have a batch file that extracts one row from a database and uses that data as a variable to use the value to be a filename, and exports data as text file.
It works fine except for exporting two text files.
One of the two files is the correct file.
The other one is the unnecessary file. The filename is ".txt" and has the same data as the first one.
Here is my bat file:
SET SQLCMD=sqlcmd -S server -d db -Q "set NOCOUNT
on; select name from t1" -h-1
FOR /F "usebackq delims=, tokens=1" %%i IN (`%SQLCMD%`) DO (
CALL :PROCESSING %%i
)
:PROCESSING
SET COL1=%1
bcp "SELECT * FROM t1" queryout "C:\Export\%COL1%.txt." -S server -T -c
What do I need to fix to get only one file?
Since I am new to coding, if anyone could help, that would be very appreciated.
Thank you,
I am trying to run a command in a batch script to parse to the fourth word of a line.
This command works fine on the command line but gives an error when run inside a script. The error states :
Skip was not expected at this time
db2cmd -i -c FOR /F "usebackq skip=3 tokens=4" %%G IN ("aliasname.txt") DO DB2 UNCATALOG SYSTEM DATABASE %%G
Could you please check what might I be doing wrong here?
Two different options, achieve the same result, depends on your style.
omit the explicit db2cmd -i -c prefix for any Db2 CLP action and auto run the whole script under db2cmd . This has the advantage that it will run either in a CMD.EXE or DB2CMD.EXE window without change. In this case you do not need special quoting or escaping. The batchfile would look like this (adjust the path of db2cmd to suit your environment).
set db2cmd="C:\Program Files\IBM\SQLLIB\BIN\db2cmd.exe"
#if "%DB2CLP%"=="" %db2cmd% /w /c /i "%0" %* && #goto :EOF
FOR /F "usebackq skip=3 tokens=4" %%G IN ("aliasname.txt") DO DB2 UNCATALOG DATABASE %%G
If you prefer explicit db2cmd -w -c prefix on Db2 CLP commands, then you must escape the double-quotes as mentioned by Mark Barinstein's answer, although the %%G is required then inside the script:
db2cmd -i -c FOR /F \"usebackq skip=3 tokens=4\" %%G IN ("aliasname.txt") DO DB2 UNCATALOG DATABASE %%G
-
Additionally, you may need to fix the syntax of the Db2 CLP command e.g. db2 uncatalog system odbc data source or alternatively db2 uncatalog database depending on what you wish to uncatalog.
You have to use %G variable reference if you run command from the command line, not %%G as you use in batch files.
Double quotes must be escaped.
Try this:
db2cmd -i -c FOR /F \"usebackq skip=3 tokens=4\" %G IN (\"aliasname.txt\") DO DB2 UNCATALOG SYSTEM DATABASE %G
BTW,
There is no UNCATALOG SYSTEM DATABASE DB2 command.
There is UNCATALOG DATABASE.
Currently, the following code in a batch file works: it runs all SQL scripts in %SCRIPTFOLDER% and sends each script's output to a CSV within the same folder as the scripts.
FOR /F "tokens=*" %%S IN (
'DIR /B "%SCRIPTFOLDER%\*.sql" '
) DO (
sqlcmd -b -S %INSTANCE% -d %DATABASE% -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
IF ERRORLEVEL 1 GOTO errorhandling
ECHO %%~nS.csv successfully created
)
What I'd like to do is allow the user to specify where the generated CSVs get sent to using a variable %OUTPUTFOLDER%.
I tried placing %OUTPUTFOLDER%, which is a full path, drive, and folders (e.g. D:\some folder\output) in various positions within %%~dpnS.csv. Specifically,
%%~dp%OUTPUTFOLDER%nS.csv
and
%%~dpn%OUTPUTFOLDER%S.csv
but they didn't work and I'm (probably obviously to you) woefully inept at batch file syntax!
I understand that dp is the drive and path and that S is the file name, but I'm not sure how to integrate that with a the variable that is the path.
The iterating variable is %%S, the modifier ~dpn forces an evaluation of drive path and name.
In this case you want to specify the drive and path yourself so depending on wether %OUTPUTFOLDER% has a trailing backslash
-o "%OUTPUTFOLDER%%%~nS"
or not use:
-o "%OUTPUTFOLDER%\%%~nS"
I'm in a process of creating a batch file to list names of all SQL scripts in a folder to a text file. My plan to convert this text file to a batch file so that I can use it to execute the scripts in the server. So I would like to have the following string appended before each file name while creating the initial text file
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
This is a batch file command and I would like to be appended before the each file names.
eg:
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i 001_ALTER_PERSON.sql
The code I'm using is
set MSSQLSERVER_NAME = "%MSSQLSERVER_NAME%"
set %MSSQLSERVER_DBNAME = "%MSSQLSERVER_DBNAME%"
set myvar=sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
for /r . %%g in (*.sql) do echo %myvar% %%~nxg >> D:\test.txt
pause
Out put I'm getting
sqlcmd -S -d -i 015_ALTER_vBOARD_PAPERS.sql
Let me know how to tackle this
set myvar=sqlcmd -S %%MSSQLSERVER_NAME%% -d %%MSSQLSERVER_DBNAME%% -i
should cure your problem - % escapes %
note that spaces ARE significant in variable names, so set var = somethingwill set var[space] to [space]something
set %varname%=... is rare -it sets the variablename (contents of varname).
Even more rare to have unbalanced %... not sure that will work at all...
I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j