I am working on sql server monitoring product and i have database query that will fetch data regarding All Table details of all the Databases in SQL server.
For this i have two options.
-
Fire query on data base from code as
select name from [master].sys.sysdatabases
Get the DB name of all the data base first then i will fire my main query on each DB
using"USE <fetched DB name>;"+"mainQuery";Please check followin code for the same.
public DataTable GetResultsOfAllDB(string query) { SqlConnection con = new SqlConnection(_ConnectionString); string locleQuery = "select name from [master].sys.sysdatabases"; DataTable dtResult = new DataTable("Result"); SqlCommand cmdData = new SqlCommand(locleQuery, con); cmdData.CommandTimeout = 0; SqlDataAdapter adapter = new SqlDataAdapter(cmdData); DataTable dtDataBases = new DataTable("DataBase"); adapter.Fill(dtDataBases); foreach (DataRow drDB in dtDataBases.Rows) { if (dtResult.Rows.Count >= 15000) break; locleQuery = " Use [" + Convert.ToString(drDB[0]) + "]; " + query; cmdData = new SqlCommand(locleQuery, con); adapter = new SqlDataAdapter(cmdData); DataTable dtTemp = new DataTable(); adapter.Fill(dtTemp); dtResult.Merge(dtTemp); } return dtResult; } -
I will use sys store procedure i.e.EXEC sp_MSforeachdb and fetched data will be stored store data in table datatype select from temptable; Drop Table temptable.
Check following query for the sameDeclare @TableDetail table
(
field1 varchar(500),
field2 int,
field3 varchar(500),
field4 varchar(500),
field5 decimal(18,2),
field6 decimal(18,2)
)
INSERT @TableDetail EXEC sp_MSforeachdb 'USE [?]; QYERY/COMMAND FOR ALL DATABASE'
Select
field1,field2 ,field3 ,field4 ,field5,field6 FROM @TableDetail
Note : In second option query takes time because if number of database and number of table are huge then this will wait until all database get finish.
Now my question is which is the good option from above two options and why? or any other solution for the same.
Thanks in advance.
One key difference is the second option blocks until everything is done. All of the work is done sql server side. That has the issue of not being able to apply feedback to the user as it runs and it can potentially time out and not be resiliant to network blips. This option can be used as a pure sql script (some sql admins like that) where the first needs a program.
In the first example, the client is doing iterative more granular tasks where you can supply feedback to the user. You can also retry in the face of network blips without redoing all of the work. In the first example, you can also use SqlConnectionBuild instead of USE concatentation.
If performance is a concern, you could also potentially parallelize the first one with some locking around adapter.Fill