在SQL2005/SQL2008中CTE用法差异
--SQL2008中执行成功
SELECT @@version /* Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyright (c) Microsoft Corporation Data Center Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: ) (Hypervisor) */ IF 1>0 WITH c AS ( SELECT 1 AS ID) SELECT * FROM c
--SQL2005中执行相同语句时出错
SELECT @@VERSION /* Microsoft SQL Server 2005 - 9.00.5000.00 (Intel X86) Dec 10 2010 10:56:29 Copyright (c) 1988-2005 Microsoft Corporation Enterprise Edition on Windows NT 6.1 (Build 7600: ) */ IF 1>0 WITH c AS ( SELECT 1 AS ID) SELECT * FROM c /* 消息 319,级别 15,状态 1,第 2 行 关键字 'with' 附近有语法错误。如果此语句是公用表表达式或 xmlnamespaces 子句,那么前一个语句必须以分号结尾。 */ --改为 IF 1>0 BEGIN WITH c AS ( SELECT 1 AS ID) SELECT * FROM c END