SET NOCOUNT ON SET STATISTICS IO OFF CREATE TABLE #Temp(ExecSQL nvarchar(3500)); CREATE TABLE #Rs(RsSQL nvarchar(3500)); DECLARE @DBName nvarchar(255) , @SQL nvarchar(3500); SET @DBName = 'AdventureWorks'; SET @SQL = ' SELECT ''CREATE SYNONYM ['' + TABLE_SCHEMA + ''_'' + TABLE_NAME + ''] FOR ['' + TABLE_CATALOG + ''].['' + TABLE_SCHEMA + ''].['' + TABLE_NAME + '']'' FROM ' + @DBName + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA <> ''dbo'''; INSERT #Temp EXEC(@SQL); DECLARE Cur CURSOR FOR SELECT ExecSQL FROM #Temp OPEN cur; FETCH NEXT FROM cur INTO @SQL; WHILE @@FETCH_STATUS NOT IN (-1, -2) BEGIN --EXEC(@SQL); --PRINT @SQL; INSERT #Rs(RsSQL) VALUES(@SQL); FETCH NEXT FROM cur INTO @SQL; END SELECT * FROM #Rs DROP TABLE #Temp DROP TABLE #Rs CLOSE cur; DEALLOCATE cur;