Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 237 : SQL Server DBA Scripts : A. Compare Database

BI SQL # 237 : SQL Server DBA Scripts : A. Compare Database

Hi Folks,

In this article we are going to cover 3 types of Comparing Database A. Compare Database.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Output Column
  • Input Parameter of SQL Script
  • SQL Script Code
  • SQL Script Output Screenshot
  • User Level to execute

Problem Statement of SQL Script:

Compare database.

Description of SQL Script:

This script compares the table schemas between two SQL Server databases and gives the report for missing columns , data type mismatch , length & precision mismatch and Collation Mismatch.

SQL Script Output Column

image

Input Parameter of SQL Script

@Sourcedb sysname ,@Destdb sysname ,@Tablename sysname

SQL Script Code

DECLARE @Sourcedb SYSNAME
DECLARE @Destdb SYSNAME
DECLARE @Tablename SYSNAME
DECLARE @SQL VARCHAR(max)

SELECT @Sourcedb = 'BenEngine'

SELECT @Destdb = 'BenEngine17Aug'

SELECT @Tablename = 'npsbenefitCode' --  '%' for all tables 

SELECT @SQL = 
    ' SELECT Tablename  = ISNULL(Source.tablename,Destination.tablename) 
                      ,ColumnName = ISNULL(Source.Columnname,
Destination.Columnname) 
                      ,Source.Datatype 
                      ,Source.Length 
                      ,Source.precision 
                      ,Destination.Datatype 
                      ,Destination.Length 
                      ,Destination.precision 
                      ,[Column]  = 
                       Case  
                       When Source.Columnname IS NULL then 
''Column Missing in the Source'' 
                       When Destination.Columnname IS NULL then 
''Column Missing in the Destination'' 
                       ELSE '''' 
                       end 
                      ,DataType = CASE WHEN Source.Columnname IS NOT NULL  
                                        AND Destination.Columnname IS NOT NULL  
                                        AND Source.Datatype <> Destination.
Datatype THEN ''Data Type mismatch''  
                                  END 
                      ,Length   = CASE WHEN Source.Columnname IS NOT NULL  
                                        AND Destination.Columnname IS NOT NULL  
                                        AND Source.Length <> Destination.
Length THEN ''Length mismatch''  
                                  END 
                      ,Precision = CASE WHEN Source.Columnname IS NOT NULL  
                                        AND Destination.Columnname IS NOT NULL 
                                        AND Source.precision <> Destination.
precision THEN ''precision mismatch'' 
                                    END 
                      ,Collation = CASE WHEN Source.Columnname IS NOT NULL  
                                        AND Destination.Columnname IS NOT NULL 
                                        AND ISNULL(Source.collation_name,'''') 
<> ISNULL(Destination.collation_name,'''') THEN ''Collation mismatch'' 
                                        END 
                        
   FROM  
 ( 
 SELECT Tablename  = so.name  
      , Columnname = sc.name 
      , DataType   = St.name 
      , Length     = Sc.max_length 
      , precision  = Sc.precision 
      , collation_name = Sc.collation_name 
  FROM ' 
    + @Sourcedb + '.SYS.objects So 
  JOIN ' + @Sourcedb + 
    '.SYS.columns Sc 
    ON So.object_id = Sc.object_id 
  JOIN ' 
    + @Sourcedb + 
    '.SYS.types St 
    ON Sc.system_type_id = St.system_type_id 
   AND Sc.user_type_id   = St.user_type_id 
 WHERE SO.TYPE =''U'' 
   AND SO.Name like ''' 
    + @Tablename + 
    ''' 
  ) Source 
 FULL OUTER JOIN 
 ( 
  SELECT Tablename  = so.name  
      , Columnname = sc.name 
      , DataType   = St.name 
      , Length     = Sc.max_length 
      , precision  = Sc.precision 
      , collation_name = Sc.collation_name 
  FROM ' 
    + @Destdb + '.SYS.objects So 
  JOIN ' + @Destdb + 
    '.SYS.columns Sc 
    ON So.object_id = Sc.object_id 
  JOIN ' 
    + @Destdb + 
    '.SYS.types St 
    ON Sc.system_type_id = St.system_type_id 
   AND Sc.user_type_id   = St.user_type_id 
WHERE SO.TYPE =''U'' 
  AND SO.Name like ''' 
    + @Tablename + 
    ''' 
 ) Destination  
 ON source.tablename = Destination.Tablename  
 AND source.Columnname = Destination.Columnname '

EXEC (@Sql)

SQL Script Output Screenshot

image

User Level to execute

200

    Hope you will like 3 types of Comparing Database A. Compare Database.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: