I finally managed to solve this problem I had. I found a specific solution to what I was looking for.I decided to post it here, because I think it might help someone else who might need it someday. :)
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
Kelvin, I’ve made an issue of your question which I think has become more readable, could you tell me from my edition if this is what you want? I say it because if it’s not I’ll come back with the question the way it was!
– Marconi
Thanks Marconi is just what I wanted to ask :)
– Gladiador
What a problem in :)
– Marconi
I’m already days trying to find a solution and I can’t find a way out...
– Gladiador
Kevin, The @Rodolpho Sa response worked for me. I think that’s what you want
– Marconi