Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having quite a few (~500) tables with same name but in different schemas i.e.
1) DWH and 2)REP
the column lengths for the above tables should ideally be the same in both the schemas, however that is not the case.

How do I write a SQL query that will give all those columns in the above 500 tables for which there is a mismatch in the column lengths.

e.g. DWH.Table1 col1 varchar(30), col2 varchar(200) and REP.Table1 col1 varchar(40), col2 varchar(255)

What I have tried:

tried using information_schema.columns
Posted
Updated 4-Mar-22 18:24pm

1 solution

You can compare the schemas of tables using querying the dm_exec_describe_first_result_set dynamic management function:

CREATE DATABASE TESTDB
CREATE DATABASE TESTDB2
CREATE TABLE TESTDB.dbo.FirstComTable
( ID INT IDENTITY (1,1) PRIMARY KEY,
  FirstName VARCHAR (50),
  LastName VARCHAR (50),
  Address VARCHAR (500)
)
GO
CREATE TABLE TESTDB2.dbo.FirstComTable
( ID INT IDENTITY (1,1) PRIMARY KEY,
  FirstName VARCHAR (50),
  LastName VARCHAR (50),
  Address NVARCHAR (400)
)
GO
SELECT FT.name , ST.name  , 
FT.system_type_name  , ST.system_type_name ,
FT.max_length  , ST.max_length
FROM sys.dm_exec_describe_first_result_set (N'SELECT * FROM TESTDB.DBO.FirstComTable', NULL, 0) FT
LEFT OUTER JOIN  sys.dm_exec_describe_first_result_set (N'SELECT * FROM TESTDB2.DBO.FirstComTable', NULL, 0) ST
ON FT.Name =ST.Name


You can check the further compare methods through the link:
Different Ways to Compare SQL Server Tables Schema and Data - {coding}Sight[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900