视图是否可以从表值函数中选择列?
CREATE FUNCTION fnGetLatestOrderStatus(@OrderId int)
RETURNS TABLE
AS
RETURN
SELECT TOP (1) Status, TimestampUtc
FROM OrderStatusHistory
ORDER BY TimestampUtc DESC
GO
CREATE VIEW MyView AS
SELECT
OrderId,
CustomerId,
fnGetLatestOrderStatus(OrderId).Status AS OrderStatus,
fnGetLatestOrderStatus(OrderId).TimestampUtc AS OrderStatusTimestamp
FROM Orders
GO
SSMS不喜欢此错误
找不到列“ fnGetLatestOrderStatus”或用户定义的函数或聚合“ fnGetLatestOrderStatus”,或者名称不明确。
答案 0 :(得分:2)
这是您想要做的吗?
SELECT o.OrderId, o.CustomerId, flos.*
FROM Orders o CROSS APPLY
dbo.fnGetLatestOrderStatus(o.OrderId int) flos;
您需要在FROM
子句中调用表值函数。
答案 1 :(得分:1)
如果语法正确,则有可能。注意:
CREATE VIEW MyView
AS
SELECT
OrderId
, CustomerId
, (select Status from dbo.fnGetLatestOrderStatus(OrderId)) AS OrderStatus
, (select TimestampUtc from dbo.fnGetLatestOrderStatus(OrderId)) AS OrderStatusTimestamp
FROM Orders
GO
注意:而且戈登的解决方案可能更适合您的问题,但我认为有必要指出这两个错误,供您参考