在存储过程mysql中不工作的顺序

xxls0lw8  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(353)
select         
       tr.TransactionId as TxnIdentifier,      
       m.MerchantName as MerchantName,       
       case           
       when tr.ChannelType=2 then 'ACH'
       when tr.ChannelType=3 then 'CC'    
       when tr.ChannelType=4 then 'Debit'   
       else null       
       end as ChannelType,       
       tr.CaptureAmount as CaptureAmount,      
       case                          when tr.OperationType=0 then 'Sale'   
       when tr.OperationType=1 then 'Verify only'            
       when tr.OperationType=2 then 'ForceSale'         
       when tr.OperationType=3 then 'Adjust'              
       when tr.OperationType=4 then 'Activate'           
       when tr.OperationType=5 then 'Deactivate'          
       when tr.OperationType=6 then 'Reload'             
       when tr.OperationType=7 then 'Refund'              
       when tr.OperationType=8 then 'Inquire'              
       else null                       end as TxnType,   
       case                          when tr.TransactionStatus=0 then 'Created'       
       when tr.TransactionStatus=1 then 'Pending'                   
       when tr.TransactionStatus=2 then 'Authorized'           
       when tr.TransactionStatus=3 then 'Posted'               
       when tr.TransactionStatus=4 then 'Accepted'   
       when tr.TransactionStatus=5 then 'Failed'               
       when tr.TransactionStatus=6 then 'Returned'                   
       when tr.TransactionStatus=7 then 'Chargeback'              
       when tr.TransactionStatus=8 then 'Void'               
       when tr.TransactionStatus=9 then 'Refunded'                 
       when tr.TransactionStatus=10 then 'Approved'             
       when tr.TransactionStatus=11 then 'Void attempted'             
       when tr.TransactionStatus=12 then 'Refund attempted'            
       when tr.TransactionStatus=13 then 'Hold'             
       when tr.TransactionStatus=14 then 'Denied'            
       when tr.TransactionStatus=15 then 'Settlement hold'              
       when tr.TransactionStatus=16 then 'Success'             
       when tr.TransactionStatus=17 then 'Retried'                  
       when tr.TransactionStatus=100 then 'Unknown'          
       else null                       end as TxnStatus,         
       tr.PreAuthCode as AuthCode,             
       DATE_FORMAT(tr.TransactionDate, '%m-%d-%Y %H:%i:%s') as TransactionDate,              
       case                          when tr.OperationType='7' then tr.Amount       
       else null                       end as RefundAmount,               
       case                          when tr.OperationType='7' 
       then DATE_FORMAT(tr.TransactionDate, '%m-%d-%Y %H:%i:%s')        
       else null                       end as RefundedOn,     
       tr.TraceNumber as TraceNumber                 
       from TransactionEntity tr             
       inner join  `enter code here`DOMAIN.Merchant m on m.Id=tr.MerchantId where 1=1
            and (tr.MerchantId = merchantId or merchantId=0)
            and (tr.ChannelType = channelType or channelType=0)
            And (tr.TransactionDate >= startDate or startDate is null) 
            And (tr.TransactionDate <= endDate  or endDate is null)
ORDER BY
        CASE WHEN sortField = 'TransactionDate' AND sortDirection='ASC' THEN TransactionDate END ASC,
        CASE WHEN sortField = 'TransactionDate' AND sortDirection='DESC' THEN TransactionDate END DESC

如果在存储过程外部应用order by子句,它可以正常工作,但在sp内部它不工作(在transactiondate上应用order by desc不工作)

44u64gxh

44u64gxh1#

尝试创建如下所示的存储过程

DELIMITER //

CREATE PROCEDURE select_whatever(sortField VARCHAR(20), sortDirection CHAR(4))
BEGIN
    DECLARE select_clause TEXT;
    DECLARE order_by_clause VARCHAR(100);

    SET select_clause = "
select         
   tr.TransactionId as TxnIdentifier,      
   m.MerchantName as MerchantName,       
   case           
       when tr.ChannelType=2 then 'ACH'
       when tr.ChannelType=3 then 'CC'    
       when tr.ChannelType=4 then 'Debit'   
       else null       
       end as ChannelType,       
   tr.CaptureAmount as CaptureAmount,      
   case                          
       when tr.OperationType=0 then 'Sale'   
       when tr.OperationType=1 then 'Verify only'            
       when tr.OperationType=2 then 'ForceSale'         
       when tr.OperationType=3 then 'Adjust'              
       when tr.OperationType=4 then 'Activate'           
       when tr.OperationType=5 then 'Deactivate'          
       when tr.OperationType=6 then 'Reload'             
       when tr.OperationType=7 then 'Refund'              
       when tr.OperationType=8 then 'Inquire'              
       else null                       
       end as TxnType,   
   case                          
       when tr.TransactionStatus=0 then 'Created'       
       when tr.TransactionStatus=1 then 'Pending'                   
       when tr.TransactionStatus=2 then 'Authorized'           
       when tr.TransactionStatus=3 then 'Posted'               
       when tr.TransactionStatus=4 then 'Accepted'   
       when tr.TransactionStatus=5 then 'Failed'               
       when tr.TransactionStatus=6 then 'Returned'                   
       when tr.TransactionStatus=7 then 'Chargeback'              
       when tr.TransactionStatus=8 then 'Void'               
       when tr.TransactionStatus=9 then 'Refunded'                 
       when tr.TransactionStatus=10 then 'Approved'             
       when tr.TransactionStatus=11 then 'Void attempted'             
       when tr.TransactionStatus=12 then 'Refund attempted'            
       when tr.TransactionStatus=13 then 'Hold'             
       when tr.TransactionStatus=14 then 'Denied'            
       when tr.TransactionStatus=15 then 'Settlement hold'              
       when tr.TransactionStatus=16 then 'Success'             
       when tr.TransactionStatus=17 then 'Retried'                  
       when tr.TransactionStatus=100 then 'Unknown'          
       else null                       
       end as TxnStatus,         
   tr.PreAuthCode as AuthCode,             
   DATE_FORMAT(tr.TransactionDate, '%m-%d-%Y %H:%i:%s') as TransactionDate,              
   case                          
       when tr.OperationType='7' then tr.Amount       
       else null                       
       end as RefundAmount,               
   case                          
       when tr.OperationType='7' 
            then DATE_FORMAT(tr.TransactionDate, '%m-%d-%Y %H:%i:%s')        
       else null                       
       end as RefundedOn,     
   tr.TraceNumber as TraceNumber                 
   from TransactionEntity tr             
   inner join  `enter code here`DOMAIN.Merchant m on m.Id=tr.MerchantId where 1=1
        and (tr.MerchantId = merchantId or merchantId=0)
        and (tr.ChannelType = channelType or channelType=0)
        And (tr.TransactionDate >= startDate or startDate is null) 
        And (tr.TransactionDate <= endDate  or endDate is null)
    ";

    SET order_by_clause = "ORDER BY";

    IF sortField = "TransactionDate" AND sortDirection="ASC" THEN
        SET order_by_clause = CONCAT(order_by_clause, " TransactionDate ASC")
    ELSEIF sortField = "TransactionDate" AND sortDirection="DESC" THEN
        SET order_by_clause = CONCAT(order_by_clause, " TransactionDate DESC")
    END IF

    SET @dynamic_sql = CONCAT(select_clause, ' ', order_by_clause);

    PREPARE select_whatever_statement
    FROM @dynamic_sql;

    EXECUTE select_whatever_statement;

    DEALLOCATE PREPARE select_whatever_statement;
END //

DELIMITER ;
wqlqzqxt

wqlqzqxt2#

你不能在一个订单上使用两种排序方式。

select         
       tr.TransactionId as TxnIdentifier,      
       m.MerchantName as MerchantName,       
       case           
       when tr.ChannelType=2 then 'ACH'
       when tr.ChannelType=3 then 'CC'    
       when tr.ChannelType=4 then 'Debit'   
       else null       
       end as ChannelType,       
       tr.CaptureAmount as CaptureAmount,      
       case                          when tr.OperationType=0 then 'Sale'   
       when tr.OperationType=1 then 'Verify only'            
       when tr.OperationType=2 then 'ForceSale'         
       when tr.OperationType=3 then 'Adjust'              
       when tr.OperationType=4 then 'Activate'           
       when tr.OperationType=5 then 'Deactivate'          
       when tr.OperationType=6 then 'Reload'             
       when tr.OperationType=7 then 'Refund'              
       when tr.OperationType=8 then 'Inquire'              
       else null                       end as TxnType,   
       case                          when tr.TransactionStatus=0 then 'Created'       
       when tr.TransactionStatus=1 then 'Pending'                   
       when tr.TransactionStatus=2 then 'Authorized'           
       when tr.TransactionStatus=3 then 'Posted'               
       when tr.TransactionStatus=4 then 'Accepted'   
       when tr.TransactionStatus=5 then 'Failed'               
       when tr.TransactionStatus=6 then 'Returned'                   
       when tr.TransactionStatus=7 then 'Chargeback'              
       when tr.TransactionStatus=8 then 'Void'               
       when tr.TransactionStatus=9 then 'Refunded'                 
       when tr.TransactionStatus=10 then 'Approved'             
       when tr.TransactionStatus=11 then 'Void attempted'             
       when tr.TransactionStatus=12 then 'Refund attempted'            
       when tr.TransactionStatus=13 then 'Hold'             
       when tr.TransactionStatus=14 then 'Denied'            
       when tr.TransactionStatus=15 then 'Settlement hold'              
       when tr.TransactionStatus=16 then 'Success'             
       when tr.TransactionStatus=17 then 'Retried'                  
       when tr.TransactionStatus=100 then 'Unknown'          
       else null                       end as TxnStatus,         
       tr.PreAuthCode as AuthCode,             
       DATE_FORMAT(tr.TransactionDate, '%m-%d-%Y %H:%i:%s') as TransactionDate,              
       case                          when tr.OperationType='7' then tr.Amount       
       else null                       end as RefundAmount,               
       case                          when tr.OperationType='7' 
       then DATE_FORMAT(tr.TransactionDate, '%m-%d-%Y %H:%i:%s')        
       else null                       end as RefundedOn,     
       tr.TraceNumber as TraceNumber                 
       from TransactionEntity tr             
       inner join  `enter code here`DOMAIN.Merchant m on m.Id=tr.MerchantId where 1=1
            and (tr.MerchantId = merchantId or merchantId=0)
            and (tr.ChannelType = channelType or channelType=0)
            And (tr.TransactionDate >= startDate or startDate is null) 
            And (tr.TransactionDate <= endDate  or endDate is null)
ORDER BY
        CASE WHEN sortField = 'TransactionDate' AND sortDirection='ASC' THEN TransactionDate  ,
        WHEN sortField = 'TransactionDate' AND sortDirection='DESC' THEN TransactionDate 
END DESC OR ASC

相关问题