I finally managed to solve this problem that I had. I came up with a specific solution of what I was looking for. I decided to post here because I think this could help someone else who might need one day. :)
DECLARE @banco_nome nvarchar(MAX), @tabela_nome nvarchar(MAX)
DECLARE @banco_cursor CURSOR
DECLARE @sqlstatement nvarchar(MAX)
DECLARE @count_sql nvarchar(MAX)
DECLARE @total int
DECLARE @RegistrosFotograficos TABLE
(
DatabaseName nvarchar(max),
TableName nvarchar(max),
Total int
)
SET @banco_cursor = CURSOR FORWARD_ONLY FOR
SELECT name FROM sys.databases
OPEN @banco_cursor
FETCH NEXT FROM @banco_cursor INTO @banco_nome
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlstatement = 'DECLARE tabela_cursor CURSOR FORWARD_ONLY FOR SELECT TABLE_NAME FROM ' + @banco_nome + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ''BASE TABLE'' AND TABLE_NAME LIKE ''%ATTACH'' ORDER BY TABLE_NAME'
EXEC sp_executesql @sqlstatement
OPEN tabela_cursor
FETCH NEXT FROM tabela_cursor INTO @tabela_nome
WHILE @@FETCH_STATUS = 0
BEGIN
SET @count_sql = 'USE ' + @banco_nome + '; SELECT @total=COUNT(1) FROM ' + @tabela_nome;
EXECUTE sp_executesql @count_sql, N'@total int OUTPUT', @total=@total OUTPUT
INSERT INTO @RegistrosFotograficos (DatabaseName, TableName, Total) VALUES (@banco_nome, @tabela_nome, @total);
FETCH NEXT FROM tabela_cursor INTO @tabela_nome
END
CLOSE tabela_cursor;
DEALLOCATE tabela_cursor;
FETCH NEXT FROM @banco_cursor INTO @banco_nome
END
CLOSE @banco_cursor;
DEALLOCATE @banco_cursor;
SELECT * FROM @RegistrosFotograficos