从数据库中选择项目

时间:2015-07-14 19:12:38

标签: sql-server

我目前正在开发购物车,对于管理员方面,我正在尝试获取尚未发货的所有订单(及其商品)。我正在使用以下存储过程,但它没有按照我打算的方式工作,它为购物车中的每个项目提取名称,发票,价格等,我想要的是提取订单信息一次,然后列出所有每个订单的商品(包括数量和价格)。

CREATE PROCEDURE dbo.sp_GetOrdersNotShipped 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    BEGIN TRY
        SELECT
            p.ProductName AS 'Name',
            o.OrderId AS 'OrderId',
            o.InvoiceNumber AS 'Invoice',
            o.OrderTotal AS 'Total',
            oi.ProductQuantity AS 'Quantity'
        FROM
            Products p INNER JOIN OrderItems oi ON p.ProductId = oi.ProductId
            INNER JOIN Orders o on oi.OrderId = o.OrderId
        WHERE
            o.IsShipped = 0
    END TRY
    BEGIN CATCH
        DECLARE @ErrorNumber INT = ERROR_NUMBER();
        DECLARE @ErrorLine INT = ERROR_LINE();
        DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
        DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
        DECLARE @ErrorState INT = ERROR_STATE();

        PRINT 'Actual error number: ' + CAST(@ErrorNumber AS VARCHAR(10));
        PRINT 'Actual line number: ' + CAST(@ErrorLine AS VARCHAR(10));

        RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
    END CATCH
END
GO

0 个答案:

没有答案