MySQL5.5 If Statement

时间:2015-05-18 00:51:24

标签: mysql

在MySQLWorkbench 6.2 for Windows上使用MySQL55,

<System.Web.Services.WebMethod()> _
Public Shared Function getMarkers() As String
    Return "It's Ok"
End Function

假设DROP TRIGGER IF EXISTS `maxBBLimit`; CREATE TRIGGER `maxBBLimit` AFTER UPDATE ON locationobject FOR EACH ROW IF locationobject.boxLowY > 1000 THEN CALL RaiseException(); END IF; 有一个名为locationobject的无符号int变量,它只允许0到1000之间的值,如果它高于此范围,则调用boxLowY

由于这些错误,我的工作区拒绝编译:

RaiseException()

^ 17:43:40 CREATE TRIGGER `maxBBLimit` AFTER UPDATE ON locationobject FOR EACH ROW IF locationobject.boxLowY > 1000 THEN CALL RaiseException() Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 0.000 sec

之后,)下的红色波浪形
RaiseException(

^这个17:43:40 ; END IF Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; END IF' at line 1 0.000 sec 声明中的END下有一个红色的波形;

任何人都有任何建议如何编译此代码?

1 个答案:

答案 0 :(得分:1)

触发器定义应该包含delimiter语句。

此外,您可以使用new来引用表格。而且,您需要一个before update触发器。如果在update之后失败,则错误值仍会进入数据库。而且,您通常使用signal来返回错误。所以,如果我没有错过任何东西:

DELIMITER $$
DROP TRIGGER IF EXISTS `maxBBLimit` $$
CREATE TRIGGER `maxBBLimit`
AFTER UPDATE ON locationobject
FOR EACH ROW
    IF new.boxLowY > 1000 THEN 
        SIGNAL SQLSTATE '4500' SET MESSAGE_TEXT = 'boxLowY out of range';
    END IF$$
DELIMITER ;