2017-12-02 199 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

非常感谢!我完全无视了整个声明! –