I want to create a maintenance database that i can clean up master and msdb, i am trying to collect all the table info from master and msdb that are not Microsoft tables, bearing in mind that some instances might have different structures due to different requirements. I am getting an error on the code
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near '@ServerName'
is there maybe a simpler way of doing this?
DECLARE servers_cursor CURSOR
FOR
-- select * from environment
-- select * from DatabaseInfo
select distinct LinkedserverName,Environment,ServerUse
from SQLMonitor.dbo.Environment
join master..sysservers on srvname COLLATE DATABASE_DEFAULT = LinkedserverName COLLATE DATABASE_DEFAULT
where ServerUse = 'DEV'
--and IncludeBackupsStats = 1
order by LinkedServerName
OPEN servers_cursor
DECLARE @Servername varchar(255)
DECLARE @sql varchar(8000)
DECLARE @Environment char(4)
DECLARE @ServerUse varchar(15)
FETCH NEXT FROM servers_cursor INTO @ServerName,@Environment,@ServerUse
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT @ServerName
CREATE TABLE [_TempDetails_]
(DatabaseName sysname
,TableName sysname
,ActualCommand nvarchar(max))
INSERT INTO [TempTableScripts].dbo._TempDetails_
SELECT * FROM OPENQUERY(@ServerName, '
declare @TableNames table
(
DatabaseName sysname
, TableName sysname
)
insert @TableNames
EXEC sp_msforeachdb @command1 = ''use ?;
IF ''''?'''' IN (''''master'''',''''msdb'''')
BEGIN
SELECT ''''?'''', name from sys.tables T WHERE is_ms_shipped = 0
END'+'
select *, ''''SELECT TOP 1 * INTO ['''' + @ServerName + ''''_-_'''' + Databasename +''''_-_'''' + TableName +''''] FROM ['''' + @ServerName+''''].[''''+ Databasename + ''''].dbo.['''' + TableName + '''']''''
from @TableNames
')
--exec (@sql)
FETCH NEXT FROM servers_cursor INTO @ServerName,@Environment,@ServerUse
--PRINT @ServerName
END
CLOSE servers_cursor
DEALLOCATE servers_cursor
Best Answer
The following code will inspect the master and msdb databases for all linked-servers on the current instance, reporting non-Microsoft-shipped tables:
Your code has two problems;
You cannot use a variable in
OPENQUERY
like this:That would need to be:
Clearly, that won't work for your case since you're trying to do this dynamically. To make that work, you'd need to do dynamic SQL inside dynamic SQL.
This line contains an embedded reference to the
@ServerName
variable, but that variable is not visible inside the dynamic T-SQL... That line would need to be: