[SQL ] NOT EXISTS的写法

楼主: rul3nk41   2017-05-31 15:16:25
不好意思,想请问各位大大们一个问题,小弟最近用VC++练习not exists的语法
程式码如下
_stprintf(strSource, _T("INSERT INTO table1 %s VALUES %s;"), strField,
strValue);
strField是字段名称,strValue是要存入的资料,测试后是可以存入的
接下来想要让已经存入过的资料不要重复入库
_stprintf(strSource, _T("INSERT INTO table1 %s VALUES %s FROM dual WHERE not
exists(select * from table1 where Caption2 = '%s');"), strField, strValue);
到这边发生了问题,没有办法将资料写入,小弟猜测是语法问题
但是试了几个方法都没有办法解决,想请问各位大大小弟这样写有哪边错误,
应该要如何写才能做到小弟要的结果呢?
补充:
测试过的方法(都失败)
//方法一
_stprintf(strSource, _T("INSERT INTO table1 %s SELECT %s FROM DUAL WHERE NOT
EXISTS ( SELECT * FROM table1 WHERE Caption2 = %s );"), strField, strValue,
strMatch);
//方法二
_stprintf(strSource, _T("INSERT INTO table1 %s VALUES %s IF NOT
EXISTS(Caption2 = %s);"), strField, strValue, strMatch);
作者: Kanyahi (狼爵々)   2017-05-31 16:55:00
INSERT INTO table1 %1 SELECT %s WHERE NOT EXIST ()%1 =>%s 打错 测试过可以执行insert into table1 %s values %s where not exists ()试试insert into values的方式 无法where 所以无法使用exists可能有其他的写法是我不知道的@@a
作者: smlboby (波比)   2017-06-06 07:07:00
if not exist (...) insert into ...

Links booklink

Contact Us: admin [ a t ] ucptt.com