xSQL Schema Comparison and Synchronization SDK 3.0

xSQL Object Command Line (xSQLObjectCmd.exe)

Overview

xSQL Object Command Line utility allows for comparing the schema of SQL Server databases via the command line. With the xSQL Object Command Line you can:


Comparison options are specified via an xml configuration file. Additional command line switches are used for other settings such as encoding, file location etc.

Runtime files

The following files are required to run the Command Line:



Command Line argument and options

The command line options control the general behavior of the xSQL Object Command Line utility such as the mode of operation (quiet), file encoding, the location of the log and error files. Each option has a short and a long form. For example /q and /quiet specify the "quiet" mode which suppresses all the comparison messages from being displayed on the screen or saved to a file. The value of the option should be separated from the option switch using a colon, not a space. For example /l:c:\compare.log saves all comparison messages to the output file c:\compare.log.

The schema comparison options and preferences on the other hand are specified via an xml configuration file. When the same command line option exists for an xml-specified parameter, the command line value has priority over the one specified in the xml file.


xSQL Object Command Line utility supports the following argument and options:

Argument Description
Xml File The xml configuration file that contains the comparison preferences.
Option Description
? Displays the help screen.
/v[alidate] Performs validation of the xml configuration file. No comparison is performed.
/l[ogfile]:filename Specifies the name of log file where comparison messages should be saved.
/t /logtype:File|WindowsLog Indicates the type of logging for schema errors. Supported options are disk file (File) or Windows log (WindowsLog).
/e[rror]:filename Specifies the name of the error log. Depending on the /t option, this can indicate a disk file or Windows log.
  • If the /t:WindowsLog is specified, the /e option indicates a Windows log. If the specified log is one of the Windows built-in logs, the Command Line uses that log for all the comparison errors; otherwise it creates a new one.
    The following example logs comparison errors to Windows Application log:

    /t:WindowsLog /e:Application

  • If the /t:File is specified, the /e option indicates a disk file.
Note: Use /t:WindowsLog carefully. The Command Line creates a new Windows log if the log name specified via /e switch does not match a built-in Windows log.
/o[verride] Overrides all output files.
/q[uiet] Indicates the quite mode, which suppresses all comparison messages from being displayed on the screen or saved to the log file.
/en[coding] Specifies the encoding of the output files. The supported options are UTF7, UTF8, ASCII, Unicode and ANSI. The default encoding is ANSI.


Xml configuration file


Overview

Due to the large number of arguments, xSQL Object Command Line utility uses an xml configuration file for all the comparison options.

The Xml configuration file allows you to:

The xml file groups the comparison parameters and options into the following sections:

Section Description
Database
   <LeftDatabase>
   <RightDatabase>
Contains the databases to compare. The Command Line requires two sections, one for each database. These are referred to as the “left” and the “right” database.
Schema Objects
   <SchemaObjects>
Contains the type of database objects to compare such as tables, views, stored procedures.
Schema Options
   <SchemaOptions>
Contains comparison options.
Command Line Settings
   <CommandLineSettings>
Specifies other settings such as the file name of the synchronization script, synchronization log, error log, file encoding.
Schema Filter
   <SchemaFilterValueSet>
Specifies schema filters that allow you to include or exclude specific database objects.

A simple Xml configuration file has the following structure:

<?xml version="1.0" encoding="utf-8" ?>
<CommandLineParameters xmlns="http://www.xsqlsoftware.com/schemas/xSQLObjectCmdSchema.xsd">
        <LeftDatabase>
                <SqlServer>(local)</SqlServer>
                <DatabaseName>DB_Prod</DatabaseName>
                <TrustedConnection>true</TrustedConnection>
        </LeftDatabase>
        <RightDatabase>
                <SqlServer>(local)</SqlServer>
                <DatabaseName>DB_Staging</DatabaseName>
                <TrustedConnection>true</TrustedConnection>
        </RightDatabase>
        <SchemaObjects>
                <Object Type="DefaultObjects"></Object>
        </SchemaObjects>
        <SchemaOptions>
                <Option>DefaultOptions</Option>
        </SchemaOptions>
        <CommandLineSettings>
                <SchemaScriptFile>CmpScript.sql</SchemaScriptFile>
                <SchemaLogFile>CmpLog.txt</SchemaLogFile>
                <ErrorLogType>File</ErrorLogType>
                <ErrorLogName>CmpErr.txt</ErrorLogName>
                <CompareSchema Direction="LeftAsRight" Synchronize="false"></CompareSchema>
        </CommandLineSettings>
</CommandLineParameters>
      
The <LeftDatabase> and <RightDatabase> elements specify the database on the "left" and the "right" side of the comparison respectively. In this example the databases DB_Prod and DB_Staging are located on the "(local)" SQL Server and the connection to SQL Server is Trusted Connection.
<Objects> elements contains the type of database objects to compare. The value DefaultObjects specified with the attribute "Type" indicates the default selection of database objects.
<SchemaOptions> tag determines the schema comparison options. The value DefaultOptions indicates the default options.
<CommandLineSettings> element contains:

<ErrorFile>: Specifies the name of the file that will contain schema errors.
<SchemaScriptFile>: The file name on which synchronization script will be saved.
<SchemaLogFile>: The file name on which the synchronization logs will saved.
<CompareSchema>: Indicates the direction of the comparison as LeftToRight or RightToLeft. In a left-to-right comparison, the Command Line makes the schema of the database specified by <LeftDatabase> element the same as the schema of the database specified by <RightDatabase> element.
The attribute Synchronize determines whether the Command Line should stop with the generation of the schema differences script or whether it should execute the script to synchronize databases. A false value indicates that only the synchronization script should be generated.

The following command invokes the Command Line with the Xml configuration file (assuming that the file is named simpleXml.xml)

        xSQLObjectCmd simpleXml.xml
      

Examples


xSQL Object Command Line sample xml files are located in the \Samples\CommandLines\ObjectCmd subfolder under the main installation folder of xSQL Bundle.



Xml Configuration File Schema


<CommandLineParameters>
        <LeftDatabase>
                <SqlServer>
                <DatabaseName>
                <Username>
                <Password>
                <TrustedConnection>
                <ConnectionTimeout>
                <CommandTimeout>
                <SnapshotFile>
                <CreateSnapshot>
        </LeftDatabase>
        <RightDatabase>
                <SqlServer>
                <DatabaseName>
                <Username>
                <Password>
                <TrustedConnection>
                <ConnectionTimeout>
                <CommandTimeout>
                <SnapshotFile>
                <CreateSnapshot>
        </RightDatabase>
        <SchemaFilterValueSets>
                <FilterValueSet Name>
                        <Value MatchType>
                </FilterValueSet>
        </SchemaFilterValueSets>
        <SchemaObjects>
                <Object Type Action FilterValueSet FilterValidationType>
        </SchemaObjects>
        <SchemaOptions>
                <Option>
        </SchemaOptions>
        <CommandLineSettings>
                <LogFile>
                <ErrorFile>
                <SchemaScriptFile>
                <SchemaLogFile>
                <FileEncoding>
                <CompareSchema Direction Synchronize>
        </CommandLineSettings>
</ CommandLineParameters >  
      

Element Description
CommandLineParameters Root element.
LeftDatabase, RightDatabase The elements that define the left and the right database.
SqlServer The Sql Server that contains the database.
DatabaseName The database name.
Username The Sql Server login name.
Password The Sql Server password.
TrustedConnection The Sql server authentication type. true if the Windows authentication (trusted connection) should be used to connect to Sql Server; otherwise false. If not specified the default authentication method is Windows authentication.
ConnectionTimeout The connection timeout in seconds, which is the amount of time that Command Line waits to establish a connection with the Sql server before generating an error. The default timeout is 60 seconds.
CommandTimeout The command timeout in seconds, which is the amount of time that Command Line waits to execute a Sql statement before generating an error. The default timeout is 20 minutes.
SnapshotFile Saves a snapshot of the database to the specified file. This requires that <CreateSnapshot> element to have the true.
CreateSnapshot Forces Command Line to save a snapshot of the database before starting the comparison. The element <SnapshotFile> is required and must contain a valid file name.
SchemaFilterValueSets Starts the schema filters section.
FilterValueSet A schema filter element identified by the attribute Name.
Value A schema filter criteria.
  • StartWith:  Is evaluated to true if the object name starts with the specified pattern
  • DoNotStartWith:  Is evaluated to true if the object name does not start with the specified pattern
  • EndWith:  Is evaluated to true if the object name ends with the specified pattern
  • DoNotEndWith:  Is evaluated to true if the object name does not end with the specified pattern
  • Contain:  Is evaluated to true if the object name contains the specified pattern
  • DoNotContain:  Is evaluated to true if the object name does not contain the specified pattern
  • Equal:  Is evaluated to true if the object name is equal to the specified pattern
  • NotEqual:  Is evaluated to true if the object name is not equal to the specified pattern
  • RegularExpression:  The filter defines a regular expression pattern and it is evaluated to true if the object name matches the pattern
SchemaObjects Starts the section that defines the types of schema objects to include in or exclude from the comparison.
Object A database object type to include or exclude from the comparison. The object element contains the following attributes:
  • Type:  One of the following database object types:
AllObjects
DefaultObjects

Table
View
StoredProcedure
UserDefinedDataType
UserDefinedFunction
User
Role
PrimaryKey
UniqueConstraint
Index
ForeignKey
DefaultConstraint
CheckConstraint
Trigger
Default
Rule
Permission
ExtendedProperty
FullTextCatalog
Schema
Certificate
AsymmetricKey
SymmetricKey
PartitionFunction
PartitionScheme
Synonym
ManagedAssembly
UserDefinedAggregate
UserDefinedType
XMLSchemaCollection
ServiceMessageType
ServiceContract
ServiceQueue
ServiceRoute
ServiceInstance
RemoteServiceBinding
ServicePriority
DatabaseTrigger
ServerTrigger

  • Action:  Indicates whether an object type is included in or excluded from the comparison:
Include
Exclude>
  • FilterValueSet:  Specifies a schema filter for this type of object. The schema filter referenced by these attributes must exist and should be defined with aFilterValueSet element.
  • FilterValidationType:  Defines how the schema filter referenced by FilterValueSet attribute is validated. The object can either meet all or one of the filter criteria:
AllCriteria
AtLeastOneCriteria
SchemaOptions Starts the section that defines the schema options.
Option One of the following schema options:
DefaultOptions

AlterAssemblyWithDropCreate
BindDefaultsColumns
BindDefaultsDataTypes
BindRulesColumns
BindRulesDataTypes
CompareColumnsOrdinal
CompareDefaultConstName
CreateDefaultsFutureOnly
CreateRulesFutureOnly
GenerateTransaction
IgnoreANSINulls
IgnoreCharCase
IgnoreCollation
IgnoreQuotedIdentifier
IgnoreSpaces
IgnoreFunctionsComments
IgnoreProcedureComments
IgnoreTriggersComments
IgnoreViewsComments
IncludeEncryptedObjects
IncludeIndexFillFactor
IncludeIndexPadding
IncludeIndexRecompute
IncludeInvalidDependenies
IncludeObjectsFileGroup
OrderFunctionsByDependency
OrderProceduresByDependency
OrderViewsByDependency
PerformFileGroupTranslation
RaiseObjectsEvents
CompareDataCompression
CompareChangeTracking
CommandLineSettings Contains additional settings used by the Command Line.
LogFile Logs the schema comparison messages to the specified file.
ErrorLogType Determines the type of logging of the schema comparison errors. This can be File or WindowsLog.
ErrorLogName Logs the errors that might occur during the comparison or synchronization to the specified file or Windows log.
SchemaScriptFile Saves the synchronization script to the specified file.
SchemaLogFile Saves the synchronization logs to the specified file.
FileEncoding Specifies the encoding of all output files. This element can have one of these values: Unicode, UTF7, UTF8, ASCII, ANSI. The default encoding is ANSI.
CompareSchema Contains the following attributes:
  • Direction:  Indicates whether the left database should be synchronized to match the structure of the right database or vice versa. Specify the LeftAsRight or RightAsLeft values.
  • Synchronize:  If true the synchronization script is executed at the end of the comparison; otherwise the script is only generated.


Error Codes

The Command Line utility returns the following error codes:

Error Code Description
0 No error
1 Invalid Command Line options
2 Invalid Xml file
4 The path of the output files is invalid
8 Failed to compare databases.
16 Failed to execute the synchronization script.