为什么MySQL不允许我声明这些变量

bqjvbblv  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(144)

我有这个触发器(而不是插入),这是为常规SQL,我正试图把它变成MySQL,这是我所得到的:

Create Trigger trGenerateNpcID
Before Insert On NPC For Each Row
Begin
    Declare Race varchar(5);
    Declare Name varchar(25);
    Declare Level int;
    Declare Class1 varchar(5);
    Declare Class2 varchar(5);
    Declare City varchar(5);

    Set Race = (Select Race From Inserted);
    Set Name = (Select Name From Inserted);
    Set Level = (Select Level From Inserted);
    Set Class1 = (Select Class1 From Inserted);
    Set Class2 = (Select Class2 From Inserted);
    Set City = (Select City From Inserted);

    Insert into NPC
    Values(Left(Race, 1) + Left(Name, 1) + Left(Level, 1) + next value for sqNpcID,
    Name, Race, Level, Class1, Class2, City);
End;

每次我试着去做,它都会给我这个错误:

1064-SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,了解在第4行""附近使用的正确语法

我会很感激你的任何帮助。

j8ag8udp

j8ag8udp1#

它应该起作用:

CREATE TRIGGER trGenerateNpcID
    BEFORE INSERT ON NPC FOR EACH ROW
    BEGIN
        DECLARE Race VARCHAR(5);
        DECLARE Name VARCHAR(25);
        DECLARE Level INT;
        DECLARE Class1 VARCHAR(5);
        DECLARE Class2 VARCHAR(5);
        DECLARE City VARCHAR(5);
    
        SET Race = (SELECT Race FROM INSERTED);
        SET Name = (SELECT Name FROM INSERTED);
        SET Level = (SELECT Level FROM INSERTED);
        SET Class1 = (SELECT Class1 FROM INSERTED);
        SET Class2 = (SELECT Class2 FROM INSERTED);
        SET City = (SELECT City FROM INSERTED);
    
        INSERT INTO NPC
        VALUES(LEFT(Race,1) + LEFT(Name,1) + LEFT(Level,1) + NEXT VALUE FOR sqNpcID,
        Name, Race, Level, Class1, Class2, City);
    END;

相关问题