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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:04:06+00:00 2026-05-31T10:04:06+00:00

I am stuck with a SQL query. How do I get all the databases

  • 0

I am stuck with a SQL query. How do I get all the databases names from multiple SQL Servers at a time? I have the server IP’s in a table.

  • 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-31T10:04:07+00:00Added an answer on May 31, 2026 at 10:04 am

    This gives all the databases of the current server

    EXEC sp_databases
    

    If you know the other server ips you could use sp_addlinkedserver to link them and get the desired results.

    EDIT: following is a complete answer for you as I described above but using sys.databases

    DECLARE @T AS TABLE (dbName VARCHAR(250),ipAddress VARCHAR(20))
    DECLARE @SERVERS AS TABLE (Id INT IDENTITY(1,1),serverIp VARCHAR(20),
                               rmUser VARCHAR(20),rmPassword VARCHAR(20))
    DECLARE @ipAdd AS VARCHAR(20),@user AS VARCHAR(20), @pw AS VARCHAR(20),@rowCount AS INT
    
    SELECT @rowCount = 1
    
    --ADD THE LIST OF SERVERS WITH LOGIN USER NAME AND PASSWORDS HERE
    INSERT INTO @SERVERS (serverIp,rmUser,rmPassword) 
        VALUES ('serverIp1','rmuser1','rmpassword1'), ('serverIp2','rmuser2','rmpassword2')
    
    WHILE EXISTS(SELECT Id FROM @SERVERS WHERE Id = @rowCount)
    BEGIN
    SELECT @ipAdd = serverIp, @user= rmUser, @pw = rmPassword 
        FROM @SERVERS WHERE Id =  @rowCount
    
        --Link the server and add login
    EXEC sp_addlinkedserver @ipAdd,N'SQL Server';
    EXEC sp_addlinkedsrvlogin @rmtsrvname=@ipAdd, 
                              @useself='false',
                              @rmtuser=@user,
                              @rmtpassword=@pw
    
         --INSERT results into a temp view and then into @T
     EXEC('CREATE VIEW vTemp AS SELECT name FROM ['+@ipAdd+'].master.sys.databases')
     INSERT INTO @T  SELECT name,@ipAdd FROM vTemp
    
         --Drop view
     DROP VIEW vTemp
    
         --Drop login and link to the remort server
     EXEC sp_droplinkedsrvlogin @ipAdd, NULL
     EXEC sp_dropserver @ipAdd, NULL;
    
         SELECT @rowCount = @rowCount +1
    
    END
    
    --FINALLY YOUR RESULTS
    SELECT * FROM @T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to find friends of friends (not my friends) from an sql table
I'm stuck on a SQL query. Consider the following table: Table DG_GAME_ROUNDS RoundId int
I have an SSIS package that exports data from a table on a SQL
I have a SQL query that takes a very long time to run on
I'm stuck on a MDX query... I have a table which looks like: Date
I am stuck figuring out a working SQL Query for the fallowing: I need
I've been stuck with this simple Sql query for last 3 hours and my
I'm a little stuck with an sql query. I've searched everywhere but to no
I have set up transactional replication between two SQL Servers on different ends of
I have a class that contains all of my SQL objects, including the methods

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.