-1

我有一个存储过程,它接收 10 个参数类型 Date 但我可以在咨询为空时显示信息,因为必须显示此咨询上所有客户的信息流。有时客户有带有信用票据的订单,但有时只有订单但没有信用票据......我为 Where 子句这样做:

        WHERE 
                (Customer.cd_CustomerID = ISNULL(@customer, Customer.cd_CustomerID))
                AND (@orderID IS NULL OR Orders.cd_OrderID = @orderID)
                AND (@Location IS NULL OR CustomerSL.cd_LocID = @Location)      
                and (Convert(date,Orders.fh_Date,111) BETWEEN coalesce (@FechaPedido1,'1900-01-01') AND  coalesce (@FechaPedido2,'3000-12-31' ))
                and (Convert(date,Receipt.fh_Date,111) BETWEEN coalesce (@FechaRemision1,'1900-01-01') AND  coalesce (@FechaRemision2,'3000-12-31' )) 
                and (Convert(date,Invoice.fh_Date,111) BETWEEN coalesce (@FechaFactura1,'1900-01-01') AND  coalesce (@FechaFactura2,'3000-12-31' )) 
                and (Convert(date,CreditNote.fh_Date,111) BETWEEN coalesce (@FechaNotaCredito1,'1900-01-01') AND  coalesce (@FechaNotaCredito2,'3000-12-31' ))
                and (Convert(date,Dispersion.fc_CreatedDate,111) BETWEEN coalesce (@FechaDispersion1,'1900-01-01') AND  coalesce (@FechaDispersion2,'3000-12-31' ))

但在这种情况下,只显示具有客户端所有流程的信息。咨询没有向我显示没有没有减免或分散的订单的信息流,无论如何...已经尝试在 Where 中使用 IF 但我的语法有一些问题是...

if (@fechapedido1 is null) begin (Convert(date,Orders.fh_Date,111) BETWEEN @FechaPedido1 and @FechaPedido2)
end

SQL 服务器向我显示此消息“关键字 'Convert' 附近的语法不正确。” 但那条线是正确的......:S请帮助我:D谢谢:D

4

1 回答 1

2

子句中不能有IF语句WHERE,试试这个:

AND 
(
    (
        @fechapedido1 is null AND 
        Convert(date,Orders.fh_Date,111) BETWEEN @FechaPedido1 and @FechaPedido2
    ) OR
    @fechapedido1 is not null 
)
于 2012-10-30T16:48:49.213 回答