Zum Inhalt springen

Diese Seite ist noch nicht auf Deutsch verfügbar und wird daher auf Englisch angezeigt.

SQL Environment Comparison

How to compare two SQL Server environments layer by layer: instance settings, database options, schema, row counts, jobs and logins, with queries for each and how to report the differences.

"Why does this work in test but not in production?" usually ends with someone discovering a difference between the two environments. Comparing environments deliberately, layer by layer, finds those differences before they cause trouble. It is useful before a migration, before a big release, when diagnosing a problem that only happens in one place, or simply as a regular health check.

This guide works through each layer of a SQL Server environment, with a query or approach for comparing it, and then covers how to present the differences so people can act on them.

Work in layers

Compare from the outside in. A difference at an outer layer, such as the SQL Server version, can explain differences further in, so it is worth checking first:

1. Instance: version, edition, configuration.

2. Databases: options and settings.

3. Schema: tables, indexes and code.

4. Data: row counts and key reference data.

5. Operations: jobs, logins and linked servers.

Instance

Start with version, edition and the settings that affect behaviour:

```sql

SELECT SERVERPROPERTY('ProductVersion') AS version,

SERVERPROPERTY('ProductUpdateLevel') AS update_level,

SERVERPROPERTY('Edition') AS edition,

SERVERPROPERTY('Collation') AS server_collation;

SELECT name, value_in_use

FROM sys.configurations

ORDER BY name;

```

Run both on each environment and compare the output side by side. Edition differences matter more than people expect: some features and optimisations are only available in Enterprise Edition, so a query can behave differently on Standard even with identical settings.

Databases

Database options affect locking, recovery and the optimiser. `sys.databases` holds most of them:

```sql

SELECT name, compatibility_level, collation_name, recovery_model_desc,

is_read_committed_snapshot_on, snapshot_isolation_state_desc,

is_auto_create_stats_on, is_auto_update_stats_on, is_auto_update_stats_async_on,

is_query_store_on, page_verify_option_desc

FROM sys.databases

WHERE database_id > 4

ORDER BY name;

```

Also compare database-scoped configuration from `sys.database_scoped_configurations` in each database. Settings such as MAXDOP and legacy cardinality estimation can be set per database and override the instance.

Schema

For tables, indexes and code, a schema comparison tool such as SQL Server Data Tools or `sqlpackage` gives the most complete result, listing every object that is missing, extra or different. For a quick check without a tool, compare object counts by type, and hashes of code object definitions, between the two environments. Our guide to [SQL schema comparison](/guides/sql-schema-comparison) goes into the tools in more detail.

Data

You rarely want to compare all the data, but row counts per table are cheap and revealing:

```sql

SELECT SCHEMA_NAME(t.schema_id) + '.' + t.name AS table_name, SUM(p.rows) AS row_count

FROM sys.tables t

JOIN sys.partitions p ON p.object_id = t.object_id AND p.index_id IN (0, 1)

GROUP BY t.schema_id, t.name

ORDER BY table_name;

```

`sys.partitions` row counts are close enough for comparison and avoid scanning every table. For reference and lookup tables, which should match exactly, compare checksums of the content as well as counts.

Operations

The last layer is the operational set-up around the databases:

  • **SQL Agent jobs**: which exist, whether they are enabled and their schedules, from `msdb.dbo.sysjobs` and `sysjobschedules`.
  • **Logins and server roles**: which logins exist and who is in sysadmin and other roles.
  • **Linked servers**: which exist and where they point, from `sys.servers`.
  • **Credentials and proxies** used by jobs.

Differences here explain many "it works in one place only" problems, such as a job that exists only in production or a linked server that points somewhere different.

Automate the comparison

Running these queries by hand on two servers is fine once. For a regular check, script it. A PowerShell loop, or a Central Management Server query, can run each layer's query against every environment and save the results to a table, with the server name and date on every row. Comparing environments then becomes a set of simple joins between rows from different servers, and comparing this week with last week shows exactly what changed. Keep the scripts in source control alongside your other operational tooling, so the comparison itself stays consistent over time.

Present the differences

A raw list of hundreds of differences isn't useful. Sort them into three groups:

  • **Expected differences** that are allowed by your standards, such as logins, data volume or backup schedules. Note them once and filter them out of future reports.
  • **Differences that need fixing**, such as missing indexes, different compatibility levels or code that doesn't match source control.
  • **Differences that need a decision**, where it isn't clear which side is right.

For each one, say which environment is correct and who will fix it. Repeating the comparison regularly, and comparing the results with last time, turns it from a one-off audit into ongoing assurance.

Troubleshooting Scenarios

A query behaves differently in two environments

Compare edition, version, compatibility level, database-scoped configuration and statistics settings before looking at the query itself.

The comparison lists hundreds of differences

Filter out the expected ones, such as logins and data volumes, and group the rest by layer.

Row counts differ slightly between environments

That is expected for transactional tables. Focus on reference tables and tables that should be static.

A job exists in production but not in test

Decide whether test needs it. If it does, add it to source control and deploy it to both.

Where SQL Cockpit fits

Comparing environments means looking at the same objects and settings across several servers at once. SQL Cockpit brings object search and schema context across connected SQL Server environments into one workspace, which helps when tracking down where an object or table differs. The comparison queries and tools in this guide still provide the detailed, layer-by-layer picture.

FAQ

How do I compare two SQL Server environments?

Work layer by layer: instance version and configuration, database options, schema, row counts, then jobs, logins and linked servers.

How do I compare SQL Server configuration settings?

Query sys.configurations and SERVERPROPERTY on each instance and compare the results side by side.

How do I compare row counts between databases?

Sum rows from sys.partitions for each table's heap or clustered index, which is fast and avoids scanning the tables.

Why does SQL Server edition matter when comparing environments?

Some features and optimisations differ between editions, so the same query can behave differently on Standard and Enterprise.

What should I do with the differences I find?

Sort them into expected, needing a fix and needing a decision, and assign each fix to an owner.

Semantic Keywords

  • SQL environment comparison
  • schema drift
  • table synchronisation
  • environment comparison
  • migration orchestration
  • SQL Server monitoring
  • SQL Agent monitoring
  • runtime analysis
  • runtime baselines
  • runtime variance
  • SQL Server operations
  • database telemetry
  • historical analysis
  • operational dashboards
  • cross-server monitoring

Suggested Related Articles