title: TSQL statement in OSQL fails when called from a batch file called from a batch file
body: <p>I am automating various EXEs via a batch file, including calling an existing batch file which in turn executes various TSQL statements via OSQL.exe.</p>
  
  <p>The existing batch file works fine. However, when called by my batch file, a call to osql.exe fails, which leads to the batch file quitting itself.</p>
  
  <pre><code>:check_user_privs
  
  %OSQLPATH% %CONNECTSTRING% -S "%SERVER_INSTANCE%" -d "%DBNAME%" -Q "DECLARE @userName varchar(100) ; set @userName = user_name() ; IF IS_SRVROLEMEMBER ('sysadmin') != 1 RAISERROR ('This user ''%s'' is not a member of the ''sysadmin'' group.' , 16 , 127 , @userName )"  &gt; /nul
  if errorlevel 1 (
      echo ** ERROR - The user is NOT a sysadmin member on "%SERVER_INSTANCE%" - Exiting ** 1
      echo.
      pause
      exit
  )
  </code></pre>
  
  <p>Suffice to say, the error occurs. With ECHO ON, that first line becomes:</p>
  
  <pre><code>"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE" -U"ADMIN" -P"PASSWORD" -S "SQL_SRVR10" -d "DB01" -Q "DECLARE @userName varchar(100) ; set @userName = user_name() ; IF IS_SRVROLEMEMBER ('sysadmin') != 1 RAISERROR ('This user ''ADMIN'' is not a member of the ''sysadmin'' group.' , 16 , 127 , @userName )"  &gt; /nul
  </code></pre>
  
  <p>This line is identical when I am calling the script directly from the command line or from my own batch file.</p>
  
  <p>I've manually captured the environment variables that are set when this batch file is called from my batch file. I then set them manually at the command line and then run the batch file manually. No repro.</p>
  
  <p>I changed the TSQL statement to simply output the return value of the function IS_SRVROLEMEMBER(). Both from calling from my script and command line, the output is identical - "1":</p>
  
  <pre><code>"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\OSQL.EXE" -U"ADMIN" -P"PASSWORD" -S "SQL_SRVR10" -d "DB01" -Q "SELECT IS_SRVROLEMEMBER ('sysadmin')"
  </code></pre>