MySql存储过程

时间:2016-08-09 14:59:13

标签: mysql stored-procedures

我在默认为DESC的可选OrderBy值中传递,如果设置为false,则应ORDER BY ASC。我不确定如何根据输入进行此排序。这是我目前的情况:

CREATE DEFINER=`app`@`%` PROCEDURE `BLAH`(
pWithdrawalTransactionId INT,
pLimit INT,
pSortDescending TINYINT
)
BEGIN
    DECLARE vLimit INT DEFAULT COALESCE(pLimit, 100);
    DECLARE vSort TINYINT DEFAULT COALESCE(pSortDescending, 1);

    SELECT
        f.WithdrawalFulfillmentId, f.PaymentStatusId, f.PaymentProcessorId, f.PaymentTypeId, r.Amount, r.RequestedAmount, r.NativeAmount, r.NativeRequestedAmount, r.RefundTransactionId, r.UpdatedDate
    FROM
        FinOps.UserWithdrawalFulfillment f
    INNER JOIN 
        FinOps.UserRefundTransaction r ON f.RefundTransactionId = r.RefundTransactionId
    WHERE
        f.WithdrawalTransactionId = pWithdrawalTransactionId
    LIMIT
        vLimit;
END

1 个答案:

答案 0 :(得分:1)

所以正确的方法是这样的:

CREATE DEFINER=`app`@`%` PROCEDURE `BLAH`(
pWithdrawalTransactionId INT,
pLimit INT,
pSortDescending TINYINT
)
BEGIN
    DECLARE vLimit INT DEFAULT COALESCE(pLimit, 100);
    DECLARE vSort TINYINT DEFAULT COALESCE(pSortDescending, 1);

    SELECT
        f.WithdrawalFulfillmentId, f.PaymentStatusId, f.PaymentProcessorId, f.PaymentTypeId, r.Amount, r.RequestedAmount, r.NativeAmount, r.NativeRequestedAmount, r.RefundTransactionId, r.UpdatedDate
    FROM
        FinOps.UserWithdrawalFulfillment f
    INNER JOIN 
        FinOps.UserRefundTransaction r ON f.RefundTransactionId = r.RefundTransactionId
    WHERE
        f.WithdrawalTransactionId = pWithdrawalTransactionId
    ORDER BY CASE WHEN pSortDescending = 1 THEN WithdrawalFulfillmentId * -1 ELSE WithdrawalFulfillmentId END ASC
    LIMIT
        vLimit;
END
相关问题