0

我有一个查询,我认为使用交叉应用会更有效。我已将语法从加入更改为交叉应用,但出现一般语法错误:

消息 170,级别 15,状态 1,第 14 行第 14 行:“应用”附近的语法不正确。消息 156,级别 15,状态 1,第 21 行关键字“as”附近的语法不正确。

这是我第一次使用交叉应用,我没有看到语法错误(至少在与我找到的示例进行比较时)。这是代码:

Select v.PC_ID
, v.PC_Name
, v.LOB
, v.REG
, v.DST
, v.PC
, d.Effective_Date as DateOfLastInc
, p.TotalPriceIncPct
, p.last_update_by
, d.PlanEffective_Date
, p2.PlanTotalPriceIncPct
--INTO #temp1
from v_PC_Profile v
cross apply (
    Select pc_id
, Effective_Date=max(Effective_Date)
, PlanEffective_Date=max(PlanEffective_Date) 
from dbo.Price_Inc_PC_Details
where pc_id = v.pc_id
group by pc_id
) as d --on d.PC_ID=v.PC_ID
left join dbo.Calendar c on c.FULL_DATE=d.Effective_Date 
left join dbo.Price_Inc_PC_Details p on d.PC_ID=p.PC_ID and d.Effective_Date=p.Effective_Date
left join dbo.Price_Inc_PC_Details p2 on d.PC_ID=p2.PC_ID and         d.PlanEffective_Date=p2.PlanEffective_Date --added by ajl 1/15/2013

WHERE segment NOT IN ('Closed', 'Overhead')
and segment not like '%closed%'
and Close_Date is NULL

group by v.PC_ID
, v.PC_Name
, v.PC
, d.Effective_Date
, p.TotalPriceIncPct
, d.PlanEffective_Date
, p2.PlanTotalPriceIncPct
, v.REG
, v.DST
, v.LOB
, p.last_update_by
, p.PlanEffective_Date
, p.PlanTotalPriceIncPct
order by v.PC_ID

任何帮助将不胜感激!

标清

4

3 回答 3

8

过去,在更高版本的 SQL Server 中兼容级别为 80(又名 SQL Server 2000)的数据库会为 UDF 提供此错误。

语法看起来不错,在我的 SQL Server 2012 上解析也不错。我在兼容级别为 80 的 SQL Server 2008 R2 上创建了一个数据库,但它也可以解析。

请参阅MSDN 上的“使用 APPLY”

要使用 APPLY,数据库兼容级别必须至少为 90。

于 2014-01-30T15:35:37.247 回答
1

事实证明它是在 SQL Server 2000 上。我不知道我们有任何早于 2005 年的版本。非常感谢大家!

于 2014-01-31T20:20:38.923 回答
0

似乎在交叉应用运算符之后的子查询中,'group by'和'where'的顺序应该相互改变:

(
Select pc_id
, Effective_Date=max(Effective_Date)
, PlanEffective_Date=max(PlanEffective_Date) 
from dbo.Price_Inc_PC_Details
group by pc_id
having pc_id = v.pc_id
) as d

请尝试一下,看看它是否能解决您的问题,因为这不会改变您想要达到的结果。

此外,查询末尾的 group by 似乎服务于“DISTINCT”的目的。

于 2014-01-30T15:50:51.710 回答