Quantcast
Channel: SQL PraRup – SQL Server Mentalist
Viewing all articles
Browse latest Browse all 121

BI SQL # 238 : SQL Server DBA Scripts : B. Compare Database

$
0
0

Hi Folks,

In this article we are going to cover 2 type of comparing Database B. 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 is a stored procedure to compare the code of the objects like Stored Procedures, Triggers , Views and Function between two databases. This will be help full to generate a report on which objects are out of synch. This uses the system table SYS.SQL_modules.

    SQL Script Output Column

    image


    Input Parameter of SQL Script

    @SourceDB SYSNAME, @TargetDb SYSNAME, @ObjectName SYSNAME

SQL Script Code

IF OBJECT_ID('CompareObjectCode', 'P') IS NOT NULL
    DROP PROC CompareObjectCode
GO

CREATE PROC CompareObjectCode @SourceDBName SYSNAME
    ,@DestDBName SYSNAME
    ,@ObjectName SYSNAME = '%'
AS
DECLARE @SQL VARCHAR(MAX)

SELECT @Sql = 
    ' 
    SELECT  ''Object Name'' = ISNULL(SRC.name,Dest.name) 
           ,''Object Type'' = ISNULL(SRC.type_desc,Dest.type_desc) 
           ,''Status'' =   CASE 
                                WHEN SRC.Definition is null THEN 
''Missing in Source'' 
                                WHEN Dest.Definition is null THEN 
''Missing in Destination'' 
                                WHEN SRC.Definition <> Dest.Definition 
THEN ''Definition Mismatch'' 
                                ELSE ''Match'' 
                                END 
           ,''Source Created Date'' = SRC.create_date 
           ,''Source Modify Date''  = SRC.modify_date 
           ,''Destination Created Date'' = Dest.create_date 
           ,''Destination Modify Date''  = Dest.modify_date 
      FROM  
        ( 
         SELECT so.name,so.create_date,so.modify_date,sm.definition 
,so.type_desc 
           FROM ' 
    + @SourceDBName + '.SYS.objects so 
           JOIN ' + 
    @SourceDBName + 
    '.SYS.sql_modules sm 
             ON so.object_id = sm.object_id 
          WHERE so.name like ''' 
    + @ObjectName + 
    ''' 
        ) Src 
        FULL OUTER JOIN 
        ( 
         SELECT so.name,so.create_date,so.modify_date,sm.definition 
,so.type_desc 
           FROM ' 
    + @DestDBName + '.SYS.objects so 
           JOIN ' + 
    @DestDBName + 
    '.SYS.sql_modules sm 
             ON so.object_id = sm.object_id 
          WHERE so.name like ''' 
    + @ObjectName + 
    ''' 
        ) Dest 
        ON SRC.name = dEST.name 
    '

EXEC (@Sql)
GO

SQL Script Output Screenshot

image

User Level to execute

      200

    Hope you will like 2 type of comparing Database B. 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 |


Filed under: Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar Tagged: DBA, Fast SQL, Microsoft SQL Server, SQL Help, SQL Mentalist, SQL Script, SQL Server, SQL Server 2008 R2, SQL Server 2012, SQL tips, SQL Tricks, TSQL, Vishal Pawar

Viewing all articles
Browse latest Browse all 121

Trending Articles