在像下面这样的查询中使用多个表值函数时,SSMS会抛出错误。此外,[PRECALCPAGES_asof]的[Date]参数以红色下划线标出。
我试图理解为什么会失败。我认为这可能与SQL Server引擎的工作方式有关。已经查看了MSDN上的文档,但不幸的是我不知道该寻找什么。为什么会引起这种情况并且有办法绕过它?
查询
SELECT
[Date]
, COUNT(*)
FROM
[Warehouse].[dbo].[DimDate]
CROSS APPLY
[PROJECTS_asof]([Date])
INNER JOIN
[PRECALCPAGES_asof]([Date]) ON [PRECALCPAGES_asof].[PROJECTID] = [PROJECTS_asof].[PROJECTID]
GROUP BY
[Date]
错误
Msg 207, Level 16, State 1, Line 9
Invalid column name 'Date'.
功能
CREATE FUNCTION [ProfitManager].[PROJECTS_asof]
(
@date DATETIME
)
RETURNS TABLE AS
RETURN
(
SELECT
[PROJECTID]
, [PROJECT]
, ...
FROM
Profitmanager.[PROJECTS_HISTORY]
WHERE
[RowStartDate] <= @date
AND
[RowEndDate] > @date
)
GO
CREATE FUNCTION [ProfitManager].[PRECALCPAGES_asof]
(
@date DATETIME
)
RETURNS TABLE AS
RETURN
(
SELECT
[PAGEID]
, [PAGENAME]
, ...
FROM
Profitmanager.[PRECALCPAGES_HISTORY]
WHERE
[RowStartDate] <= @date
AND
[RowEndDate] > @date
)
GO
答案 0 :(得分:2)
我认为您不能将表中的字段用作连接中函数的参数。你应该使用交叉申请。
SELECT
[Date]
, COUNT(*)
FROM
[Warehouse].[dbo].[DimDate]
CROSS APPLY
[PROJECTS_asof]([Date])
CROSS APPLY
[PRECALCPAGES_asof]([Date])
WHERE
[PRECALCPAGES_asof].[PROJECTID] = [PROJECTS_asof].[PROJECTID]
GROUP BY
[Date]