Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2019 August SQL Server: How to compare your objects between two Databases

SQL Server: How to compare your objects between two Databases

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

When we are working with different versions of database, database professionals are comparing the objects between databases of SQL Server.
They must make sure about that all databases are in sync.

Using this T-SQL Script, you can compare two databases and find a list of unmatched objects like: Constraints, Tables, Views, Stored Procedure, Triggers.

Sample demonstration:
First, Create two sample databases:

1
2
3
4
CREATE DATABASE ABC
GO
CREATE DATABASE XYZ
GO

Create few sample tables in both the databases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CREATE TABLE ABC.dbo.tbl_Test
(
ID INT
,MyDate DATE
)
GO
 
CREATE TABLE ABC.dbo.tbl_MyTest
(
ID INT
,Name VARCHAR(50)
)
GO
 
CREATE TABLE XYZ.dbo.tbl_Test
(
ID INT
,MyDate DATE
)
GO
 
CREATE TABLE XYZ.dbo.tbl_MyFinal
(
ID INT
,Name VARCHAR(50)
)
GO

T-SQL Script to compare objects between two databases:
According to your databases, You can change the name of @SourceDatabase and @DestinationDatabase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
DECLARE @SourceDatabase VARCHAR(50)
DECLARE @DestinationDatabase VARCHAR(50)
DECLARE @SQL VARCHAR(MAX)
SELECT @SourceDatabase = 'ABC'
SELECT @DestinationDatabase = 'XYZ'
SELECT @SQL =
'
SELECT
ISNULL(S.name,D.name) ObjectName
,CASE
WHEN S.object_id IS NULL
THEN D.type_desc + '' is missing in the Source Database: ' + @SourceDatabase + '''
WHEN D.object_id IS NULL
THEN S.type_desc + '' is missing in the Destination Database: ' + @DestinationDatabase + '''
END ''Status''
FROM
(
SELECT * FROM ' + @SourceDatabase + '.SYS.objects
WHERE Type_desc not in (''INTERNAL_TABLE'',''SYSTEM_TABLE'',''SERVICE_QUEUE'')
) AS S
FULL OUTER JOIN
(
SELECT * FROM ' + @DestinationDatabase + '.SYS.objects
WHERE Type_desc not in (''INTERNAL_TABLE'',''SYSTEM_TABLE'',''SERVICE_QUEUE'')
) AS D
ON S.name = D.name
AND S.type = D.type
ORDER BY isnull(S.type,D.type)
'
EXEC (@Sql)

The Result:

1
2
3
4
5
ObjectName Status
------------------ -------------------------------------------------------
tbl_Test NULL
tbl_MyFinal USER_TABLE is missing in the Source Database: ABC
tbl_MyTest USER_TABLE is missing in the Destination Database: XYZ

Aug 5, 2019Anvesh Patel
SQL Puzzle: SQL Advance Query - Generate the group of SequencesSQL Puzzle: SQL Advance Query - Find the Order basis on thier Status and Step
Comments: 1
  1. Suresh
    October 22, 2019 at 1:48 pm

    Hi Anvesh,

    This script is very useful.

    Please let me know is there any script to compare objects definition (code).

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

August 5, 2019 1 Comment SQL Server, SQL Server DBA ScriptAnvesh Patel, compare, database, database research and development, dbrnd, foreign key, functions, Primary key, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, stored procedures, tables, trigger, TSQL, views
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....