2013-01-11 33 views
5

我有一个SQL函数,它返回一个项目的最低和最高销售价格。我想提出一个查询与它的销售价格SQL Server传递表列到函数的参数

像这样得到其他StockItem列在一起:

SELECT i.StockItemID ii, 
     i.Name, 
     i.Code, 
     pli.SellingPrice AS MinSellingPrice, 
     pli.StandardSellingPrice AS MaxSellingPrice, 
     i.WebDetailedDescription, 
     i.WebAdditionalInfo, 
     i.FeaturedItemDescription 
FROM SC_StockItem AS i, 
    func_GetPrice(17, i.StockItemID, 5) pli 

然而,这给出了一个错误:

Msg 4104, Level 16, State 1, Line 12 The multi-part identifier "i.StockItemID" could not be bound.

任何想法我怎么能做这个 ?

在此先感谢

回答

19

如果是表值函数,那么你可以使用OUTER APPLY

select i.StockItemID ii, 
    i.Name, 
    i.Code, 
    pli.SellingPrice as MinSellingPrice, 
    pli.StandardSellingPrice as MaxSellingPrice, 
    i.WebDetailedDescription, 
    i.WebAdditionalInfo, 
    i.FeaturedItemDescription 
from SC_StockItem as i 
OUTER APPLY func_GetPrice(17, i.StockItemID, 5) pli 

From MSDN

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query.

+0

伟大谢谢:) – Jonny