2010-11-19 5 views
0

現在のフォーマットでは正しく動作せず、すべての行を返しません。値は値を返しません。ここ はSPです:SPを動的SQLクエリに変換するか、COALESCEを使用するSPに変換するのを助けてください。

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[AdvancedSearch] 
(
@StartTime datetime = null, 
@EndTime datetime = null, 
@CustomerEmail nvarchar(255) = null, 
@Username nvarchar(255) = null, 
@CustomerName nvarchar(255) = null, 
@OrderNumber int = null, 
@MinimumOrderAmount decimal = null, 
@MaximumOrderAmount decimal = null, 
@ShippingMethod nvarchar(255) = null, 
@SKU nvarchar(255) = null, 
@CouponID int = null, 
@DiscountType int = null, 
@ShippingCountryID int = null, 
@UserRegistration nvarchar(255) = null, 
@OrderStatusPending int = null, 
@OrderStatusProcessing int = null, 
@OrderStatusComplete int = null, 
@OrderStatusCancelled int = null, 
@OrderStatusCancelledDiscontinued int = null, 
@OrderStatusCancelledCustomerRequest int = null, 
@OrderStatusCancelledPendingNeverPaid int = null 
    ) 
    AS 
    BEGIN 
    SET NOCOUNT ON 

    SELECT DISTINCT o.OrderID, o.OrderTotal, n.Name AS OrderStatus, p.Name AS PaymentStatus, s.Name AS ShippingStatus, o.BillingFirstName + ' ' + o.BillingLastName AS CustomerName, o.CreatedOn AS CreatedOn FROM Nop_Order o 
    LEFT OUTER JOIN StatusOrders n ON o.OrderStatusID = n.OrderStatusID 
    LEFT OUTER JOIN StatusPayments p ON o.PaymentStatusID = p.PaymentStatusID 
    LEFT OUTER JOIN ShippingStatus s ON o.ShippingStatusID = s.ShippingStatusID 
    LEFT OUTER JOIN Customer c ON o.CustomerID = c.CustomerID 
    LEFT OUTER JOIN Discount d ON o.DiscountID = d.DiscountID 
    LEFT OUTER JOIN OrderProductionVariable opv ON o.OrderID = opv.OrderID 
    LEFT OUTER JOIN ProductionVariations pv ON opv.ProductVariantID = pv.ProductVariantId 
    WHERE (o.CreatedOn > @StartTime OR @StartTime IS NULL) 
    AND (o.CreatedOn < @EndTime OR @EndTime IS NULL) 
    AND (o.ShippingEmail = @CustomerEmail OR @CustomerEmail IS NULL) 
    AND (o.OrderStatusID IN (CAST(@OrderStatusID as int)) OR @OrderStatusID IS NULL) 
    AND (o.PaymentStatusID IN (@PaymentStatusID) OR @PaymentStatusID IS NULL) 
    AND (c.Username = @Username OR @Username IS NULL) 
    AND (o.BillingFirstName + ' ' + o.BillingLastName = @CustomerName OR @CustomerName IS NULL) 
    AND (o.ShippingFirstName + ' ' + o.ShippingLastName = @CustomerName OR @CustomerName IS NULL) 
    AND (o.OrderID = @OrderNumber OR @OrderNumber IS NULL) 
    AND (o.OrderTotal > @MinimumOrderAmount or @MinimumOrderAmount IS NULL) 
    AND (o.OrderTotal < @MaximumOrderAmount OR @MaximumOrderAmount IS NULL) 
    AND (o.ShippingMethod = @ShippingMethod OR @ShippingMethod IS NULL) 
    AND (d.DiscountTypeID = @DiscountType OR @DiscountType IS NULL) 
    AND (o.ShippingCountryID = @ShippingCountryID OR @ShippingCountryID IS NULL) 
    AND (o.DiscountID = @CouponID OR @CouponID IS NULL) 
    AND (pv.SKU = @SKU OR @SKU IS NULL) 
    AND (c.Email = @UserRegistration OR @UserRegistration IS NULL) 
    AND (o.Deleted = 0) 
AND (o.OrderStatusID = @OrderStatusPending OR o.OrderStatusID = @OrderStatusProcessing OR o.OrderStatusID = @OrderStatusComplete OR o.OrderStatusID = @OrderStatusCancelled OR o.OrderStatusID = @OrderStatusCancelledDiscontinued 
OR o.OrderStatusID = @OrderStatusCancelledCustomerRequest OR o.OrderStatusID = @OrderStatusCancelledPendingNeverPaid) 
    ORDER BY o.OrderID  
    END 

私が代わりにCOALESCEで何かをしようとしたが、私はCOALESCEとint型の値を持つたびCOALESCEは、少なくともではない私のSPで、INTで動作するように思われません、SPにはありません任意の値を返します。ここ は、COALESCEとSPです:私は本当にこのクエリの作業を見たいと思う

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[AdvancedSearch] 
(
    @StartTime datetime = null, 
    @EndTime datetime = null, 
    @CustomerEmail nvarchar(255) = null, 
    @Username nvarchar(255) = null, 
    @CustomerName nvarchar(255) = null, 
    @OrderNumber int = null, 
    @MinimumOrderAmount decimal = null, 
    @MaximumOrderAmount decimal = null, 
    @ShippingMethod nvarchar(255) = null, 
    @SKU nvarchar(255) = null, 
    @CouponID int = null, 
    @DiscountType int = null, 
    @ShippingCountryID int = null, 
    @UserRegistration nvarchar(255) = null, 
    @OrderStatusPending int = null, 
    @OrderStatusProcessing int = null, 
    @OrderStatusComplete int = null, 
    @OrderStatusCancelled int = null, 
    @OrderStatusCancelledDiscontinued int = null, 
    @OrderStatusCancelledCustomerRequest int = null, 
    @OrderStatusCancelledPendingNeverPaid int = null 
    ) 
    AS 
    BEGIN 
    SET NOCOUNT ON 

    SELECT DISTINCT o.OrderID, o.OrderTotal, n.Name AS OrderStatus, p.Name AS PaymentStatus, s.Name AS ShippingStatus, o.BillingFirstName + ' ' + o.BillingLastName AS CustomerName, o.CreatedOn AS CreatedOn FROM Nop_Order o 
     LEFT OUTER JOIN StatusOrders n ON o.OrderStatusID = n.OrderStatusID 
    LEFT OUTER JOIN StatusPayments p ON o.PaymentStatusID = p.PaymentStatusID 
    LEFT OUTER JOIN ShippingStatus s ON o.ShippingStatusID = s.ShippingStatusID 
    LEFT OUTER JOIN Customer c ON o.CustomerID = c.CustomerID 
    LEFT OUTER JOIN Discount d ON o.DiscountID = d.DiscountID 
    LEFT OUTER JOIN OrderProductionVariable opv ON o.OrderID = opv.OrderID 
    LEFT OUTER JOIN ProductionVariations pv ON opv.ProductVariantID = pv.ProductVariantId 
    WHERE (o.CreatedOn > COALESCE(@StartTime, '01-01-1899')) 
    AND (o.CreatedOn < COALESCE(@EndTime, '01-01-2099')) 
    AND (o.ShippingEmail = COALESCE(@CustomerEmail, o.ShippingEmail)) 
    AND (c.Username = COALESCE(@Username, c.Username)) 
    AND (o.BillingFirstName + ' ' + o.BillingLastName = COALESCE(@CustomerName, o.BillingFirstName + ' ' + o.BillingLastName)) 
    AND (o.ShippingFirstName + ' ' + o.ShippingLastName = COALESCE(@CustomerName, o.ShippingFirstName + ' ' + o.ShippingLastName)) 
    AND (o.OrderID = COALESCE(@OrderNumber, o.OrderID)) 
    AND (o.OrderTotal > COALESCE(@MinimumOrderAmount, o.OrderTotal)) 
    AND (o.OrderTotal < COALESCE(@MaximumOrderAmount, o.OrderTotal)) 
    AND (o.ShippingMethod = COALESCE(@ShippingMethod, o.ShippingMethod)) 
    AND (d.DiscountTypeID = COALESCE(@DiscountType, d.DiscountTypeID)) 
    AND (o.ShippingCountryID = COALESCE(@ShippingCountryID, o.ShippingCountryID)) 
    AND (o.DiscountID = COALESCE(@CouponID, O.DiscountID)) 
    AND (pv.SKU = COALESCE(@SKU, pv.SKU)) 
    AND (c.Email = COALESCE(@UserRegistration, c.Email)) 
    AND (o.Deleted = 0) 
    AND(o.OrderStatusID = 
    COALESCE(@OrderStatusPending, o.OrderStatusID) 
    | COALESCE(@OrderStatusProcessing, o.OrderStatusID) 
    | COALESCE(@OrderStatusComplete, o.OrderStatusID) 
    | COALESCE(@OrderStatusCancelled, o.OrderStatusID) 
    | COALESCE(@OrderStatusCancelledCustomerRequest, o.OrderStatusID) 
    | COALESCE(@OrderStatusCancelledDiscontinued, o.OrderStatusID) 
    | COALESCE(@OrderStatusCancelledPendingNeverPaid, o.OrderStatusID)) 
    ORDER BY o.OrderID 

    END 

、私が持っている最大の問題は、オーダー状況です。ウェブサイトから複数の選択肢が送信されている可能性があります。それらをすべてマージして結果をフィルタリングする必要があります。 私は、このSPのいくつかを修正するか、動的クエリを使って誰かを助けてくれることを願っています。 ありがとうございました。

+0

* AND(o.OrderTotal> COALESCE(@MinimumOrderAmount、o.OrderTotal))*は決してwにはなりません@MinimumOrderAmountのorkはNULLです。論理的にo.OrderTotal> o.OrderTotal **は真実ではありません –

+0

返信のためにthx、どのように私は問題を解決できるか考えていますか?質問の一番下にある複数の選択が実際の問題です。注文量の問題を修正できます。 – Laziale

答えて

0

アペール以上の合併症から、あなたの問題がであるように思わ: - それは何かお読みください

AND(o.OrderStatusID = 
    COALESCE(@OrderStatusPending, o.OrderStatusID) 
| COALESCE(@OrderStatusProcessing, o.OrderStatusID) 
| COALESCE(@OrderStatusComplete, o.OrderStatusID) 
| COALESCE(@OrderStatusCancelled, o.OrderStatusID) 
| COALESCE(@OrderStatusCancelledCustomerRequest, o.OrderStatusID) 
| COALESCE(@OrderStatusCancelledDiscontinued, o.OrderStatusID) 
| COALESCE(@OrderStatusCancelledPendingNeverPaid, o.OrderStatusID)) 

:すべての@変数がこれをnullに設定されているよう

AND (o.OrderStatusID = COALESCE(@OrderStatusPending, o.OrderStatusID) 
or o.OrderStatusID =COALESCE(@OrderStatusProcessing, o.OrderStatusID) 
or o.OrderStatusID =COALESCE(@OrderStatusComplete, o.OrderStatusID) 
or o.OrderStatusID =COALESCE(@OrderStatusCancelled, o.OrderStatusID) 
or o.OrderStatusID =COALESCE(@OrderStatusCancelledCustomerRequest, o.OrderStatusID) 
or o.OrderStatusID =COALESCE(@OrderStatusCancelledDiscontinued, o.OrderStatusID) 
or o.OrderStatusID =COALESCE(@OrderStatusCancelledPendingNeverPaid, o.OrderStatusID)) 

Howverをただであります

AND o.OrderStatusID = o.OrderStatusID 
+0

ORを使用すると、合体で使用しているときにエラーがスローされます。 – Laziale

関連する問題