SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On



How to Compare columns names of Two tables that reside in the different database... Expand / Collapse
Author
Message
Posted Monday, December 29, 2008 2:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, January 04, 2009 9:50 AM
Points: 2, Visits: 22
Hi,

I want to compare column names of the Two table that reside in the Different database.

Means I have one Table "TableA" in the database "DB1" & I have another table "TableB" in the another database "DB2". I want to comapre Columns of TableA with the TableB.

Can you please give what query will work for this?

Thanks
Post #626446
Posted Monday, December 29, 2008 3:26 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, June 30, 2009 11:45 PM
Points: 1,713, Visits: 1,364
One of the diff ways

-- to find out columns of DB1.TableA which are THERE IN DB2.TableA

select * from DB1.information_schema.columns A
WHERE column_name = SOME (SELECT column_name from DB2.information_schema.columns B WHERE B.table_name = 'TableA')
AND A.table_name = 'TableA'

-- to find out columns of DB1.TableA NOT THERE IN DB2.TableA

select * from DB1.information_schema.columns A
WHERE column_name <> ALL (SELECT column_name from DB2.information_schema.columns B WHERE B.table_name = 'TableA')
AND A.table_name = 'TableA'


"Keep Trying"
Post #626453
Posted Monday, December 29, 2008 3:37 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, June 25, 2009 12:17 AM
Points: 687, Visits: 94
You can try something like this:

SELECT * FROM (
SELECT * FROM DB1.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='TableA'
) a FULL OUTER JOIN (
SELECT * FROM DB2.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='TableA'
) b ON a.COLUMN_NAME=b.COLUMN_NAME
WHERE a.COLUMN_NAME IS NULL OR b.COLUMN_NAME IS NULL
OR a.DATA_TYPE<>b.DATA_TYPE OR a.IS_NULLABLE<>b.IS_NULLABLE
OR a.CHARACTER_MAXIMUM_LENGTH<>b.CHARACTER_MAXIMUM_LENGTH
OR a.NUMERIC_PRECISION<>b.NUMERIC_PRECISION OR a.NUMERIC_SCALE<>b.NUMERIC_SCALE
OR a.COLLATION_NAME<>b.COLLATION_NAME -- and maybe some other columns

Razvan Socol
SQL Server MVP



Post #626457
Posted Monday, December 29, 2008 3:39 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, June 22, 2009 5:25 AM
Points: 61, Visits: 81
Download the Redgate Toolbelt trial - the SQLCompare tool should solve your problem.


Post #626458
Posted Monday, December 29, 2008 11:56 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, January 04, 2009 9:50 AM
Points: 2, Visits: 22
Thanks you very much.
Query works. :)
Post #627043
« Prev Topic | Next Topic »


Permissions Expand / Collapse