Compare Schema

  • How cud i compare a table structure from one server to another using T-SQl. I just need to check the length of data types from 2 identical tables on different server.

    thanks

  • checksum() and checksum_agg() and INFORMATION_SCHEMA.COLUMNS

    might help you out.

    ~BOT

  • Try this:

    select c.name,c.length,c.xusertype,lc.name,lc.length,lc.xusertype from sysobjects o inner join syscolumns c

    on o.id=c.id

    ,[DestServerName].northwind.dbo.sysobjects lo join [DestServerName].northwind.dbo.syscolumns lc

    on lo.id=lc.id

    where c.name =lc.name

    and o.name='orders'

    and lo.name='orders'

    I wrote this test code for comparing northwind database orders table columns.

    HTH!

    MJ

  • I would like to compare all the columns in the DB from one server to another and populate the differences by using T-SQL just as we get from redgate comapre since i can not afford it am trying to do through T-SQL. any one has such scripts.

  • How about OpenDBiff from CodePlex:

    http://www.codeplex.com/OpenDBiff

    Or SQL Accord Community Edition:

    http://www.sqleffects.com/Articles/Product/sqlAccordInfo/aboutSqlAccordCommunityEd.html

    Kendal Van Dyke
    http://kendalvandyke.blogspot.com/[/url]

  • This Might Help.

    This will compare two databases on the same server... similarly extend the code to compare two databases on different... Should be very easy...

    This script will give you Database name, Table name and Column name and column information for all those tables which are not present in either one of the database.

    Similarly if you change the Type in the script , you can compare different objects, like

    Type = U - Compares tables. -- Currently configured.

    Type = V - Compare Views -- Columns and table names should be changed in the script

    Type = P - Compare stored procedures. -- Columns and table names should be changed in the script

    -- Works only in SQL SERVER 2005, ( Does not work in SQL Server 2000)

    CREATE proc USP_CompareSchema

    @db1 sysname ,

    @db2 sysname,

    @db3 sysname -- This is where your result table will be created.

    as

    declare @sql varchar(8000)

    declare @result varchar(8000)

    declare @table_name varchar(100)

    declare @Nomismatchsql varchar(100)

    select @table_name = @db3+'.dbo.SCHEMAVALIDATIONRESULTS_'+ convert(varchar(8),getdate(),112)+'_' + substring ( convert(varchar(20),getdate(),13) , 13 , 2)+ '_'+substring ( convert(varchar(20),getdate(),13) , 16 , 2)

    set @sql = 'select * into '+@table_name+' from

    (

    select C.name '+'''Table_Name'''+', A.name '+'''Column_Name'''+', B.name '+'''Data_Type'''+',

    case when A.length = '+'''-1'''+' then '+'''Max'''+' else convert(varchar(50), A.length) end '+'''Column_Length'''+','+'''' +@db1+''''+' '+'''DATABASE NAME'''+' from '+@db1+'.dbo.Syscolumns A, '+@db1+'.dbo.Systypes B, '+@db1+'.dbo.sysobjects C

    where A.xusertype = B.xusertype and A.id = C.id and C.xtype = '+'''u'''+' and c.id <> 645577338

    except

    select C.name '+'''Table_Name'''+', A.name '+'''Column_Name'''+', B.name '+'''Data_Type'''+',

    case when A.length = '+'''-1'''+' then '+'''Max'''+' else convert(varchar(50), A.length) end '+'''Column_Length'''+','+ ''''+@db1+''''+' '+'''DATABASE NAME'''+' from '+@db2+'.dbo.Syscolumns A, '+@db2+'.dbo.Systypes B, '+@db1+'.dbo.sysobjects C

    where A.xusertype = B.xusertype and A.id = C.id and C.xtype = '+'''u'''+' and c.id <> 645577338 ) X

    Insert into '+@table_name+' select * from

    (

    select C.name '+'''Table_Name'''+', A.name '+'''Column_Name'''+', B.name '+'''Data_Type'''+',

    case when A.length = '+'''-1'''+' then '+'''Max'''+' else convert(varchar(50), A.length) end '+'''Column_Length'''+','+'''' +@db2+''''+' '+'''DATABASE NAME'''+' from '+@db2+'.dbo.Syscolumns A, '+@db2+'.dbo.Systypes B, '+@db2+'.dbo.sysobjects C

    where A.xusertype = B.xusertype and A.id = C.id and C.xtype = '+'''u'''+' and c.id <> 645577338

    except

    select C.name '+'''Table_Name'''+', A.name '+'''Column_Name'''+', B.name '+'''Data_Type'''+',

    case when A.length = '+'''-1'''+' then '+'''Max'''+' else convert(varchar(50), A.length) end '+'''Column_Length'''+','+ ''''+@db2+''''+' '+'''DATABASE NAME'''+' from '+@db1+'.dbo.Syscolumns A, '+@db1+'.dbo.Systypes B, '+@db1+'.dbo.sysobjects C

    where A.xusertype = B.xusertype and A.id = C.id and C.xtype = '+'''u'''+' and c.id <> 645577338 ) Y'

    exec (@sql)

    select 'Results are stored in Table '+@Table_name 'Name of Results Table'

    set @result = 'select * from '+@Table_name

    exec (@result)

    Example :

    Mirror4, Mirror3 , Mirror2 are the names of the database.

    Final Result table will be stored in mirror2 database in this example.

    -- Input

    exec USP_CompareSchema 'Mirror4', 'Mirror3', 'Mirror2'

    -- Output

    Table_NameColumn_NameData_TypeColumn_LengthDATABASE NAME

    example1reidint4mirror3

    example1renamevarchar20mirror3

    example1meidint4mirror4

    example1menamevarchar20mirror4

    Try it once...

    Thanks,

    IM

  • Please create this SP on Master database :

    CREATE procedure dbo.proc_compare_databases_tables

    @svr_a sysname, -- Servername of source database

    @svr_b sysname, --Source database

    @db_a sysname, -- Destination servername

    @db_b sysname --Destination database

    as

    begin

    set nocount on

    -- if @db_a is null

    -- begin

    -- raiserror('Database A name is null', 16, 1)

    -- return

    -- end

    --

    -- if @db_b is null

    -- begin

    -- raiserror('Database B name is null', 16, 1)

    -- return

    -- end

    --

    -- if db_id(@db_a) is null

    -- begin

    -- raiserror('Database A does not exist', 16, 1)

    -- return

    -- end

    --

    -- if db_id(@db_b) is null

    -- begin

    -- raiserror('Database B does not exist', 16, 1)

    -- return

    -- end

    declare @command varchar(max)

    ------------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------------

    -- compare tables

    set @command = cast('select

    ra.schema_name,

    rb.schema_name,

    ra.table_name,

    rb.table_name,

    ra.column_name,

    rb.column_name,

    ra.type_name,

    rb.type_name,

    ra.uses_ansi_nulls,

    rb.uses_ansi_nulls,

    ra.column_id,

    rb.column_id,

    ra.system_type_id,

    rb.system_type_id,

    ra.max_length,

    rb.max_length,

    ra.precision,

    rb.precision,

    ra.scale,

    rb.scale,

    ra.collation_name,

    rb.collation_name,

    ra.is_nullable,

    rb.is_nullable,

    ra.is_ansi_padded,

    rb.is_ansi_padded,

    ra.is_rowguidcol,

    rb.is_rowguidcol,

    ra.is_identity,

    rb.is_identity,

    ra.is_computed,

    rb.is_computed

    from

    (

    select

    st.object_id,

    ss.name as schema_name,

    st.name as table_name,

    sc.name as column_name,

    sts.name as type_name,

    uses_ansi_nulls,

    column_id,

    sc.system_type_id,

    sc.user_type_id,

    sc.max_length,

    sc.precision,

    sc.scale,

    sc.collation_name,

    sc.is_nullable,

    sc.is_ansi_padded,

    sc.is_rowguidcol,

    sc.is_identity,

    sc.is_computed

    from ['+@svr_a+'].[' as varchar(max)) + @db_a + '].sys.tables st inner join

    ['+@svr_a+'].[' + @db_a + '].sys.schemas ss on

    st.schema_id = ss.schema_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.columns as sc on

    sc.object_id = st.object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.types as sts on

    sc.user_type_id = sts.user_type_id

    )

    as ra full outer join

    (

    select

    st.object_id,

    ss.name as schema_name,

    st.name as table_name,

    sc.name as column_name,

    sts.name as type_name,

    uses_ansi_nulls,

    column_id,

    sc.system_type_id,

    sc.user_type_id,

    sc.max_length,

    sc.precision,

    sc.scale,

    sc.collation_name,

    sc.is_nullable,

    sc.is_ansi_padded,

    sc.is_rowguidcol,

    sc.is_identity,

    sc.is_computed

    from

    ['+@svr_b+'].[' + @db_b + '].sys.tables st inner join

    ['+@svr_b+'].[' + @db_b + '].sys.schemas ss on

    st.schema_id = ss.schema_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.columns as sc on

    sc.object_id = st.object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.types as sts on

    sc.user_type_id = sts.user_type_id

    )

    as rb on

    ra.schema_name = rb.schema_name collate database_default and

    ra.table_name = rb.table_name collate database_default and

    ra.column_name = rb.column_name collate database_default

    where

    ra.object_id is null or rb.object_id is null or

    (ra.object_id is not null and rb.object_id is not null and

    (

    (ra.type_name <> rb.type_name collate database_default ) or

    (ra.column_id <> rb.column_id) or

    (ra.uses_ansi_nulls <> rb.uses_ansi_nulls) or

    (ra.system_type_id <> rb.system_type_id) or

    (ra.user_type_id <> rb.user_type_id) or

    (ra.max_length <> rb.max_length) or

    (ra.precision <> rb.precision) or

    (ra.scale <> rb.scale) or

    (ra.is_nullable <> rb.is_nullable) or

    (ra.is_ansi_padded <> rb.is_ansi_padded) or

    (ra.is_rowguidcol <> rb.is_rowguidcol) or

    (ra.is_identity <> rb.is_identity) or

    (ra.is_computed <> rb.is_computed) or

    (ra.collation_name <> rb.collation_name collate database_default)

    )

    )

    order by

    ra.schema_name,

    ra.table_name,

    ra.column_name'

    --print @command

    print (@command)

    exec (@command)

    ------------------------------------------------------------------------------------------------------------------

    -- compare indexes

    set @command = cast('

    select

    ra.schema_name,

    rb.schema_name,

    ra.table_name,

    rb.table_name,

    ra.index_name,

    rb.index_name,

    ra.index_column_name,

    rb.index_column_name,

    ra.type_name,

    rb.type_name,

    ra.is_primary_key,

    rb.is_primary_key,

    ra.key_ordinal,

    rb.key_ordinal,

    ra.is_descending_key,

    rb.is_descending_key,

    ra.is_included_column,

    rb.is_included_column,

    ra.type,

    rb.type,

    ra.type_desc,

    rb.type_desc,

    ra.is_unique,

    rb.is_unique,

    ra.ignore_dup_key,

    rb.ignore_dup_key,

    ra.is_unique_constraint,

    rb.is_unique_constraint,

    ra.is_disabled,

    rb.is_disabled,

    ra.system_type_id,

    rb.system_type_id,

    ra.user_type_id,

    rb.user_type_id

    from

    (

    select

    st.object_id,

    ss.name as schema_name,

    st.name as table_name,

    si.name as index_name,

    sc.name as index_column_name,

    sts.name as type_name,

    is_primary_key,

    key_ordinal,

    is_descending_key,

    is_included_column,

    si.type,

    si.type_desc,

    is_unique,

    ignore_dup_key,

    is_unique_constraint,

    is_disabled,

    sc.system_type_id,

    sc.user_type_id

    from

    ['+@svr_a+'].['as varchar(max)) + @db_a + '].sys.tables st inner join

    ['+@svr_a+'].[' + @db_a + '].sys.schemas ss on

    st.schema_id = ss.schema_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.columns as sc on

    sc.object_id = st.object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.indexes as si on

    si.object_id = st.object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.index_columns sic on

    si.object_id = sic.object_id and

    si.index_id = sic.index_id and

    sc.column_id = sic.column_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.types as sts on

    sc.user_type_id = sts.user_type_id

    )

    as ra full outer join

    (

    select

    st.object_id,

    ss.name as schema_name,

    st.name as table_name,

    si.name as index_name,

    sc.name as index_column_name,

    sts.name as type_name,

    is_primary_key,

    key_ordinal,

    is_descending_key,

    is_included_column,

    si.type,

    si.type_desc,

    is_unique,

    ignore_dup_key,

    is_unique_constraint,

    is_disabled,

    sc.system_type_id,

    sc.user_type_id

    from

    ['+@svr_b+'].[' + @db_b + '].sys.tables st inner join

    ['+@svr_b+'].[' + @db_b + '].sys.schemas ss on

    st.schema_id = ss.schema_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.columns as sc on

    sc.object_id = st.object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.indexes as si on

    si.object_id = st.object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.index_columns sic on

    si.object_id = sic.object_id and

    si.index_id = sic.index_id and

    sc.column_id = sic.column_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.types as sts on

    sc.user_type_id = sts.user_type_id

    )

    as rb on

    ra.schema_name = rb.schema_name collate database_default and

    ra.table_name = rb.table_name collate database_default and

    ra.index_name = rb.index_name collate database_default and

    ra.index_column_name = rb.index_column_name

    where

    ra.object_id is null or rb.object_id is null or

    (ra.object_id is not null and rb.object_id is not null and

    (

    (ra.is_primary_key <> rb.is_primary_key) or

    (ra.type <> rb.type) or

    (ra.type_desc <> rb.type_desc collate database_default ) or

    (ra.type_name <> rb.type_name collate database_default ) or

    (ra.is_unique <> rb.is_unique) or

    (ra.is_descending_key <> rb.is_descending_key) or

    (ra.key_ordinal <> rb.key_ordinal) or

    (ra.is_included_column <> rb.is_included_column) or

    (ra.ignore_dup_key <> rb.ignore_dup_key) or

    (ra.is_unique_constraint <> rb.is_unique_constraint) or

    (ra.is_disabled <> rb.is_disabled) or

    (ra.system_type_id <> rb.system_type_id) or

    (ra.user_type_id <> rb.user_type_id)

    )

    )

    order by

    ra.schema_name,

    ra.table_name,

    ra.is_primary_key desc,

    ra.index_name,

    ra.is_included_column asc,

    ra.key_ordinal asc'

    --print @command

    print (@command)

    exec (@command)

    -------------------------------

    set @command = cast('select

    ra.schema_name,

    rb.schema_name,

    ra.table_name,

    rb.table_name,

    ra.check_constraint_name,

    rb.check_constraint_name,

    ra.type_desc,

    rb.type_desc,

    ra.definition,

    rb.definition

    from

    (

    select

    st.object_id,

    ss.name as schema_name,

    st.name as table_name,

    scc.name as check_constraint_name,

    scc.type_desc,

    definition

    from

    ['+@svr_a+'].['as varchar(max)) + @db_a + '].sys.check_constraints as scc inner join

    ['+@svr_a+'].[' + @db_a + '].sys.tables as st on

    st.object_id = scc.parent_object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.schemas ss on

    st.schema_id = ss.schema_id

    )

    as ra full outer join

    (

    select

    st.object_id,

    ss.name as schema_name,

    st.name as table_name,

    scc.name as check_constraint_name,

    scc.type_desc,

    definition

    from

    ['+@svr_b+'].[' + @db_b + '].sys.check_constraints as scc inner join

    ['+@svr_b+'].[' + @db_b + '].sys.tables as st on

    st.object_id = scc.parent_object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.schemas ss on

    st.schema_id = ss.schema_id

    )

    as rb on

    ra.schema_name = rb.schema_name collate database_default and

    ra.table_name = rb.table_name collate database_default and

    ra.check_constraint_name = rb.check_constraint_name collate database_default

    where

    ra.object_id is null or rb.object_id is null or

    (ra.object_id is not null and rb.object_id is not null and

    (

    (ra.definition <> rb.definition)

    )

    )

    order by

    ra.schema_name,

    ra.table_name,

    ra.check_constraint_name'

    --print @command

    print (@command)

    exec (@command)

    ---------------------------------------------------------------------------------------------

    -- compare foreign keys

    set @command = cast('select

    ra.foreign_key_name,

    rb.foreign_key_name,

    ra.type_desc,

    rb.type_desc,

    ra.is_disabled,

    rb.is_disabled,

    ra.delete_referential_action,

    rb.delete_referential_action,

    ra.delete_referential_action_desc,

    rb.delete_referential_action_desc,

    ra.update_referential_action,

    rb.update_referential_action,

    ra.update_referential_action_desc,

    rb.update_referential_action_desc,

    ra.schema_a_name,

    rb.schema_a_name,

    ra.table_a_name,

    rb.table_a_name,

    ra.schema_b_name,

    rb.schema_b_name,

    ra.table_b_name,

    rb.table_b_name,

    ra.column_name,

    rb.column_name,

    ra.ref_column_name,

    rb.ref_column_name

    from

    (

    select

    sfk.object_id,

    sfk.name as foreign_key_name,

    sfk.type_desc,

    is_disabled,

    delete_referential_action,

    delete_referential_action_desc,

    update_referential_action,

    update_referential_action_desc,

    ss1.name as schema_a_name,

    so1.name as table_a_name,

    ss2.name as schema_b_name,

    so2.name as table_b_name,

    sc1.name as column_name,

    sc2.name as ref_column_name

    from

    ['+@svr_a+'].[' as varchar(max)) + @db_a + '].sys.foreign_keys as sfk inner join

    ['+@svr_a+'].[' + @db_a + '].sys.objects as so1 on

    so1.object_id = sfk.parent_object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.objects as so2 on

    so2.object_id = sfk.parent_object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.foreign_key_columns as sfkc on

    sfk.object_id = sfkc.constraint_object_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.columns as sc1 on

    sc1.object_id = sfkc.referenced_object_id and

    sc1.column_id = sfkc.parent_column_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.columns as sc2 on

    sc2.object_id = sfkc.referenced_object_id and

    sc2.column_id = sfkc.parent_column_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.schemas as ss1 on

    so1.schema_id = ss1.schema_id inner join

    ['+@svr_a+'].[' + @db_a + '].sys.schemas as ss2 on

    so2.schema_id = ss2.schema_id

    )

    as ra full outer join

    (

    select

    sfk.object_id,

    sfk.name as foreign_key_name,

    sfk.type_desc,

    is_disabled,

    delete_referential_action,

    delete_referential_action_desc,

    update_referential_action,

    update_referential_action_desc,

    ss1.name as schema_a_name,

    so1.name as table_a_name,

    ss2.name as schema_b_name,

    so2.name as table_b_name,

    sc1.name as column_name,

    sc2.name as ref_column_name

    from

    ['+@svr_b+'].[' + @db_b + '].sys.foreign_keys as sfk inner join

    ['+@svr_b+'].[' + @db_b + '].sys.objects as so1 on

    so1.object_id = sfk.parent_object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.objects as so2 on

    so2.object_id = sfk.parent_object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.foreign_key_columns as sfkc on

    sfk.object_id = sfkc.constraint_object_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.columns as sc1 on

    sc1.object_id = sfkc.referenced_object_id and

    sc1.column_id = sfkc.parent_column_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.columns as sc2 on

    sc2.object_id = sfkc.referenced_object_id and

    sc2.column_id = sfkc.parent_column_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.schemas as ss1 on

    so1.schema_id = ss1.schema_id inner join

    ['+@svr_b+'].[' + @db_b + '].sys.schemas as ss2 on

    so2.schema_id = ss2.schema_id

    )

    as rb on

    ra.schema_a_name = rb.schema_a_name collate database_default and

    ra.schema_b_name = rb.schema_b_name collate database_default and

    ra.table_a_name = rb.table_a_name collate database_default and

    ra.table_b_name = rb.table_b_name collate database_default and

    ra.table_a_name = rb.table_a_name collate database_default and

    ra.column_name = rb.ref_column_name collate database_default and

    ra.foreign_key_name = rb.foreign_key_name collate database_default

    where

    ra.object_id is null or rb.object_id is null or

    (ra.object_id is not null and rb.object_id is not null and

    (

    (ra.is_disabled <> rb.is_disabled) or

    (ra.delete_referential_action <> rb.delete_referential_action) or

    (ra.update_referential_action_desc <> rb.update_referential_action_desc)

    )

    )

    order by

    ra.foreign_key_name,

    rb.foreign_key_name,

    ra.schema_a_name,

    rb.schema_a_name,

    ra.table_a_name,

    rb.table_a_name,

    ra.schema_b_name,

    rb.schema_b_name,

    ra.table_b_name,

    rb.table_b_name,

    ra.column_name,

    rb.column_name,

    ra.ref_column_name,

    rb.ref_column_name'

    --print @command

    print (@command)

    exec (@command)

    ---------------------------------------------------------------------------------------------------

    -- -- find other missing objects

    --

    -- set @command = cast('select

    -- ra.schema_name,

    -- ra.object_name,

    -- ra.object_type,

    -- rb.schema_name,

    -- rb.object_name,

    -- rb.object_type

    -- from

    -- (

    -- select

    -- ss.name as schema_name,

    -- so.object_id,

    -- so.name as object_name,

    -- so.type as object_type

    -- from

    -- [' as varchar(max)) + @db_a + '].sys.objects as so left outer join

    -- [' + @db_a + '].sys.schemas as ss on

    -- so.schema_id = ss.schema_id

    -- )

    -- as ra full outer join

    -- (

    -- select

    -- ss.name as schema_name,

    -- so.object_id,

    -- so.name as object_name,

    -- so.type as object_type

    -- from

    -- [' + @db_b + '].sys.objects as so left outer join

    -- [' + @db_b + '].sys.schemas as ss on

    -- so.schema_id = ss.schema_id

    -- )

    -- as rb on

    -- ra.schema_name = rb.schema_name collate database_default and

    -- ra.object_name = rb.object_name collate database_default and

    -- ra.object_type = rb.object_type collate database_default

    -- where

    -- ra.object_id is null or rb.object_id is null

    -- order by

    -- ra.schema_name,

    -- rb.schema_name,

    -- ra.object_name,

    -- rb.object_name,

    -- ra.object_type,

    -- rb.object_type'

    --

    -- --print @command

    -- exec (@command)

    end

    ----

    _____________________________________________________________________________________________________________
    Paresh Prajapati
    ➡ +919924626601
    http://paresh-sqldba.blogspot.com/[/url]
    LinkedIn | Tweet Me | FaceBook | Brijj

  • U can also create procedure :

    create PROCEDURE PD_Lista_Tabela_Diferente

    (@DBFontes varchar(50),

    @DBVerifica varchar(50),

    @Iniciais_Nome_Tabela varchar(50)

    )

    AS

    begin

    SET nocount off

    DECLARE @STR varchar(1000)

    CREATE TABLE #TMP_TABELAS_DIFERENTES(TABLE_CATALOG_DBFontes varchar(100),TABLE_NAME_DBFontes varchar(100),COLUMN_NAME_DBFontes varchar(100),DATA_TYPE_DBFontes varchar(100),TABLE_CATALOG_DBVerifica varchar(100),TABLE_NAME_DBVerifica varchar(100),COLUMN_NAME_DBVerifica varchar(100),DATA_TYPE_DBVerifica varchar(100))

    SET @STR = 'SELECT A.TABLE_CATALOG as TABLE_CATALOG_DBFontes,

    A.TABLE_NAME as TABLE_NAME_DBFontes,

    A.COLUMN_NAME as COLUMN_NAME_DBFontes,

    A.DATA_TYPE as DATA_TYPE_DBFontes,

    B.TABLE_CATALOG as TABLE_CATALOG_DBVerifica,

    B.TABLE_NAME as TABLE_NAME_DBVerifica,

    B.COLUMN_NAME as COLUMN_NAME_DBVerifica,

    B.DATA_TYPE as DATA_TYPE_DBVerifica

    FROM '+@DBFontes+'.INFORMATION_SCHEMA.COLUMNS A

    INNER JOIN '+@DBFontes+'.INFORMATION_SCHEMA.TABLES T ON A.TABLE_CATALOG = T.TABLE_CATALOG AND

    A.TABLE_SCHEMA = T.TABLE_SCHEMA AND

    A.TABLE_NAME = T.TABLE_NAME AND T.TABLE_TYPE = ''BASE TABLE''

    LEFT JOIN '+@DBVerifica+'.INFORMATION_SCHEMA.COLUMNS B ON A.TABLE_NAME = B.TABLE_NAME AND A.COLUMN_NAME = B.COLUMN_NAME

    WHERE ( B.COLUMN_NAME IS NULL OR

    A.COLUMN_NAME IS NULL OR

    A.DATA_TYPE <> B.DATA_TYPE )

    AND A.TABLE_NAME LIKE '''+@Iniciais_Nome_Tabela+'%''

    ORDER BY A.TABLE_NAME, A.COLUMN_NAME'

    INSERT INTO #TMP_TABELAS_DIFERENTES (TABLE_CATALOG_DBFontes, TABLE_NAME_DBFontes, COLUMN_NAME_DBFontes, DATA_TYPE_DBFontes,TABLE_CATALOG_DBVerifica, TABLE_NAME_DBVerifica, COLUMN_NAME_DBVerifica, DATA_TYPE_DBVerifica )

    execute(@STR)

    SET nocount on

    SELECT * FROM #TMP_TABELAS_DIFERENTES ORDER BY TABLE_NAME_DBFontes, COLUMN_NAME_DBFontes

    drop table #TMP_TABELAS_DIFERENTES

    end

    GO

    ---

    To execute : exec dbo.PD_Lista_Tabela_Diferente 'new database' , 'old database, ''

    _____________________________________________________________________________________________________________
    Paresh Prajapati
    ➡ +919924626601
    http://paresh-sqldba.blogspot.com/[/url]
    LinkedIn | Tweet Me | FaceBook | Brijj

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply