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)
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.
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.
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