SQL Compare - 9.0
Learning SQL Compare - 9.0
Simple examples using the SQL Compare command line
This topic provides some simple examples of how to use the command line interface.
For more detailed information on using the command line, see:
- Example: selecting single tables for comparison
- Example: selecting tables with unrelated names
- Switches used in the SQL Compare command line
Comparing and synchronizing database schema
To compare the database structure of WidgetStaging with WidgetProduction:
sqlcompare /Database1:WidgetStaging /Database2:WidgetProduction
To compare the database structure of WidgetStaging with WidgetProduction, and synchronize the databases by updating WidgetProduction:
sqlcompare /Database1:WidgetStaging /Database2:WidgetProduction
/Synchronize
Using an existing SQL Compare project from the command line
This is useful, for example, as you cannot choose or create a custom filter from the command line, and specifying complex object selections using a regular expression can be unwieldy.
To use a project you have saved as "widgets.scp" from the command line:
sqlcompare /Project:"C:\SQLCompare\Projects\Widgets.scp"
Note that:
- When you use a project, all objects that were selected for comparison when you saved the project are automatically included.
- When you use the command line, your project option selections are ignored and the defaults are used. Use /Options to specify any additional options you want to use with a command line project.
For more information, see Options used in the command line
- If you want to include or exclude objects from an existing project, you must modify your selection using the graphical user interface.
You cannot use the /Include and /Exclude switches with /Project.
Scheduling or automating a comparison or synchronization
You can use the Microsoft® Windows® Scheduled Task wizard to schedule a comparison by creating a script to run the comparison.
The following example compares the structure of WidgetStaging and WidgetProduction, and outputs the results as the file log_file.txt
First create the script:
cd "C:\Program Files\Red Gate\SQL Compare 8"
sqlcompare /db1:WidgetStaging
/db2:WidgetProduction >> C:\log_file.txt
Next save the script as a .bat file. You specify the .bat file as the program to run from within the Scheduled Task wizard by browsing to it.
To schedule a synchronization of the two databases, updating the database WidgetProduction, you would create the script:
cd "C:\Program Files\Red Gate\SQL Compare 8"
SQLCompare /db1:WidgetStaging /db2:WidgetProduction
/Synchronize
In these examples MS-DOS batch scripting is used, a basic scripting language that is supported on all versions of Windows. If preferred, you could use VBScript, JScript, PHP, Perl, Python or any other scripting language of your choice.
Creating a scripts folder
You can create a folder of object creation scripts representing the schema of a data source. To export WidgetProduction to a scripts folder:
sqlcompare /Database1:WidgetProduction
/Makescripts:"C:\WidgetProductionScripts"
If the folder does not already exist, it is created. All the subfolders containing different object types in the schema are automatically created beneath the specified folder. If the folder does exist, it must be empty or the export will fail.
For more information on scripts folders, see Working with scripts folders
Creating a snapshot
You can create a SQL Compare snapshot representing the schema of a data source. To export WidgetProduction to a snapshot:
sqlcompare /Database1:WidgetProduction
/Makesnapshot:"C:\Snapshots\WP_1.snp"
For more information on SQL Compare schema snapshots, see Working with snapshots
Using a scripts folder or snapshot as a data source
To compare the WidgetProduction scripts folder with the WidgetStaging database:
sqlcompare /Scripts1:"C:\WidgetProductionScripts" /Database2:WidgetStaging
To compare the WidgetStaging database with the WidgetProduction scripts folder and synchronize the scripts
sqlcompare /Database1:WidgetStaging /Scripts2:"C:\WidgetProductionScripts" /Synchronize /Force
The /force switch specifies that any read-only files in the scripts folder that need to be edited during synchronization will be made writable. If you do not include the /force switch and read-only files need to be modified, the synchronization will fail and an error message will be displayed.
To compare two snapshots of WidgetStaging:
sqlcompare /Snapshot1:"C:\Snapshots\WidgetProd_1.snp"
/Snapshot2:"C:\Snapshots\WidgetProd_2.snp"
To output the synchronization SQL script, for example for auditing purposes, and overwrite the file if it already exists:
sqlcompare /database1:WidgetStaging /database2:WidgetProduction
/scriptfile:"C:\SQLScripts\Widgets.sql" /force
Using a backup as a data source
To compare a backup of WidgetDev with WidgetLive:
sqlcompare /Backup1:"D:\MSSQL\BACKUP\WidgetDev_20080807_143235.sqb"
/db2:WidgetLive
If you are comparing a backup set that contains multiple files, use the /backupset1 switch to specify the files which make up the first backup set, and use the /backupset2 switches to specify the files which make up the second:
sqlcompare /Backup1:"D:\MSSQL\BACKUP\WidgetDev.bak"
/Backupset1:"2008-09-23 Full Backup" /db2:WidgetLive
If the backup set switches are not specified, SQL Compare uses the latest backup set.
To specify more than one backup file, separate the file names using semicolons.
sqlcompare /Backup1:"D:\WidgetDev_Full.bak";"D:\WidgetDev_Diff.bak"
/db2:WidgetDevelopment
For encrypted backups that have been created using SQL Backup, use the /BackupPassword1 and /BackupPassword2 switches to specify the passwords; when there is more than one backup password, separate the passwords using semicolons.
sqlcompare /Backup1:"D:\MSSQL\BACKUP\WidgetDev.sqb" /password1:Pa$$w0rd
/db2:WidgetLive
Note that if you are comparing a differential backup, you must also specify the associated full backup.
Fore more information, see Working with backups
See also |
Was this article helpful?
SQL Compare
- Logging and log files
- Stored Procedures and Functions created with wrong name
- Synchronization scripts may be in incorrect version of SQL syntax
- Extended properties not permitted error (version 5.2)
- CLR Procedures' permissions are not recreated (version 5)
- Support for 'returns NULL on NULL Input' in CLR functions (version 5)
- Project files (scp files) suddenly appear on desktop (version 5)
- Stored procedures using full-text queries fail to synchronize
- When does the synchronization process rebuild tables?
- Unable to begin a distributed transaction error synchronizing databases
- User or role already exists in the current database error
- Clarification on the "Ignore Triggers" option in the SQL Compare command-line version
- Common table expressions returned by User Defined Functions may fail
- Can't compare encrypted text in SQL Server 2005, 2008
- Exception when connecting to SQL Server (Version 6)
- SQL comparison and synchronization automation capabilities
- Error logging in SQL Compare 6.2
- Using Team Foundation Server source control system
- Creating a SQL 2000 compatible database from a SQL 2005 database using SQL Compare
- Support for numbered stored procedures
- SQL Compare 7 Object-level restore
- Index was outside the bounds of the array error when registering databases X64
- HTML reports are generated for identical comparisons
- Comparison may seem slower than necessary
- DEFAULT_SCHEMA clause cannot be used with a Windows group
- SQL Compare may drop and create CLR assemblies
- SDK activation prompt may appear when starting a new SQL Compare project
- Column does not allow nulls. INSERT fails
- This SQL Server has been optimized for x concurrent queries.
- SQL Compare comparison differences
- Using SQL Server Management Studio Express as a query editor
- How to force SQL Compare and SQL Data Compare to use an Encrypted Connection
- Invalid SQL when synchronizing an index to a scripts folder when data compression is specified
- Tips
- Changes to distribution of command line
- Common error messages
- Copying the structure of a database
- Permissions required to use SQL Compare
- Rollback on script failure or cancellation
- Errors in scripts folders
- Logging and log files
all SQL products
- Compatibility of Red Gate tools in 64-bit environments
- Application has encountered an error and needs to close
- Error message after installing SQL Toolbelt - The description for Event ID ( 1 ) in Source ( nview_info ) cannot be found.
- Changing the temporary directory used by the installer
- Toolbelt Installer "hanging" while "scanning volumes"
- Login failing with "trusted SQL Server connection" error when using RunAs
all products
- Some Red Gate products identified as containing a trojan by Anti-Virus software
- Activation may fail with Unknown Error -1
- Product uses web help although a CHM file is available locally
- Argument exception resulting from missing environment variable
- Check for updates may fail when used through proxies
- 'Unidentified Publisher' error when repairing or uninstalling
- Licensing activates product as standard edition
- Moving Red Gate software products to another machine
- Red Gate tools log locations
- The application UI opening slowly when there is no internet access
SQL Compare
- Activating your products
- Activating your products
- Getting help offline
- Release notes - version 9.5 EAP
- Introducing SQL Compare 8: FAQs for SQL Compare 7 users
- Release notes - version 10.xx
- SQL Compare release notes - version 9.xx
all SQL products
all products
- Red Gate product acknowledgements
- Activating your products
- Activating your products
- Red Gate bundle history
- Check for updates
- Troubleshooting Check for Updates errors
- Current versions
- Deactivating your products
- Installing Red Gate products from the .msi file
- Requesting additional activations
- Serial numbers for bundles
- Reactivating using a different serial number
- Extending your trial
- Finding your serial numbers
- Moving a serial number from one computer to another
- No response received for manual activation
- Licensing and activation resources
- Licensing and activation resources
- Troubleshooting licensing and activation errors
- Licensing and activation FAQs
- Red Gate tools log file locations
- Download old versions of products
- Download product prerequisites & utilities
- Support & upgrades
- Upgrading your software
- Upgrading FAQs

