IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[comp_database]') AND type = 'P')
DROP PROCEDURE [dbo].[comp_database]
GO

CREATE proc [dbo].[comp_database]
@old varchar(100),
@new varchar(100)
as
begin
--ebs中所有的表的结构
create table #a(tnm varchar(100),cnm varchar(100),tp varchar(50),lth int,prec int,scale int,cd varchar(50))
--ebs——new中所有的表的结构
create table #b(tnm varchar(100),cnm varchar(100),tp varchar(50),lth int,prec int,scale int,cd varchar(50))
--被修改字段存入#c
create table #c(tnm varchar(100),cnm varchar(100),tp varchar(50),lth int,prec int,scale int)
--新增加的表
create table #d(tnm varchar(100))
--新增的字段存入#e
create table #e(tnm varchar(100),cnm varchar(100),tp varchar(50),lth int,prec int,scale int,cd varchar(50))
create table #f(t_sql varchar(4000)) --新库中表新增字段 插入到旧库中相应的表
create table #g(t_sql varchar(4000)) --将旧库中表被修改的字段 修改成与新表中的相同
create table #h(tnm varchar(100),cnm varchar(100),txt varchar(50)) --
create table #i(tnm varchar(100),cnm varchar(100),txt varchar(50)) --
create table #j(t_sql varchar(4000)) --修改旧表默认值
create table #k(t_sql varchar(4000)) --修改新表默认值
create table #m(t_sql varchar(4000)) --修改新增字段默认值
create table #l(tnm varchar(100),cnm varchar(100),txt varchar(50)) --


exec('use '+@old+' insert into #h select c.name,a.name,b.text from syscolumns a,syscomments b,sysobjects c where a.cdefault=b.id and a.id=c.id  and  c.name not like ''order%'' order by c.name')
exec('use '+@new+' insert into #i select c.name,a.name,b.text from syscolumns a,syscomments b,sysobjects c where a.cdefault=b.id and a.id=c.id  and  c.name not like ''order%'' order by c.name')

exec('use '+@old+' insert into #a select a.name,b.name,c.name,b.prec,b.xprec,b.xscale,b.cdefault from sysobjects a,syscolumns b,systypes c where a.id=b.id and  b.xtype=c.xtype and a.type=''U'' and  a.name not like ''order%''  and  a.name not like ''dump_order%'' ')
exec('use '+@new+' insert into #b select a.name,b.name,c.name,b.prec,b.xprec,b.xscale,b.cdefault from sysobjects a,syscolumns b,systypes c where a.id=b.id and  b.xtype=c.xtype and a.type=''U'' and  a.name not like ''order%''  and  a.name not like ''dump_order%'' ')

declare @tname varchar(100)
declare @cname varchar(100)
declare @type  varchar(50)
declare @txt   varchar(50)
declare @sql   varchar(4000)
declare @default varchar(50)
declare @length int
declare @prec   int
declare @scale  int
declare @isnull int

    declare add_table cursor local forward_only read_only
    for 
        select tnm from #b
    open add_table
    fetch next from add_table into @tname 
    while @@fetch_status = 0
    begin
       if not exists(select * from #a where tnm=@tname)
       begin
            insert into #d(tnm) values(@tname)
       end
    fetch next from add_table into @tname
    end
    close add_table
    deallocate add_table
select distinct tnm '新增的表' from #d order by tnm  --新增的表


    declare add_column cursor local forward_only read_only
    for 
        select tnm,cnm,tp,lth,prec,scale,cd from #b
    open add_column
    fetch next from add_column into @tname,@cname,@type,@length,@prec,@scale,@default
    while @@fetch_status = 0
    begin
       if not exists(select * from #a where tnm=@tname and cnm=@cname)
       begin
           if not exists(select * from #d where tnm=@tname)
           begin
                insert into #e(tnm,cnm,tp,lth,prec,scale,cd) values(@tname,@cname,@type,@length,@prec,@scale,@default)
           end
       end
    fetch next from add_column into @tname,@cname,@type,@length,@prec,@scale,@default
    end
    close add_column
    deallocate add_column
select tnm '新增的字段',cnm,tp,lth,prec,scale,cd from #e where tp<>'sysname' order by tnm , cnm --新增字段

--新增字段插入相应旧表
    declare insert_column cursor local forward_only read_only
    for
        select tnm,cnm,tp,lth,prec,scale from #e where tp<>'sysname'
    open insert_column
    fetch next from insert_column into @tname,@cname,@type,@length,@prec,@scale
    while @@fetch_status = 0
    begin
        if(@type='varchar' or @type='nvarchar' or @type='char' or @type='nchar' or @type='binary' or @type='varbinary')
        begin
            set @sql='alter table '+@tname+' add '+@cname+' '+@type+'('+cast(@length as varchar)+') '+CHAR(13)+CHAR(10) 
        end
        else if(@type='decimal' or @type='numeric')
        begin
            set @sql='alter table '+@tname+' add '+@cname+' '+@type+'('+cast(@length as varchar)+','+cast(@scale as varchar)+')' +CHAR(13)+CHAR(10)  
        end
        else
        begin
            set @sql='alter table '+@tname+' add '+@cname+' '+@type+CHAR(13)+CHAR(10)  
        end
        insert into #f(t_sql) values(@sql)
       
    fetch next from insert_column into @tname,@cname,@type,@length,@prec,@scale
    end
    close insert_column
    deallocate insert_column
select t_sql as [新增的字段的SQL语句] from #f --将新库中对应旧库中相同的表新增的字段 插入旧库中相应的表

exec('use '+@new+' insert into #l select tnm,cnm,c.text from #e a,syscomments c where a.cd=c.id ')
--select distinct t_sql as [t_sql将插入旧库的字段中 字段为null的改为该字段的默认值 需要先完成新增字段插入旧库表] from #l
       declare up_add_column cursor local forward_only read_only
       for
            select distinct tnm,cnm,txt from #l
       open up_add_column
       fetch next from up_add_column into @tname,@cname,@txt
       while @@fetch_status = 0
       begin
        -- ALTER TABLE mps_business ADD CONSTRAINT DF_mps_business_ord_groupkey DEFAULT '' FOR ord_groupkey 
         set @sql='ALTER TABLE  '+@tname+' ADD CONSTRAINT  DF_'+@tname+'_'+@cname+'  DEFAULT '+@txt+' FOR '+@cname+' '+CHAR(13)+CHAR(10)  
           insert into #m(t_sql) values(@sql)
           set @sql='update '+@tname+' set '+@cname+'='+@txt+' where '+@cname+' is null'+CHAR(13)+CHAR(10)  
           insert into #m(t_sql) values(@sql)
       fetch next from up_add_column into @tname,@cname,@txt
       end
       close up_add_column
       deallocate up_add_column     select t_sql as [新增的字段赋缺省值] from #m

    declare up_column cursor local forward_only read_only
    for 
        select tnm,cnm,tp,lth,prec,scale from #b
    open up_column
    fetch next from up_column into @tname,@cname,@type,@length,@prec,@scale
    while @@fetch_status = 0
    begin
       if not exists(select * from #a where tnm=@tname and cnm=@cname and tp=@type and lth=@length and prec=@prec and scale=@scale)
       begin
            if not exists(select * from #d where tnm=@tname)
            begin
                if not exists(select * from #e where tnm=@tname and cnm=@cname)
                insert into #c(tnm,cnm,tp,lth,prec,scale) values(@tname,@cname,@type,@length,@prec,@scale)
             end
       end
    fetch next from up_column into @tname,@cname,@type,@length,@prec,@scale
    end
    close up_column
    deallocate up_column
select tnm '被修改字段',cnm,tp,lth,prec,scale from #c where tp<>'sysname' and lth is not null order by tnm,cnm --被修改字段


    
    --修改旧表中被修改的字段
    declare up_old cursor local forward_only read_only
    for
        select tnm,cnm,tp,lth,prec,scale from #c where tp<>'sysname' and lth is not null
    open up_old
    fetch next from up_old into @tname,@cname,@type,@length,@prec,@scale
    while @@fetch_status = 0
    begin
        if(@type='varchar' or @type='nvarchar' or @type='char' or @type='nchar' or @type='binary' or @type='varbinary')
        begin
            set @sql='alter table '+@tname+' alter column '+@cname+' '+@type+'('+cast(@length as varchar)+')'+CHAR(13)+CHAR(10)  
        end
        else if(@type='decimal' or @type='numeric')
        begin
            set @sql='alter table '+@tname+' alter column '+@cname+' '+@type+'('+cast(@length as varchar)+','+cast(@scale as varchar)+')'   +CHAR(13)+CHAR(10)  
        end
        else
        begin
            set @sql='alter table '+@tname+' alter column '+@cname+' '+@type+CHAR(13)+CHAR(10)  
        end
        -- 先删除
        insert into #g(t_sql) values('alter table '+@tname+' drop CONSTRAINT DF_'+@tname+'_'+@cname)
        insert into #g(t_sql) values(' go ')
        
        insert into #g(t_sql) values(@sql)
        insert into #g(t_sql) values(' go ')
        -- 重新生成缺省值
        -- #h(tnm varchar(100),cnm varchar(100),txt varchar(50)) 
        set @txt = '' 
        select top 1 @txt=txt from #h where tnm=@tname and cnm=@cname 
        set @txt=isnull(@txt,'') 
        if (@txt>'') 
        begin
          insert into #g(t_sql) values('ALTER TABLE '+@tname+'  ADD  CONSTRAINT [DF_'+@tname+'_'+@cname+']  DEFAULT '+@txt+' FOR ['+@cname+']') 
          insert into #g(t_sql) values(' go ')
        end 
    fetch next from up_old into @tname,@cname,@type,@length,@prec,@scale
    end 
    close up_old
    deallocate up_old

select t_sql as [被修改字段的SQL语句] from #g  --修改旧库中表被修改的字段 与新库中对应表字段类型相同

       declare up_old_cnm cursor local forward_only read_only
       for
            select tnm,cnm,txt from #h where tnm<>'dtproperties'
       open up_old_cnm
       fetch next from up_old_cnm into @tname,@cname,@txt
       while @@fetch_status = 0
       begin
           set @sql='update '+@tname+' set '+@cname+'='+@txt+' where '+@cname+' is null'+CHAR(13)+CHAR(10)  
           insert into #j(t_sql) values(@sql)
       fetch next from up_old_cnm into @tname,@cname,@txt
       end
       close up_old_cnm
       deallocate up_old_cnm

-- select t_sql as [被修改字段赋缺省值] from #j  --修改旧库中每个表的默认值（该字段内容为 null 的才修改）

       declare up_old_cnm cursor local forward_only read_only
       for
            select tnm,cnm,txt from #i where tnm<>'dtproperties'
       open up_old_cnm
       fetch next from up_old_cnm into @tname,@cname,@txt
       while @@fetch_status = 0
       begin
           set @sql='update '+@tname+' set '+@cname+'='+@txt+' where '+@cname+' is null'+CHAR(13)+CHAR(10)  
           insert into #k(t_sql) values(@sql)
       fetch next from up_old_cnm into @tname,@cname,@txt
       end
       close up_old_cnm
       deallocate up_old_cnm

select t_sql as [被修改字段赋缺省值] from #k  --修改新库中每个表的默认值（该字段内容为 null 的才修改）

--select * from #k left join #j on #j.t_sql=#k.t_sql where #j.t_sql is null
drop table #a
drop table #b
drop table #c
drop table #d
drop table #e
drop table #f
drop table #g
drop table #h
drop table #i
end