SQLCMD bat not working

  • Very new to sqlcmd. I can run my command in a cmd, however if I create a bat file with the exact same command it fails.

    Command:

    sqlcmd -E -S HQSQL2010 -d sophos51 -Q "Select CAST(IPADDRESS / 256 / 256 / 256 % 256 AS VARCHAR) + '.' + CAST(IPADDRESS / 256 / 256 % 256 AS VARCHAR) + '.' + CAST(IPADDRESS / 256 % 256 AS VARCHAR) + '.' + CAST(IPADDRESS % 256 AS VARCHAR) AS IPADDRESS, NAME AS Computername From ComputersANDDeletedComputers where managed=0 Order by IPAddress" -w 1000 -o C:\temp\UnmanagedComputers.csv

    bat Error:

    Msg 102, Level 15, State 1, Server HQSQL2010, Line 1

    Incorrect syntax near '256'.

    Any help would be greatly appreciated !!!

  • Just a guess, within a batch file, it might be seeing the '%' and subsequently expecting a parameter number to replace with a parameter. From a superficial web search it looks like you might get your desired results by doubling up each % sign as a way of 'escaping' it so that the windows cmd will pass it to sqlcmd.

  • Thanks for the reply. What I did was took the Select statement out of the bat file and put it in an sql file. so windows task calls the bat and the bat calls the sql. wjorks great.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply