I want to list all the databases of a given server Sql Server (2008 or higher) that contain a certain table. I have seen several examples from cursors, stored procedures that run through the server databases to the use of sp_msforeachdb, which in several places discourages. What would be the best way to do this? I put what I'm doing now:
Dim daBBDD As New SqlClient.SqlDataAdapter("SELECT QUOTENAME(name) Name FROM sys.databases WHERE database_id > 4 AND state = 0 ORDER BY name", CnxGestion)
Dim dtBBDD As New DataTable("DDBB")
daBBDD.Fill(dtBBDD)
Dim daTable As SqlClient.SqlDataAdapter
Dim dtTable As DataTable
For Each _bbdd As DataRow In dtBBDD.Rows
daTable = New SqlClient.SqlDataAdapter(String.Format("SELECT * FROM {0}.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TABLA_BUSCADA'", _bbdd(0)), CnxGestion)
dtTable = New DataTable(_bbdd(0).ToString)
daTable.Fill(dtTable)
If dtTable.Rows.Count > 0 Then
''Contiene la tabla
else
''No contiene la tabla
End If
Next