作者rul3nk41 ()
看板Database
标题[SQL ] NOT EXISTS的写法
时间Wed May 31 15:16:25 2017
不好意思,想请问各位大大们一个问题,小弟最近用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);
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.36.3.151
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1496214987.A.6E7.html
1F:→ Kanyahi: INSERT INTO table1 %1 SELECT %s WHERE NOT EXIST () 05/31 16:55
2F:→ Kanyahi: %1 =>%s 打错 测试过可以执行 05/31 16:58
感谢大大!! 小弟去测试看看
大大,小弟这样子执行过了
想请问一下,如果不要用SELECT,用INSERT INTO %s VALUE %s的语法
可以使用NOT EXISTS吗?
如果可以需要怎麽做呢?
※ 编辑: rul3nk41 (114.36.3.151), 05/31/2017 18:55:03
3F:→ Kanyahi: insert into table1 %s values %s where not exists ()试 05/31 20:46
4F:→ Kanyahi: 试 05/31 20:46
5F:→ Kanyahi: insert into values的方式 无法where 所以无法使用exists 06/01 11:14
6F:→ Kanyahi: 可能有其他的写法是我不知道的@@a 06/01 11:18
7F:→ smlboby: if not exist (...) insert into ... 06/06 07:07
好像还是有地方出问题,公司的前辈已经用不同方式处理掉了!
我会再去研究一下,谢谢各位大大的回答
如果有成功再回来报告!
※ 编辑: rul3nk41 (1.160.6.16), 06/09/2017 17:29:41