更正mysql触发器语法以防止无限循环

时间:2016-03-18 21:58:25

标签: mysql sql

我正在创建以下测试触发器,以便在发生UPDATE操作时更新字段的值:

CREATE TRIGGER `test_index` 
AFTER UPDATE ON `main_itemmaster` 
FOR EACH ROW  
    UPDATE `main_itemmaster` SET `guid` = "hello" WHERE `id` = NEW.id; 

但是,每当我对此表进行更新时,都会出现以下错误:

Can't update table 'main_itemmaster' in stored function/trigger 
because it is already used by statement which invoked this stored function/trigger.

为什么会发生这种情况?我将如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

你想做什么?

显然,您无法在更新时更新相同的表格......它将成为循环播放。

您想要在插入时更新吗?这应该是可能的:

def readDict(name):
    readName = DIR+name+'.json'
    try:
        with open(readName, "r") as infile:
            dictValues = json.load(infile)
            return(dictValues)
    except IOError as e:
        print(e)
        return('None')
    except ValueError as e:
        print(e)
        return('None')

我看到了你的评论。我提议这个:

CREATE TRIGGER `test_index` 
AFTER INSERT ON `main_itemmaster` 
FOR EACH ROW  
UPDATE `main_itemmaster` SET `guid` = "hello" WHERE `id` = NEW.id; 

我测试了它,它的工作原理!此外,如果您只更新一个字段。

此致