作者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/m.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