When selecting data or performing query on Microosft SQL Server 2008 or SQL Server 2005 database, the following error message or failure may be returned:
[Microsoft][ODBC SQL Server Driver][SQL Server][Msg 4147, Level 15, State 1, Line 4] The query uses non-ANSI outer join operators (”*=” or “=*”). To run this query without modification, please set the compatibility level for current database to 80, using the SET COMPATIBILITY_LEVEL option of ALTER DATABASE. It is strongly recommended to rewrite the query using ANSI outer join operators (LEFT OUTER JOIN, RIGHT OUTER JOIN). In the future versions
of SQL Server, non-ANSI join operators will not be supported even in backward-compatibility modes.
or,
Msg 4147, Level 15, State 1, Line 3
The query uses non-ANSI outer join operators (”*=” or “=*”). To run this query without modification, please set the compatibility level for current database to 80 or lower, using stored procedure sp_dbcmptlevel. It is strongly recommended to rewrite the query using ANSI outer join operators (LEFT OUTER JOIN, RIGHT OUTER JOIN). In the future versions of SQL Server, non-ANSI join operators will not be supported even in backward-compatibility modes.
The error happens because Microsoft has dropped support for and eliminated old-style *= and =* outer join operators.
In order to fix the SQL Server error above, DB administrator has to set the database compatibility level to 80, which equivalent with SQL Server 2000 as a temporary workaround. Of course, the best solution is to modify the SQL statements or queries code to remove the old-style join operators with current standard SQL join syntax.
For example, SQL query below is not valid:
SELECT o.name FROM sys.objects o, sys.views v WHERE o.object_id *= v.object_id;
And should be replaced with the following code:
SELECT o.name FROM sys.objects o LEFT JOIN sys.views v ON o.object_id = v.object_id;
Or the following abbreviated inner join statement:
SELECT o.name FROM sys.objects o, sys.views v WHERE o.object_id = v.object_id;
For administrator who can’t change the code, the easiest workaround is to revert the compatibility level of the database in order to provide the backward compatibility to old-style joins. sp_dbcmptlevel stored procedures (deprecated) or Transact-SQL can be used to set certain (partially, not all) database behaviors to be compatible with the earlier version of SQL Server. This
MSDN article provides overview on what behaviors are changed on each compatibility level.
Version of SQL Server that can be reverted to can be one of the following:
60 = SQL Server 6.0
65 = SQL Server 6.5
70 = SQL Server 7.0
80 = SQL Server 2000
90 = SQL Server 2005
100 = SQL Server 2008
Note: Compatibility level 60, 65, and 70 no longer available in SQL Server 2008. And future version of SQL Server will support only two (2) prior version of backward compatibility. A database containing an indexed view cannot be changed to a compatibility level lower than 80. Do also take note that when a database is set to backward-compatibility mode, some of the new functionalities may be lost, such as SQL CLR support and SSMS diagrams for the database. Beside, the compatibility mode affects behaviors only for the specified database, not for the entire server.
Steps to Change and Set Compatibility Level of A Database in SQL Server
The following SQL commands can be issued in SQL Server Management Studio Query window. Remember to execute Go after each command.
1. Optional: Set the database to single user access mode:
ALTER DATABASE database_name SET SINGLE_USER;
2. Set the database compatibility level to one of the earlier version stated above:
Transact-SQL MethodALTER DATABASE database_name SET COMPATIBILITY_LEVEL = { 80 90 100 }
For example,
ALTER DATABASE my_db SET COMPATIBILITY_LEVEL = 80
sp_dbcmptlevel Stored Procedure MethodEXEC sp_dbcmptlevel database_name, compatibility_level value;
For example,
EXEC sp_dbcmptlevel my_db, 90;
3. Optional: Put back the database in multiuser access mode:
ALTER DATABASE database_name SET MULTI_USER;