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 9098543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:23:08+00:00 2026-06-17T00:23:08+00:00

I have many databases (more than 50) on SQL Server. All databases are identical.

  • 0

I have many databases (more than 50) on SQL Server. All databases are identical. Each database has a table named DBVersion. Each DBVersion has a column: “Version” which is of type varchar. In this table, always one record exists.

How can I loop through all the databases, select the “Version” from the table DBVersion and print in Microsft SQL Server Management studio the results?

The result should look like:

  1. Database1 – Version: 5
  2. Database2 – Version: 8
  3. Database50 – Version: 6

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-06-17T00:23:09+00:00Added an answer on June 17, 2026 at 12:23 am

    You can use sp_msforeachdb to itereate databases if they are on the same instance of SQL-Server

    CREATE TABLE #tmp (DatabaseName VARCHAR(50), Version VARCHAR(200));
    
    EXECUTE master.sys.sp_MSforeachdb '
                                    USE [?]; 
                                    IF EXISTS 
                                        (   SELECT  1
                                            FROM    sys.tables
                                            WHERE   [Object_ID] = OBJECT_ID(N''dbo.DBVersion'')
                                        )
                                        BEGIN
                                            INSERT #tmp (DatabaseName, Version) 
                                            SELECT ''?'', [Version] 
                                            FROM    dbo.DBVersion
                                        END';
    
    SELECT  *
    FROM    #tmp;
    
    DROP TABLE #tmp;
    

    There is some sceptisism about using undocumented procedures, so you could rewrite this with a cursor:

    CREATE TABLE #tmp (DatabaseName VARCHAR(50), Version VARCHAR(200));
    
    DECLARE DBCursor CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY
    FOR
        SELECT  Name
        FROM    sys.databases;
    OPEN DBCursor;
    
    DECLARE @DBName VARCHAR(200) = '';
    FETCH NEXT FROM DBCursor INTO @DBName;
    
    WHILE @@FETCH_STATUS != 0
        BEGIN
            DECLARE @SQL NVARCHAR(MAX) = N'USE ' + QUOTENAME(@DBName) + '
                                            IF EXISTS 
                                                (   SELECT  1
                                                    FROM    sys.tables
                                                    WHERE   [Object_ID] = OBJECT_ID(N''dbo.DBVersion'')
                                                )
                                                BEGIN
                                                    INSERT #tmp (DatabaseName, Version) 
                                                    SELECT @DB, [Version] 
                                                    FROM    dbo.DBVersion
                                                END';
            EXECUTE SP_EXECUTESQL @SQL, N'@DB VARCHAR(200)', @DBName;
    
            FETCH NEXT FROM DBCursor INTO @DBName;
        END
    
    CLOSE DBCursor;
    DEALLOCATE DBCursor;
    
    SELECT  *
    FROM    #tmp;
    
    DROP TABLE #tmp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql file generated by mysqldump --all-databases . There are many databases
I have a database with many tables and one particular table has columns: name
Im using c# .net windows form application. I have many databases created using sql
We are upgrading/converting several old Access databases to MS-SQL. Many of these databases have
I have a database on SQL server 2008 express with aprox 30 tables and
We have a SQL Server 2005 database for which we want to improve performance
We have an SQL Server 2008 R2 database that is attached to our web
I have one database that has a bigger size than expected, how can I
I have many databases that uses various column-names for the same things. For example,
Im using c# .net windows application form. I have created many databases with many

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.