Automating Schema Comparison Notifications in SQL Server

Automating Schema Comparison Notifications in SQL Server


Learn how to automatically send schema comparison logs between your development and production SQL Server databases with dbForge Schema Compare. This tutorial covers setting up email notifications for synchronization failures or differences, ensuring you're always in the loop

0:00 Introduction

0:38 Prerequisite and workflow

1:09 Step 1: Creating a PowerShell script file

2:03 Step 2: Creating a BAT file

3:20 Step 3: Launching the .bat file

Code used in this video:

Step 1: Create a PowerShell script file to send an SMTP e-mail with a schema comparison log

#Input data:
$From = "[sender’s email address]"
$To = "[recipient’s email address]"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "jordansanders"
$Password = "useveiiwtflclxrw"
$subject = "Synchronization log"
$body = "Database synchronization failed"
$file = "D:\logs\CompareResult.log"

#Message in the HTML format:
$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $subject
$message.IsBodyHTML = $true
$message.Body = $body
$att = new-object Net.Mail.Attachment($file)
$message.Attachments.Add($att)

#Recipient info:
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($message)
$att.Dispose()
pause

Step 2: Create a BAT file to initiate schema comparison and trigger email sending in case of an error

Set Compare="C:\Program Files\Devart\Compare Bundle for SQL Server\dbForge Schema Compare for SQL Server\schemacompare.com"
Set Sender= powershell.exe
Set ConnectionS= JordanS\SQL2022
Set ConnectionT= JordanSNew\SQL2022
Set DB1= SakilaDev
Set DB2= SakilaTest

%compare% /schemacompare /source connection:"Data Source=%ConnectionS%;Initial Catalog=%DB1%;Integrated Security=False;User ID=user_name; Password=user_password;" /target connection:"Data Source=%ConnectionT%;Initial Catalog=%DB2%;Integrated Security=False;User ID=user_name; Password=user_password;" /log:"D:\logs\LogFile.log"

echo exitcode: %ERRORLEVEL%

set Send=F
if %ERRORLEVEL% EQU 40 set Send=T
if %ERRORLEVEL% EQU 101 set Send=T
if %Send%==T (%Sender% -File D:\logs\SendLog\sendlog.ps1)

pause

👉 Check full overview of dbForge Schema Compare for SQL Server here: https://www.devart.com/dbforge/sql/schemacompare/

👉 Check full overview of dbForge Studio for SQL Server (including Schema Compare) here: https://www.devart.com/dbforge/sql/studio/

🔥 You can download dbForge Studio for SQL Server Trial (including Schema Compare) here: https://www.devart.com/dbforge/sql/studio/download.html

🔥 You can download dbForge dbForge Compare Bundle for SQL Server Trial (including Schema Compare) here: https://www.devart.com/dbforge/sql/schemacompare/download.html

🔔 Subscribe to our channel and get dozens of tutorials about Devart tools: https://www.youtube.com/c/DevartSoftware

#SQLSchemaComparison #SQLServer #Database #dbForge #Devart

Video Length: 04:32
Uploaded By: Devart
Published: 2/29/2024
View Count: 354

Related Software Products
dbForge Schema Compare for SQL Server
dbForge Schema Compare for SQL Server

Published By:
Devart

Description:
dbForge Schema Compare for SQL Server: See What Changed, Then Deploy It Safely dbForge Schema Compare for SQL Server is Devart's Windows tool for comparing the structure of two SQL Server or Azure SQL databases and generating the script that makes one match the other. Professional costs $299.95 a year and adds backups, script folders, source control and the command line as sources; Standard is $239.95 a year and compares live databases and...


Related Videos
Compare and deploy database schemas with dbForge Schema Compare for SQL Server
Compare and deploy database schemas with dbForge Schema Compare for SQL Server

Video Length: 03:16
Uploaded By: Devart (7/11/2019)
View Count: 4,938

dbForge Schema Compare for SQL Server Introductory Tutorial
dbForge Schema Compare for SQL Server Introductory Tutorial

Video Length: 03:32
Uploaded By: Devart (11/18/2014)
View Count: 4,688

Get started with dbForge Schema Compare
Get started with dbForge Schema Compare

Video Length: 02:09
Uploaded By: Devart (3/21/2022)
View Count: 1,041

Copyright © 2026, Ivertech. All rights reserved.