Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:03:19+00:00 2026-05-28T18:03:19+00:00

I am working on sql server monitoring product and i have database query that

  • 0

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.

  1. 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;
    }
    
  2. 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 same

    Declare @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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:03:21+00:00Added an answer on May 28, 2026 at 6:03 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in Sql Server 2005. I have an event log table that tracks
Working with SQL Server 2008 R2. I have a table of hierarchical data, with
I am working in SQL Server 2005. I have a table that lists stores
In the JDBC code, I have the following that is working with SQL Server:
I'm working with SQL Server 2008. Let's say I have these three tables: Product
I am working with SQL Server 2005 and I have trigger on a table
I have the following query working on sql server managemengt studio but it does
I'm working with sql server 2008r2 developer. I have a stored procedure that adds
For a variety of reasons the database that I'm working on (SQL Server 2005)
First of all, I'm working in SQL Server 2000 (although prayers have been made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.