comparing two tables in sql code example
Example 1: sql compare tables for differences
select isnull(db1.table_name, db2.table_name) as [table],
isnull(db1.column_name, db2.column_name) as [column],
db1.column_name as scripts_col_name
,db1.column_type as scripts_col_type
,db1.column_precision as scripts_col_precision
,db2.column_name as liquid_col_name
,db2.column_type as liquid_col_type
,db2.column_precision as liquid_col_precision
from
(select schema_name(tab.schema_id) + '.' + tab.name as table_name,
col.name as column_name
,y.name as column_type
,y.precision as column_precision
from [DB_NBR_1].sys.tables as tab
inner join [DB_NBR_1].sys.columns as col
on tab.object_id = col.object_id
JOIN sys.types y ON y.user_type_id = col.user_type_id
) db1
full outer join
(select schema_name(tab.schema_id) + '.' + tab.name as table_name,
col.name as column_name
,y.name as column_type
,y.precision as column_precision
from [DB_NBR_2].sys.tables as tab
inner join [DB_NBR_2].sys.columns as col
on tab.object_id = col.object_id
JOIN sys.types y ON y.user_type_id = col.user_type_id
) db2
on db1.table_name = db2.table_name
and db1.column_name = db2.column_name
where (db1.column_name is null or db2.column_name is null)
order by 1, 2, 3
Example 2: sql difference between tables
SELECT *
FROM Table1
MINUS
SELECT *
FROM Table2;
SELECT *
FROM <table-1>
MINUS
SELECT *
FROM <table-2>;
Example 3: sql compare data from two tables
select * from tableA
minus
select * from tableB