tags:

views:

153

answers:

3

The developer environment db server is SqlServer 2005 (developer edition)

Is there any way to make sure my SQL Queries will run in SqlServer 2000?

This database is set to Compatibility level "SQL Server 2000 (80)" but some queries that run without problems in the development system can not run in the Test Server (SqlServer).

(The problems seems to be in subqueries)

+2  A: 

Compatibility levels are designed to work the opposite way - to allow an older version of T-SQL code to work without modifications on a newer version of SQL Server. The changes typically involve T-SQL syntax and reserved words, and it's possible to use SQL Server 2005 features such as INCLUDED columns in indexes on a database in Compatibility Level 80. However, you can't use 2005 T-SQL features such as CROSS APPLY.

Your best option is to develop/test all your code against a SQL Server 2000 instance. Note that you can use 2005's Management Studio to connect to the SQL Server 2000 instance, so you don't have to go backwards with regards to tools.

Jim McLeod
"Compatibility levels are designed to work the opposite way" - no, that's exactly what he was saying.
Timothy Khouri
I mean they're designed to work the opposite way to how he's trying to use them. In this case, he's using Compatibility Level 80 to test 2005 code against 2000, whereas CL80 is supposed to be used to run 2000 code on a 2005 system.
Jim McLeod
A: 

Problem solved:

In correlated subqueries you have to (in SQL2000) explicitly define the external field.

SQL2005:

SELECT * FROM Loans WHERE EXISTS (SELECT * FROM Collaterals WHERE COLLATERAL_LOAN=LOAN_NUMBER)

SQL2000:

SELECT * FROM Loans WHERE EXISTS (SELECT * FROM Collaterals WHERE COLLATERAL_LOAN=Loans.LOAN_NUMBER)
pkario
A: 

You should always explicitly define all fields, otherwise you will not get an error when you make a mistake and write

SELECT * FROM Loans WHERE EXISTS (SELECT * FROM Collaterals WHERE LOAN_NUMBER=Loans.LOAN_NUMBER)

If Collaterals-table doesn't have column LOAN_NUMBER, the Loans-table is used instead.

Kaniu