IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_table_compare_actually_design]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_table_compare_actually_design]
GO
--[mps_describe_table_with_filter] 'order_40','tp.name in (''int'',''bigint'')'

--======================================================
--张小猛 2018118
--======================================================
CREATE proc [dbo].[mps_table_compare_actually_design](
@actuallytabname varchar(100),
@filedinfotablename varchar(100)='',
@filter varchar(100) ='' )
as
begin

--[mps_table_compare_actually_design] 'order_40','','filedtype<>fldi_datatype'
if(@filedinfotablename='') 
set @filedinfotablename = @actuallytabname

declare @sql varchar(max) = ''
exec drop_temp_table '#comparetable'

create table #comparetable(object_id varchar(100),tablename varchar(100),filedname varchar(100),filedtype varchar(100),maxlen int ,is_computed int ,precision int,scale int ,collation_name varchar(100),is_nulable int ,definition varchar(100),is_identity int ,seed_value int ,increment_value int )


set @sql = 
N'
insert into #comparetable(object_id ,tablename ,filedname ,filedtype ,maxlen  ,is_computed  ,precision ,scale  ,collation_name ,is_nulable  ,definition ,is_identity  ,seed_value  ,increment_value  )
select
        convert(varchar(100),c.object_id),
        o.name,
        c.name,
        tp.name as [tp.name>datatype],
        convert(int,c.max_length),
        convert(int,c.is_computed),
        convert(int,c.precision),
        c.scale as scale,
        c.collation_name,
        convert(int,c.is_nullable),
        dc.definition,
        convert(int,ic.is_identity),
        convert(int,ic.seed_value),
        convert(int,ic.increment_value)
    from
        sys.columns c
        join sys.types tp on c.user_type_id = tp.user_type_id
        left join sys.computed_columns cc on c.object_id = cc.object_id and c.column_id = cc.column_id
        left join sys.default_constraints dc on
            c.default_object_id != 0
            and c.object_id = dc.parent_object_id
            and c.column_id = dc.parent_column_id
        left join sys.identity_columns ic on c.is_identity = 1 and c.object_id = ic.object_id and c.column_id = ic.column_id
        left join sys.objects o on o.object_id = c.object_id 
    where o.name = '''+@actuallytabname+''''
   
    +' order by
        ic.is_identity desc,
        c.name
        
   
  select * from mps_fieldinfo where fldi_tbname = '''+@actuallytabname+''''+CHAR(10)+

  N'select * from #comparetable
      
  select fldi_tbname,filedname,filedtype,fldi_datatype,* from #comparetable,mps_fieldinfo 
  where filedname = fldi_fieldname 
  and fldi_tbname = '''+@filedinfotablename++''''+CHAR(10)+
  N'and tablename = '''+@actuallytabname+''''+CHAR(10)+
  case when  @filter='' then '' else  ' and '+@filter  end+CHAR(10)
  /*+'and (charindex(filedtype,fldi_datatype) <0 and  charindex(fldi_datatype,filedtype)<0) '*/
  print @sql 
  
  execute(@sql)

        
end