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

The Archive Base Latest Questions

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

I have a pathological issue with SQL, so I usually sort all of my

  • 0

I have a pathological issue with SQL, so I usually sort all of my database issues by building quickie software applications to handle my SQL problems.

(as I am also doing in this case)

Thanks to StackOverflow I think I can be shamed into correctness, so I would like to learn how to make this kind of SQL troubleshooting in actual SQL or T-SQL itself (if possible):

I have:

database DB1 Table A (unitNo, BuildingNo)

database DB2 Table B (unitNo, BuildingNo)

  1. I want to come up with the Units (unit numbers) existing on Table A from Database DB1 which do not exist on table B from Database DB2 and vice versa.

  2. There can be more than one unit with the same unit number, this will happen because the same unit number can be given to units of different buildings.

  3. I do not have write access to any of the databases.

I hope this isn’t seen as a ‘gimme teh codz’ post, I would like to know how people with more SQL fluency than me sort this kind of algorithm, posts to tutorials or hints are more than welcomed, no full code required, but if it helps to makes sense, then please do.

At first I thought I could just get all the unit numbers from one table and exclude them on a select from the other like so:

select concated.unit from  ( SELECT (  unitNo + ',' + CONVERT(varchar(12), BuildingNo) ) as unit FROM A) concated  having concated.unit not in  ( '201,1', '202,1', '203,1', '204,1', '205,1', '206,1', [...] 

This would usually work, but the number of units from any one table is immense, trying this crashes the SQL server with:

‘ran out of stack space’

Thanks,

Ric

  • 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. 2026-05-11T07:48:44+00:00Added an answer on May 11, 2026 at 7:48 am

    I think you’re looking for full outer join. Which gives you the unit numbers you asked for in Part 1.

    Select     A.UnitNumber, B.UnitNumber from    DB1.dbo.TableA A FULL OUTER JOIN DB2.dbo.TableB B         on A.UnitNumber = B.UnitNumber        and A.BuildingNumber = B.BuildingNumber Where    A.UnitNumber is null or B.UnitNumber is null 

    Other queries that might be of interest I’ve outlined below.

    This gives you the records in A not in B.

    Select     A.UnitNumber From     DB1.dbo.TableA A Left Join DB2.dbo.TableB B          on A.UnitNumber = B.UnitNumber          and A.BuildingNumber = B.BuildingNumber Where     B.UnitNumber is null 

    And you just reverse it to find the records in B that aren’t in A.

    Select     B.UnitNumber From     DB2.dbo.TableB B left join DB1.dbo.TableA A          on B.UnitNumber = A.UnitNumber         and B.BuildingNumber = A.BuildingNumber Where     A.UnitNumber is null 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an issue with marshall and unmarshall readers and writers. So here it is.
Have searched the database but need to specifically sum(of hours flown or days off)in
Have a rather abstract question for you all. I'm looking at getting involved in
Have an issue with jquery autocomplete, focus function After i type something in the
Have some dates in my local Oracle 11g database that are in this format:
I have been told that Singletons are hard to test. http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/ I have
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you experienced Play applications hanging when you run them under JDK 1.7? When
Have their been any studies related to the importance of how good a software
Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()

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.