2017-12-02 59 views
0

這裏一個供應商提供的SQL函數是這樣一個問題:創建,用於輸入參數和返回用最低的成本

代碼並執行,以創建一個名爲PreferredVendor函數,它接受一個
產品ID參數作爲輸入的語句並返回具有最低成本
賣方在VendorProduct賣方ID

我無法返回廠商ID用最低的成本

,這裏是表結構

Create table VendorProduct(

VendorProductID int identity primary key, 
Cost decimal(12,4), 
ProductID int identity foreign key references Product(ProductID), 
VendorID int identity foreign key references Vendor(VendorID) 
) 

我到目前爲止創建的代碼如下:

CREATE FUNCTION dbo.PreferredVendor (@ProductID int) 
RETURNS int 
AS 
BEGIN 
    DECLARE @LowestVendorPrice int 
    SELECT @LowestVendorPrice = VendorID FROM VendorProduct 
    WHERE ProductID = @ProductID 
    RETURN @LowestVendorPrice 
END 
GO 

我一直無法找出如何在這裏使用MIN!

+0

哪個[DBMS](https://en.wikipedia.org/wiki/DBMS)產品是您使用? 「SQL」只是一種查詢語言,而不是特定數據庫產品的名稱。 –

回答

2

使用此選擇你的功能

SELECT TOP 1 @LowestVendorPrice = VendorID FROM VendorProduct 
    WHERE ProductID = @ProductID 
    ORDER BY Cost 
+0

非常感謝!我完全無視了整個聲明! –

相關問題