In SQL Server 2005, Microsoft replaced the familiar command-line tool Osql with Sqlcmd. Both tools perform similar functions, with subtle differences. For example, each tool can connect to a SQL Server instance for direct queries or batch job initiation. And, for my project, they can both perform the invaluable service of listing all listening SQL Server machines on the network. One of Sqlcmd’s subtle differences is that you can use the command-line option /Lc rather than just /L to derive the server list:
- sqlcmd /Lc
The c is important in the command because it produces clean output in the server list. If you don’t use the c, the resulting list will contain a header (i.e., Servers:), and each server name will have leading spaces. This situation isn’t ideal for the next phase of my project, in which you load the command output into a table to subsequently use as input to the SSIS package. This package, in turn, will automatically connect to each of these servers to retrieve specific information. You use Sqlcmd solely to get this list of servers. By itself, the command is useful to gain the first level of understanding of the SQL Server machines on your network. However, at this point it reveals nothing more than a server or instance name.