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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:01:04+00:00 2026-06-02T04:01:04+00:00

I have successfully written code to dynamically union the results of a simple select

  • 0

I have successfully written code to dynamically union the results of a simple select statement across multiple databases. I would like to include the database name itself as a field so that I can identify each record. What is the best way to modify the code below to accomplish that?

My current results look like

field2, field3, field4
b,c,d
2,3,4

I can’t tell what database the row containing (b,c,d) comes from.

And I would like to make sure I see
field1, field2, field3, field4
First_DatabaseName, b,c,d
Second_DatabaseName,2,3,4

And above, I could then see that the row containing (b,c,d) comes from First_Database

DECLARE @sql varchar(max)

SELECT @sql = ISNULL(@sql + 'union all ',' ') + ' SELECT * FROM ' + name + '.dbo.CombinedProvider '
FROM   sys.databases
WHERE  name in ('First_DatabaseName', 'Second_DatabaseName')

EXEC (@sql)
  • 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-02T04:01:05+00:00Added an answer on June 2, 2026 at 4:01 am

    Just add the name into the subquery that you are using:

    SELECT @sql = COALESCE(@sql + 'union all ',' ') + ' SELECT '''+name+''' as dbname, c.* FROM ' + name + '.dbo.CombinedProvider c'
    FROM   sys.databases
    WHERE  name in ('First_DatabaseName', 'Second_DatabaseName')
    

    By the way, you are using an unsupported technique to do aggregate string concatenation. You might want to learn about “for xml path(”)” as an alternative technique. Check out http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/ for more information than you want to know on this subject.

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

Sidebar

Related Questions

I have written a pretty simple code to get the first result for any
I have successfully written a vhdl code to read and write a file. Basically
I have my program written in C++ and it is can be successfully compiled
I have written a google visualization treemap wrapper for GWT. I can successfully draw
I've written an entire app pretty successfully in Django but I have this nagging
I'm working with some SQL 2005 CLR code written in C#. We have recently
Using Script Task, I have written a code to create a folder and create
I have written the following code for inserting video bytes into the database DBAdapter.java
I have successfully constructed something similar to the following code in visual studio 2008:
I have written a code that does SSH in servers and runs a Java

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.