Good day,
I have a script where I am pulling server names into a variable which is put into a while loop to form a sqlcmd statement in order to connect to a list of servers and run the query. If I simply Print the statement and run it in a cmd window it returns as expected, but when I run in the SSMS window the only thing that is returned is Output NULL. If I try to insert into a defined table I get the ever famous column names or number of columns do not match.
Here is an example of what I am doing:
Table created up here server list inserted into #ServerList here
DECLARE BakInfoCur CURSOR FOR
SELECT [Server Name] FROM #ServerList
OPEN BakInfoCur
FETCH NEXT FROM BakInfoCur INTO @ServerName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Command ='sqlcmd -S ' + @ServerName+' -E -W -h-1 -s "," -Q"SET NOCOUNT ON; Select '''+@ServerName+''' AS InstanceName ,d.[name] and more complex query stuff"'
PRINT @Command
INSERT INTO #ServerBackupInfo
(InstanceName,
Database_Name,
[Type],
[Recovery Model],
[Status],
Last_Backup,
[BackupSize(MB)],
Device_Type,
Physical_Path)
EXEC xp_cmdshell @Command
FETCH NEXT FROM BakInfoCur into @ServerName
END
CLOSE BakInfoCur
DEALLOCATE BakInfoCur