通过在存储过程中不工作命令

时间:2018-10-31 04:31:12

标签: mysql

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内却不起作用(按desc在TransactionDate上应用命令不起作用)

2 个答案:

答案 0 :(得分:0)

然后尝试如下创建存储过程

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 ;

答案 1 :(得分:0)

您不能对一种“订购依据”使用两种类型的排名。

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