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

  • SEARCH
  • Home
  • 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 7947553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:27:54+00:00 2026-06-04T01:27:54+00:00

I have 10 identical databases. I get the database names at runtime. I want

  • 0

I have 10 identical databases.

I get the database names at runtime.

I want to store rows into a collection of objects.

I also only want one hit on the database server.

My current approach:-

  • In a query (no stored procedures for X reason) I get list of databases and store in a temporary table.
  • Then I iterate through each database and create a dynamic query and execute it.

    DECLARE @MaxRownum int SET @MaxRownum = (SELECT MAX(RowNum) FROM #Databases) 
    DECLARE @Iter int SET @Iter = 1
    WHILE @Iter <= @MaxRownum
    BEGIN
    DECLARE @Database varchar(255) SELECT @Database = Databases FROM #Databases 
          WHERE RowNum = @Iter
    IF HAS_DBACCESS(@Database) > 0
        BEGIN 
          //appending query
    END
    SET @Iter = @Iter + 1
    END
    EXEC(@Query)
    

Can I use Linq + entity framework with one hit to server, without dynamic query and without hampering the performance? Is there any better solution?

  • 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-06-04T01:27:56+00:00Added an answer on June 4, 2026 at 1:27 am

    Having no idea what your query is (I asked but you did not supply it), and not sure that you understand it is going to be extremely difficult to supply database names as variables without a “dynamic query”, here is a much simpler way to do it IMHO:

    DECLARE @sql NVARCHAR(MAX);
    
    SELECT @sql = N'';
    
    SELECT @sql = @sql + CHAR(13) + CHAR(10) + 'UNION ALL'
        --// you will need to fill in your "//appending query" stuff here:
        + ' SELECT ... FROM ' + QUOTENAME(Databases) + '.dbo.tablename'
     FROM #Databases
     WHERE HAS_DBACCESS(Databases) = 1;
    
    SET @sql = STUFF(@sql, 1, 9, '');
    
    EXEC sp_executesql @sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two websites that have an almost identical database schema. the only difference
I have two SQL Server databases with identical schemas, but different data. I also
I have 4 tables.There are columns in each table which have identical names. Each
I have two identical select boxes (aside from their names, of course), and I
I have a database with 500+ tables, each with identical structure, that contain historical
We have a situation where we have multiple databases with identical schema, but different
I have a movie database where movies are inserted into a table named titles
I am trying to create database tables and get them connected. I have ensured
I have 2 SharePoint lists that have identical structures. Is it possible for me
I have written two pieces of code which I intended to have identical outputs,

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.