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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:21:44+00:00 2026-05-27T07:21:44+00:00

I have 6 variables that is: @SourceDatabase, @SourceSchema, @SourceTable, @TargetDatabase, @TargetSchema, @TargetTable Im writing

  • 0

I have 6 variables that is: @SourceDatabase, @SourceSchema, @SourceTable, @TargetDatabase, @TargetSchema, @TargetTable

Im writing a query that should join values from INFORMATION_SCHEMA.COLUMNS.
Like this:

SELECT a.COLUMN_NAME, b.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS a
LEFT OUTER JOIN INFORMATION_SCHEMA b
ON b.TABLE_SCHEMA = @SourceSchema
AND b.TABLE_NAME = @SourceTable
AND b.COLUMN_NAME = a.COLUMN_NAME
WHERE a.TABLE_SCHEMA = @TargetSchema
AND a.TABLE_NAME = @TargetTable

However, after I introduced the database-attributes, to do comparison cross databases, i just noticed that i dont think i can use the same query without making it dynamic as a String and putting databasename in front of INFORMATION_SCHEMA. Like DECLARE @SQL nvarchar(254) = 'SELECT * FROM ' + @SourceDatabase + '.INFORMATION_SCHEMA ...'. etc etc

Since im not really a fan of doing dynamic sql, im wondering if you guys have any clever solutions to do what im trying to do without going dynamic?

  • 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-27T07:21:44+00:00Added an answer on May 27, 2026 at 7:21 am

    Dynamic SQL is certainly the easiest way to handle this kind of thing, but you are certain this is not an option?

    The only other alternative I can think of is to UNION ALL on all of your databases, with an additional db name column:

    SELECT 'db1' AS dbName, *
    FROM db1.sys.columns
    UNION ALL
    SELECT 'db2' AS dbName, *
    FROM db2.sys.columns
    

    The collation needs to be the same in both databases. But Dynamic SQL would be easier, and could be made to work for any database, rather than just those in your union list.

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

Sidebar

Related Questions

I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have a program that compares variables from two structs and sets a bit
I have several variables that I need to send from page to page... What
I have some jquery variables that attempt to get values of nearby elements however
Suppose we have two variables that we wish to build a model from: set.seed(10239)
I have two variables that should be compared: Double a = 1D; Double b
I have a few variables that are assigned the values of session variables (See
Basically I have some variables that I don't want to preinitialize: originalTime = None
I have a global variables that I am using as a default variable. Depending
I have two float variables that are equal, I retrieve them by PHP and

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.