2013-01-11 27 views
5

Tôi có hàm SQL trả về giá bán tối thiểu và tối đa của một mục. Tôi muốn tạo ra một truy vấn mà được cột StockItem khác cùng với nó giáMáy chủ SQL chuyển các cột trong bảng sang tham số của hàm

như vậy bán:

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 

Tuy nhiên điều này mang lại một lỗi:

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

bất kỳ ý tưởng làm thế nào tôi có thể làm cái này ?

Cảm ơn trước

Trả lời

19

Nếu đó là một chức năng bảng giá trị, sau đó bạn có thể sử dụng 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

Cảm ơn tuyệt vời :) – Jonny

Các vấn đề liên quan