views:

455

answers:

4

I recently found the following article:

http://www.tsqltutorials.com/with-common-table-expressions.php

The article doesn't list which version of SQL server this became available in. Does this work in SQL Server 2000 and if not what is the earliest version that it is usable in?

Update: I did try a quick test and the test doesn't work. I'm asking that it doesn't work in SS2000 to ensure it isn't me or my setup.

+9  A: 

Common table expressions were introduced in SQL Server 2005.

http://www.simple-talk.com/sql/sql-server-2005/sql-server-2005-common-table-expressions/

Aaron Alton
Thank you, for the link and the answer.
Frank V
Also note that if you, for whatever reason, have to specify a lower compatibility mode for your database, then syntax available in latter versions will become unavailable. For instance, SQL Server 2005 doesn't like the old ansi-join syntax, and if you set the compatibility mode of your database to 8.0 (SQL Server 2000), then WITH-statements also becomes unavailable for that database.
Lasse V. Karlsen
@Lasse - I used to thing the same thing, but such is not the case. Compatability level doesn't affect syntax availability. Check it out:CREATE DATABASE [CompatabilityTest]GOEXEC dbo.sp_dbcmptlevel @dbname=N'CompatabilityTest', @new_cmptlevel=80GOUSE CompatabilityTestGO;WITH MyCTE AS (SELECT name FROM sys.databases)SELECT * FROM MyCTE
Aaron Alton
+1  A: 

If you need that functionality, it might be a good way to convince management to upgrade.

HLGEM
We have upgraded.... except on the project I am on. We are stuck due to the heavy usage of DTS. We don't have the resources to change the DTS packages out. It's stupid.
Frank V
Ah then read this article:http://www.simple-talk.com/sql/sql-server-2005/dts-to-ssis-migration/There are tools you can download to run and maintain DTS packages from SQL Server 2005 or 2008. We use them as we too have hundreds of DTS packages that are only slowly being converted to SSIS.
HLGEM
+1  A: 

@Aaron - you said, "Compatability level doesn't affect syntax availability." Actually, I think that's only true sometimes. What I've seen using a compatibility level 80 (SQL Server 2000 mode) database in SQL Server 2005 is that some syntax that is new to SQL Server 2005 is available, other is not. For instance, as you rightly noted, CTEs are available. However, the PIVOT operator is not. So (in my little experience) it seems to depend on the particular syntactical feature. (I would post this under the top answer, but apparently this site by design requires that you get 50 reputation points before you can respond in the thread-appropriate location.)

John Schroeder
A: 

Be careful with compatibility levels and CTE, they're a little odd.

SELECT 1 a
WITH A AS (SELECT 1 a)
    SELECT 1 from A

Runs on SQL Server 2005 compatibility level 80 (SQL Server 2000), but not compatibility level 90 (SQL Server 2005). Compatibility level 90 correctly requires a semicolon before the with.

As noted before SQL Server 2000 doesn't support this at all.

Simon D