根据我之前的问题,我正在尝试打印来自不同表格的各种列,并且只打印十大最赚钱的产品,并通过售价 - 成本价计算。如何像在桌子上一样逐行打印出前10个输出?
CREATE PROCEDURE usp_top10ProfitableProducts
@productID int Output,
@productdes VARCHAR(100) Output,
@quantityorder int Output,
@totalProfit DECIMAL(5,2) Output
AS
BEGIN
SELECT Top 10 *
FROM
(SELECT
@productID = productID,
@productdes = Product.description,
@quantityorder = CustomerInventory.quantityOrdered, @totalProfit = (Inventory.sellingPrice-Inventory.costPrice) AS difference
FROM
Product p, Supplier s, SupplierOrder so, Inventory in, CustomerInventory cin
WHERE
p.ProductID = s.productID, s.supplierID = so.supplierID, so.supOrderID = in.supOrderID, in.barcodeNo = cin.barcodeNo) difference_data
ORDER BY
difference DESC
DECLARE @productID INT
DECLARE @productdes VARCHAR(100)
DECLARE @quantityorder INT
DECLARE @totalProfit DECIMAL(5,2)
EXECUTE usp_top10ProfitableProducts
@productID OUT
@productdes OUT
@quantityORDER OUT
@totalProfit OUT
PRINT
答案 0 :(得分:1)
使用视图而不是过程。但是,还有其他问题需要解决:
FROM
子句中使用逗号。 始终使用明确的JOIN
语法。视图如下:
CREATE VIEW vw_top10ProfitableProducts as
SELECT Top 10 p.productID, p.description, ci.quantityOrdered,
(i.sellingPrice - i.costPrice) as Difference
FROM Product p JOIN
Supplier s
ON p.ProductID = s.productID JOIN
SupplierOrder so
ON s.supplierID = so.supplierID JOIN
Inventory in
ON so.supOrderID = in.supOrderID JOIN
CustomerInventory cin
ON in.barcodeNo = cin.barcodeNo
ORDER BY difference Desc;
然后,您可以像常规表一样访问它:
select *
from vw_top10ProfitableProducts;
这比使用存储过程容易得多。在任何情况下,您的存储过程都无法工作,因为您想要返回十行,但您只有四个输出参数。