请教条件insert语句(if insert into或者insert into where)
siu5 2014-12-23 10:01:03 用Java写了个系统,为了不让java在插入有唯一约束的表时候报错,我想在insert语句中加入判断。
但自己写了好多次,还在网上找了一下,也测试过,都报错。
我的语句是这样的:
表的结构是:id int, empSn smaillint, remark nvarchar(500)
sql语句:
insert into tn values (null, 7, '测试数据')
where not exists (select id from tn where empSn=7)
或者:
if not exists (select id from tn where empSn=7)
insert into tn values (null, 7, '测试数据')
结果总是报这个错:
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 'where not EXISTS (select id from tn where empSn=7)' at line 2
或者:
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 id from tn where empSn=7)
insert into' at line 1
请问我应该怎样修改?谢谢!