在添加数据之前检查数据是否在数据库中

时间:2019-05-09 20:32:21

标签: mysql sql

我正在尝试创建一条SQL语句,该语句在插入数据之前检查表是否为空

BEGIN
    IF NOT EXISTS (SELECT * FROM characters)
BEGIN
    INSERT INTO characters (1,Sam,Jordan,7,4,4,"Strength","Sam's muscular build provides +2 Storage Capacity","Meaty Punch","+2 Damage To Next Attack")

产生错误:

21:26:01    BEGIN  IF NOT EXISTS (SELECT * FROM characters) BEGIN  INSERT INTO characters (1,Sam,Jordan,7,4,4,"Strength","Sam's muscular build provides +2 Storage Capacity","Meaty Punch","+2 Damage To Next Attack")  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 'IF NOT EXISTS (SELECT * FROM characters) BEGIN  INSERT INTO characters (1,Sam,Jo' at line 2    0.000 sec

1 个答案:

答案 0 :(得分:0)

尝试以下操作。

IF NOT EXISTS (SELECT * FROM characters)
BEGIN
    INSERT INTO characters 
    VALUES
    (
       1,
       'Sam',
       'Jordan',
       7,
       4,
       4,
       'Strength',
       'Sam''s muscular build  provides +2 Storage Capacity',
       'Meaty Punch',
       '+2 Damage To Next Attack'
    )

END