2

我有这个存储过程,它应该检查一个字段是否为空并执行一个查询,否则执行另一个,但我得到一个错误。这是代码:

DELIMITER $$
 CREATE PROCEDURE mw_info(
    m_date_begin INTEGER,
    m_date_end INTEGER,
    m_from INTEGER,
    m_numberOfrecords INTEGER,
    m_callsource VARCHAR(15))
 BEGIN

    IF (m_callsource = '') THEN

       SELECT * 
         FROM mw_iaa 
        WHERE calllegcode IN ('CB_A','CB_B')
          AND calltime BETWEEN m_date_begin AND m_date_end
        ORDER BY calltime DESC 
        LIMIT m_from, m_numberOfrecords;

    ELSE     

       SELECT * 
         FROM mw_iaa 
        WHERE calllegcode IN ( 'CB_A', 'CB_B')
          AND callsource = m_callsource
          AND calltime BETWEEN m_date_begin AND m_date_end
        ORDER BY calltime DESC 
        LIMIT m_from, m_numberOfrecords;    

    END IF; 

 END;  
 END$$
DELIMITER ;

这是错误:

错误代码:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以
在第 1 行的“else SELECT * FROM mw_iaa WHERE calllegcode”附近使用正确的语法

问题可能是什么?

4

2 回答 2

2

您可以只在一个查询中执行此操作,并摆脱如下IF语句:

SELECT * 
FROM mw_iaa
WHERE ((m_callsource = '') OR (callsource = m_callsource))
  AND calllegcode IN ('CB_A', 'CB_B')
  AND callsource = m_callsource
  AND calltime BETWEEN m_date_begin AND m_date_end
ORDER BY calltime DESC 
LIMIT m_from, m_numberOfrecords;

然后,每当您使用 传递m_callsource参数时'',该条件((m_callsource = '') OR (callsource = m_callsource))将被忽略,因为它没有出现。

于 2012-12-23T14:14:56.893 回答
0

我见过这样的错误,然后我在过程源中有制表符。

尝试用空格替换所有制表符。

于 2012-12-23T14:14:16.560 回答