SQL Server Error 935 occurs when there is a mismatch between the script level of a database object and the compatibility level of the database itself. To fix this, you must update the database compatibility level to match the version of the SQL Server instance or the script that created the object. This is typically achieved by running the ALTER DATABASE command to raise the compatibility level to the current version of your SQL Server engine.

What You'll Need

  • SQL Server Management Studio (SSMS) or another SQL query editor.
  • Administrative privileges (sysadmin or db_owner role) on the affected database.
  • Knowledge of the current SQL Server version installed on the host machine.

Step-by-Step

  1. Identify the Target Database: Open SQL Server Management Studio and connect to your instance. In the Object Explorer, locate the database that is triggering Error 935. Note the exact name of the database, as it will be required for the T-SQL commands.
  2. Check the Current Compatibility Level: Before making changes, determine the current compatibility level of the database. Open a New Query window and execute the following command:
    SELECT name, compatibility_level FROM sys.databases WHERE name = 'YourDatabaseName';
    Replace YourDatabaseName with your actual database name. The result will be a number (e.g., 100, 110, 130, 140, 150).
  3. Determine the Required Level: Compare the number found in step 2 with the version of your SQL Server instance. For example, SQL Server 2016 uses 130, 2017 uses 140, 2019 uses 150, and 2022 uses 160. If the database was migrated from an older server, it likely retains the old compatibility level, which causes the script level error when new features or scripts are introduced.
  4. Execute the Compatibility Update: To resolve the error, run the ALTER DATABASE command. Use the following syntax:
    ALTER DATABASE YourDatabaseName SET COMPATIBILITY_LEVEL = 150;
    (Replace 150 with the number corresponding to your current SQL Server version).
  5. Verify the Change: Re-run the selection query from step 2 to ensure the compatibility_level has been updated to the new value.
  6. Retry the Original Operation: Attempt to run the script or perform the action that originally triggered Error 935. The system should now recognize the script level as compatible with the database settings.

Common Mistakes

One of the most frequent mistakes is attempting to set the compatibility level to a version higher than what the physical SQL Server instance supports. For instance, if you are running SQL Server 2017, you cannot set the compatibility level to 150 (SQL Server 2019). Doing so will result in a different error stating that the level is not supported. Always verify your server version via SELECT @@VERSION before applying the change.

Another common error is neglecting to check for dependencies. While changing the compatibility level usually resolves Error 935, it can occasionally change the behavior of the T-SQL query optimizer. Some legacy queries that were tuned for an older version of SQL Server might perform differently (sometimes slower) after the level is increased. It is a mistake to apply this change to a massive production environment without first testing the impact on critical queries in a staging environment.

Users also frequently forget to ensure they have the correct permissions. If you are logged in as a limited user, the ALTER DATABASE command will fail with a permission denied error. You must be a member of the sysadmin fixed server role or the db_owner fixed database role to modify these settings.

Tips

When managing multiple databases across different environments, it is helpful to create a script that checks all databases for outdated compatibility levels. This allows you to proactively fix potential Error 935 issues before they disrupt your deployment pipeline. Use a cursor or a dynamic SQL loop to scan sys.databases and flag any database whose compatibility level is lower than the server's maximum supported version.

If you are using Azure SQL Database, the compatibility level is handled slightly differently than on-premises instances. Always check the Azure portal or use the sys.database_compatibility_levels view to ensure your cloud database is aligned with the latest features. Keeping the level current ensures you have access to the latest performance improvements and T-SQL syntax enhancements.

Document every change to the compatibility level in your version control or change management log. Because this setting affects how the database engine parses scripts and optimizes queries, knowing exactly when the level was changed helps in diagnosing performance regressions that might appear days or weeks after the fix.

Troubleshooting

If the ALTER DATABASE command completes successfully but you still encounter Error 935, the issue may be related to a specific object that was created with a hard-coded script level requirement. In this case, try dropping and recreating the specific object (such as a stored procedure or view) while the database is already at the higher compatibility level. This forces the object to be compiled using the current engine rules.

If you receive a "Database is in use" error when trying to change the compatibility level, it is usually because there are active connections preventing the metadata change. While SET COMPATIBILITY_LEVEL typically does not require exclusive access, some environments may behave differently. You can try setting the database to SINGLE_USER mode to kick out other connections, applying the change, and then setting it back to MULTI_USER. Be warned: this will disconnect all current users and can cause application timeouts.

In rare cases, the error may persist if the database is part of an Availability Group or is being mirrored. Ensure that the change is applied to the primary replica. The compatibility level is a database-level setting and should propagate to secondaries, but if there is a synchronization lag or a configuration mismatch at the instance level, you may see inconsistent behavior. Restarting the SQL Server service on the secondary nodes can sometimes clear cached metadata that is causing the error to persist.

Frequently Asked Questions

Does changing the compatibility level delete any data?

No, changing the compatibility level does not delete or modify any data stored in your tables. It only changes how the SQL Server engine interprets T-SQL syntax and how the query optimizer generates execution plans.

Can I lower the compatibility level if I encounter performance issues?

Yes, you can lower the compatibility level back to a previous version using the same ALTER DATABASE command. This is often done as a temporary rollback if a specific query begins to perform poorly after an upgrade.

What is the difference between the SQL Server version and the compatibility level?

The SQL Server version refers to the actual software installed on the hardware (e.g., SQL Server 2019). The compatibility level is a setting within a specific database that tells the engine to act like an older version to maintain backward compatibility for legacy code.

Will this fix the error for all objects in the database?

Yes, updating the database compatibility level applies globally to all objects within that specific database. Any script that was failing due to a level mismatch should now execute correctly.

Is it safe to run this command on a production server?

While generally safe, it is recommended to test the change in a development environment first. Because the query optimizer changes between levels, some execution plans may change, which could potentially impact the performance of highly complex queries.

Why did this error happen after a server migration?

When you restore a database backup from an older server to a newer one, the database retains the compatibility level of the original server. If you then try to use new SQL features or scripts designed for the newer version, the engine detects the mismatch and throws Error 935.