SSMS:如何查找名称相似但数据类型不同的列

SSMS: How to find columns with similar names, but different data types

如何在 table 的预定义范围内查找名称相似但数据类型不同的列?

目标:验证项目中的列元数据

例如:列deal_id用了3table秒。

我想确保 deal_id 元数据到处都是一样的。

为了避免以下情况:

请试试这个(查询 returns information_schema.columns 具有相同名称但元数据不同的列):

;with
    t1 as       -- get tables with the same column names
    (   select *
            , count(*) over(partition by column_name) clm_cnt
        from information_schema.columns        
        where TABLE_NAME like '%'  -- specify scope of your tables  here, otherwise it will look within entire DB
    ),
    t2 as  -- get flags - if amount of distinct types/lengths/etc are >1
    (   select *
            ,cast(clm_cnt - count(*) over(partition by column_name,COLUMN_DEFAULT          ) as bit) is_diff_COLUMN_DEFAULT            
            ,cast(clm_cnt - count(*) over(partition by column_name,IS_NULLABLE             ) as bit) is_diff_IS_NULLABLE             
            ,cast(clm_cnt - count(*) over(partition by column_name,DATA_TYPE               ) as bit) is_diff_DATA_TYPE               
            ,cast(clm_cnt - count(*) over(partition by column_name,CHARACTER_MAXIMUM_LENGTH) as bit) is_diff_CHARACTER_MAXIMUM_LENGTH
            ,cast(clm_cnt - count(*) over(partition by column_name,CHARACTER_OCTET_LENGTH  ) as bit) is_diff_CHARACTER_OCTET_LENGTH  
            ,cast(clm_cnt - count(*) over(partition by column_name,NUMERIC_PRECISION       ) as bit) is_diff_NUMERIC_PRECISION       
            ,cast(clm_cnt - count(*) over(partition by column_name,NUMERIC_PRECISION_RADIX ) as bit) is_diff_NUMERIC_PRECISION_RADIX 
            ,cast(clm_cnt - count(*) over(partition by column_name,NUMERIC_SCALE           ) as bit) is_diff_NUMERIC_SCALE           
            ,cast(clm_cnt - count(*) over(partition by column_name,DATETIME_PRECISION      ) as bit) is_diff_DATETIME_PRECISION      
            ,cast(clm_cnt - count(*) over(partition by column_name,COLLATION_NAME          ) as bit) is_diff_COLLATION_NAME          
        from t1
        where clm_cnt>1
    )
select * 
from t2 
where 1=1
and 
(   0
    --| is_diff_COLUMN_DEFAULT   
    --| is_diff_IS_NULLABLE       
    | is_diff_DATA_TYPE                    
    | is_diff_CHARACTER_MAXIMUM_LENGTH 
    | is_diff_CHARACTER_OCTET_LENGTH   
    | is_diff_NUMERIC_PRECISION        
    | is_diff_NUMERIC_PRECISION_RADIX  
    | is_diff_NUMERIC_SCALE            
    | is_diff_DATETIME_PRECISION       
    | is_diff_COLLATION_NAME          
)>0 

您可以在information_schema.column上查询不同的列名。对于您突出显示的属性:

select column_name
from information_schema.columns c
group by column_name
having min(data_type) <> max(data_type) or
       coalesce(min(CHARACTER_MAXIMUM_LENGTH), -1) <> coalesce(max(CHARACTER_MAXIMUM_LENGTH), -1) or
       coalesce(min(NUMERIC_PRECISION), -1) <> coalesce(max(NUMERIC_PRECISION), -1) or
       coalesce(min(NUMERIC_PRECISION_RADIX), -1) <> coalesce(max(NUMERIC_PRECISION_RADIX), -1) or
       coalesce(min(NUMERIC_SCALE), -1) <> coalesce(max(NUMERIC_SCALE), -1) or
       coalesce(min(DATETIME_PRECISION), -1) <> coalesce(max(DATETIME_PRECISION), -1) ;

您可能还想包括:

  • CHARACTER_SET_NAME
  • COLLATION_NAME

但是,如果您始终使用默认字符集和排序规则,这可能不适用于您的数据库。

为空性可能不相关。