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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:46:09+00:00 2026-05-17T23:46:09+00:00

I have 2 databases namely db1,db2. I need a query that fetch the data

  • 0

I have 2 databases namely db1,db2. I need a query that fetch the data from these dbs(db1,db2) which have inturn 2 tables(concessions,invoicing) each.

In db1.concessions => concession is primary key.
db1.invoicing => [Concession Number] is primary key

similarly in db2.concessions => concession is primary key.
db2.invoicing => [Concession Number] is primary key

In database1

db1.tbl1 => Concessions table has data

    concession
    TH-123
    TH-456
    FP-789
    NZ-609

db1.tbl2 => invoicing table has data

    [Concession Number]          invoiced_on
    TH-322                        10.09.10
    TH-900                        23.10.10
    FP-675                        04.05.09
    NZ-111                        19.11.08

luckily, in a database the value of concession in unique. i.e concessions.[concession] = invoicing.[concession Number] yields no data..

In database2:

db1.tbl1 => Concessions table has data

    concession
    TH-123
    FP-789
    NZ-999
    TH-900

db1.tbl2 => invoicing table has data

    [Concession Number]          invoiced_on(dd.mm.yy)
    TH-456                        18.01.06
    TH-777                        23.10.04
    FP-675                        03.05.09
    NZ-149                        26.11.08

HEre in db2 concession is unique, concessions.[concession] = invoicing.[concession Number] yields no data..

Now the query should fetch the records that have common
db1.(concessions.concession OR invoicing.concession number) = db2(concessions.concession OR invoicing.concession number)

In the sample data it should return, TH-123,FP-789,NZ-999, FP-675.

My 2nd question is there is possibility of extending this query to multiple database. I can’t change the count of databases to 1 as they are already fixed. Please let me know the best procedure for the same.

I tried something like this, there are syntax errors,

SELECT a.concession as db1_CON_NUMBER FROM db1.dbo.concessions as a  UNION 
SELECT b.[Concession Number] as db1_CON_NUMBER  FROM db1.dbo.invoicing as b 
INNER JOIN 
SELECT c.concession as db2_CON_NUMBER FROM db2.dbo.concessions as c  UNION 
SELECT d.[Concession Number] as db2_CON_NUMBER  FROM db2.dbo.invoicing as d 
ON db1_CON_NUMBER = db2_CON_NUMBER 

Hope you will answer both the questions.
Thanks for your patience in reading such a long mail!

  • 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-17T23:46:10+00:00Added an answer on May 17, 2026 at 11:46 pm

    You can reference other databases directly if the user has permissions.

    <database>.<user>.<tablename>
    

    Is the full “path” to the database table.

    Often you use

    db1.dbo.tbl1 join db2.dbo.tbl2
    

    where dbo is default for database owner, any table not owned by a specific user is owned by dbo by default.

    UPDATE

    To get the query to validate you can expand it to this

    SELECT * FROM 
    (SELECT a.concession as db1_CON_NUMBER FROM db1.dbo.concessions as a  
    UNION 
    SELECT b.[Concession Number] as db1_CON_NUMBER FROM db1.dbo.invoicing as b ) c
    
    INNER JOIN 
    
    (SELECT c.concession as db2_CON_NUMBER FROM db2.dbo.concessions as a 
    UNION 
    SELECT b.[Concession Number] as db2_CON_NUMBER FROM db2.dbo.invoicing as b ) d
    
    ON db1_CON_NUMBER = db2_CON_NUMBER 
    

    But I have not had time to check if this would return the right data but you can test.

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

Sidebar

Related Questions

We have 18 databases that should have identical schemas, but don't. In certain scenarios,
I have 4 databases with similar schema's, and I'm trying to create a query
I have some databases that have four files each; one for PRIMARY, IDX, IMAGE,
All of us who work with relational databases have learned (or are learning) that
I am using SQL Server 2000 and I have two databases that both replicate
I have databases with different collations. I want to be able to script out
I have several databases where the transaction log (.LDF) is many times larger than
I have two databases, one is an MS Access file, the other is a
I have 2 databases, and I want to transport an existing table containing a
I have a databases table with ~50K rows in it, each row represents a

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.