[英]Checking if data is in database before adding data SQL
I am trying to create an SQL statement that CHECKS IF A TABLE IS EMPTY before INSERTING data 我正在尝试创建一个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")
Error Produced: 生成错误:
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
Try the following. 请尝试以下方法。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.