Search code examples
sqlsql-server-2008

SQL script using SQLCMD for different physical servers


I am just trying to script some basic stuff using SQLCMD from query editor.

I wanted to execute the same sql statements but in different sql servers and want to see the output. This has nothing to do with performance but the idea is to just check the data in different servers that we migrated. I just learnt SQLCMD would be very helpful here but wonder how to script this. Just assume same db and tables exist in all servers. So far, I do the below to get what I need and I am sure this could be done a lot more nicely !

:Connect Server1
SELECT count (*) FROM [DB].[dbo].[Table1]
SELECT count (*) FROM [DB].[dbo].[Table2]

:Connect Server2
SELECT count (*) FROM [DB].[dbo].[Table1]
SELECT count (*) FROM [DB].[dbo].[Table2]

:Connect Server3
SELECT count (*) FROM [DB].[dbo].[Table1]
SELECT count (*) FROM [DB].[dbo].[Table2]

Thank you !


Solution

  • That solution is probably the best if you don't want to use a batch file

    If you are willing to add a batch file to the mix, you can do something like what is shown here Batch File

    You can do this inside SSMS by creating a Server Group. MSDN