Search This Blog

Tuesday, May 11, 2010

SQL Server – Environment Variables in (OS) CMDExec job step

Recently I had to try and use an environment variable in a SQL job step. However I came across an issue where it would not resolve the environment variable.

I wanted to use %COMPUTERNAME%, to allow me to script the job and deploy to other servers without modifying it.

It was a weird probablem, if I ECHO the command line, it would resolve the %COMPUTERNAME%, taking the output from  the step output file I was able to run the command.  However remove the ECHO and it would fail to run the command.

After trying many different things, including trying CMD /c “command”, I tried putting CALL on the beginning of the command. This is the solution.

i.e

Original Command (not working in sql job step)

xcopy  “%COMPUTERNAME%\sourcefolder” “destinationfolder”

Fixed command

CALL xcopy “%COMPUTERNAME%\sourcefolder” “destinationfolder”

image


Share/Bookmark

1 comment: