在sql存储过程中使用两个IF THEN

时间:2016-06-07 14:47:05

标签: mysql if-statement syntax-error mysql-workbench

我正在尝试在MySQL Workbench中创建一个存储过程。 我需要检查某个标志是否等于1.如果该标志等于1,则按FollowupDate搜索,否则使用QuoteDate

IF(qFollowup = 1) THEN
      IF(pFromFollowupDate != '') THEN
      BEGIN
        SET @Main = CONCAT(@Main, '  AND DATE(q.FollowupDate)  >= \'',pFromFollowupDate,'\' ');
      END;
ELSE
IF(pFromDate != '') THEN
BEGIN
  SET @Main = CONCAT(@Main, '  AND DATE(q.QuoteDate)  >= \'',pFromDate,'\' ');
END;
END IF;
END IF;

当我尝试应用更改时,它说有语法错误。

1 个答案:

答案 0 :(得分:0)

尝试:

DELIMITER //

CREATE PROCEDURE `sp_name`()
BEGIN
  IF qFollowup = 1 THEN
    IF pFromFollowupDate != '' THEN
      SET @Main = CONCAT(@Main, '  AND DATE(q.FollowupDate)  >= \'',pFromFollowupDate,'\' ');
    ELSE
      IF pFromDate != '' THEN
        SET @Main = CONCAT(@Main, '  AND DATE(q.QuoteDate)  >= \'',pFromDate,'\' ');
      END IF;
    END IF;
  END IF;
END//

DELIMITER ;