Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NOT EXISTS 2

Status
Not open for further replies.

dbomrrsm

Programmer
Feb 20, 2004
1,709
GB
I have two tables in different databases that are exactly the same in structure.

I have added an additional column called test to tableA.

When I run the below code the NOT IN finds the difference between the tables (i.e. the additional column called test) but the NOT EXISTS doesn't - can anyone tell me why ?

Code:
select *
From 	dev.Information_Schema.Columns
Where	Table_Name = 'tableA'
and column_name not in
(select column_name
From 	smart.Information_Schema.Columns
Where	Table_Name = 'tableA')

select *
From 	dev.Information_Schema.Columns
Where	Table_Name = 'tableA'
and not exists
(select *
From 	smart.Information_Schema.Columns
Where	Table_Name = 'tableA')

[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
Exists simply checks for the presence of a record. As soon as it finds 1 record, it returns true. Under the right circumstances, Exists is very efficient, but it is not appropriate for this query.

Code:
select *
From     dev.Information_Schema.Columns
Where    Table_Name = 'tableA'
and not exists
([blue]select *
From     smart.Information_Schema.Columns
Where    Table_Name = 'tableA'[/blue])

So... the part in blue would return all the schema information for the existing columns in the tableA. Since there is at least 1 field already in the table, the query would return at least 1 row, so exists = true. You're using not exists, so False, and therefore no records are returned.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George

Thanks for the clarification - very well explained.

[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
your not exists should be coded like this (untested)

Code:
select *
From     dev.Information_Schema.Columns d
Where    d.Table_Name = 'tableA'
and not exists
(select *
From     smart.Information_Schema.Columns s
Where    s.Table_Name = 'tableA'
and s.column_name = d.column_name)

Denis The SQL Menace
SQL blog:
 
SQLDenis

Excellent - thats got it - works a treat.

What I wanted to use this for was to see if there were any differences between the structure of one table and another.

To do this I would presumably need to place other and conditions in the not exists select to check for differences in other columns of the information_schema.columns.

[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top