我有这个存储过程,它应该检查字段是否为空并执行一个查询,否则执行另一个但我收到错误。这是代码:
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服务器版本,以便使用正确的语法 靠近'其他
SELECT * FROM mw_iaa WHERE calllegcode'在第1行
问题是什么?
答案 0 :(得分: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))
将被忽略,因为它未显示。
答案 1 :(得分:0)
我看到过这样的错误,然后我在程序源中有了制表符。
尝试用空格替换所有标签字符。