SQL Server: compare columns in two tables
NickName:Max Al Farakh Ask DateTime:2010-03-12T04:05:52

SQL Server: compare columns in two tables

I've recently done a migration from a really old version of some application to the current version and i faced some problems while migrating databases.

I need a query that could help me to compare columns in two tables. I mean not the data in rows, I need to compare the columns itself to figure out, what changes in table structure I've missed.

Copyright Notice:Content Author:「Max Al Farakh」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2428271/sql-server-compare-columns-in-two-tables

Answers
Gabriele Petrioli 2010-03-11T20:22:38

have a look at Red Gate SQL Compare\n\nOtherwise here is a start (for sql server)\n\nselect \n so.name as [table],\n sc.name as [column],\n sc.type, sc.length, sc.prec, sc.scale, sc.collation\nfrom \n sysobjects so\n inner join syscolumns sc ON so.id = sc.id\n\nwhere so.type='u'\n\norder by so.name, sc.colorder\n\n\nyou can have a look at the \n\n - INFORMATION_SCHEMA.TABLES\n - INFORMATION_SCHEMA.COLUMNS\n - INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS\n - INFORMATION_SCHEMA.TABLE_CONSTRAINTS\n - INFORMATION_SCHEMA.KEY_COLUMN_USAGE\n\n\ntables if you want to go deeper..\n\n[update] \n\nUsing the INFORMATION_SCHEMA tables\n\nSELECT\n [table].TABLE_NAME AS [Table_Name],\n [column].COLUMN_NAME AS [Column_Name],\n COLUMNPROPERTY(object_id([table].[TABLE_NAME]), [column].[COLUMN_NAME], 'IsIdentity') AS [identity],\n [column].DATA_TYPE AS [datatype],\n [column].CHARACTER_MAXIMUM_LENGTH AS [Character_Length],\n [column].NUMERIC_PRECISION AS Numeric_precision,\n [column].ORDINAL_POSITION AS [order],\n [column].COLUMN_DEFAULT AS [defaultvalue],\n [column].IS_NULLABLE AS [nullable]\nFROM \n INFORMATION_SCHEMA.TABLES [table] INNER JOIN \n INFORMATION_SCHEMA.COLUMNS [column] ON [table].TABLE_NAME = [column].TABLE_NAME\nWHERE\n [table].TABLE_TYPE = 'BASE TABLE'\n AND [table].TABLE_NAME <> 'sysdiagrams'\nORDER BY \n [table].TABLE_NAME ASC, \n [column].ORDINAL_POSITION ASC\n",


ScottS 2013-04-16T18:22:22

I’d really recommend you use third party comparison tool such as SQL Compare already mentioned above or ApexSQL Diff or basically any other tool on the market. \n\nEven though these are commercial tools you can get a free trial and get the job done if you don’t really need to do this daily. \n\nIf you really need to use SQL for this you can try really simple query like this and then build on top of this.\n\nselect T.name, C.*\nfrom sys.tables T\ninner join sys.columns C on T.object_id = C.object_id\nwhere T.name = 'table_name'\n",


Qcpbraca 2010-11-02T14:36:39

This works for me (had the same problem and just compiled my solution)\n\nDECLARE @TableOne VARCHAR(2048) = '',\n @TableTwo VARCHAR(2048) = ''\n\n-- In TableOne but not in TableTwo\nSELECT DISTINCT\n @TableOne AS [First table],\n '>>' AS Dir, --Direction\n @TableTwo AS [Second table],\n a.COLUMN_NAME,\n a.DATA_TYPE \n FROM INFORMATION_SCHEMA.COLUMNS a\n WHERE a.COLUMN_NAME NOT IN (SELECT COLUMN_NAME\n FROM INFORMATION_SCHEMA.COLUMNS b\n WHERE b.TABLE_NAME = @TableTwo)\n AND a.TABLE_NAME = @TableOne\nUNION ALL\n-- In TableTwo but not in TableOne\nSELECT DISTINCT\n @TableOne AS [First table],\n '<<' AS Dir, --Direction\n @TableTwo AS [Second table],\n a.COLUMN_NAME,\n a.DATA_TYPE \n FROM INFORMATION_SCHEMA.COLUMNS a\n WHERE a.COLUMN_NAME NOT IN (SELECT COLUMN_NAME\n FROM INFORMATION_SCHEMA.COLUMNS b\n WHERE b.TABLE_NAME = @TableOne)\n AND a.TABLE_NAME = @TableTwo\n ORDER BY Dir DESC, COLUMN_NAME ASC\n\n\njust set values for @TableOne and @TableTwo and run the script ;)",


garik 2010-03-11T20:15:29

Realy it is a big script. :)\n\nUse red gate sql compare. They offer you 14-day free trial \n\nIf you realy need script it can be a text and than you can compare both by using any text comparer.",


More about “SQL Server: compare columns in two tables” related questions

How to compare two columns in SQL server

I have two columns in SQL Server in two different tables. One column has 9.011, and other table columns has 9011. I need to remove the . and compare these two columns to see whether they are equa...

Show Detail

Swap columns from two sql server tables

I would like to know if there is anyway I can compare two columns in SQL Server. The two columns are located in two different tables. When the column 1's value is smaller than the column 2's valu...

Show Detail

compare two columns from two different tables in oracle sql for differences

Hi first time posting for SQL, I need to compare two different columns from two different tables in SQL. For example there is two tables and each have one column in them and I need to compare the...

Show Detail

SQL Server: compare columns in two tables

I've recently done a migration from a really old version of some application to the current version and i faced some problems while migrating databases. I need a query that could help me to compare

Show Detail

Compare two sql server tables

Can anyone suggest me how to compare two database tables in sql server and return the rows in the second table which are not in the first table. The primary key in both the tables is not the same. ...

Show Detail

Best way to compare contents of two tables in Teradata?

When you need to compare two tables to see what the differences are, are there any tools or shortcuts you use, or do you handcode the SQL to compare the two tables? Basically the core features of a

Show Detail

How to compare the columns of two tables, SQL Server?

I understand creating a join and comparing the values of specific columns from two tables. In this case, I am only interested in comparing the columns between two different tables, not the values. ...

Show Detail

SQL how to compare two columns from two different tables

I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns. I have two columns in table1 that I want to compare them with two columns in table2. Table 1 have column1 and

Show Detail

SQL Server - compare the results of two stored procedures that output multiple tables

So, similar to "SQL Server compare results of two queries that should be identical", I need to compare the output of two stored procedures to ensure the new version is generating equivalent output ...

Show Detail

SQL Server: how to compare two tables

I have problem with comparing two tables in SQL Server. I have first table [Table1] with text column where I store my content and second table [table2] with column of my keywords. And now I want to

Show Detail