哪里不存在导致插入db2的问题

时间:2014-06-05 18:52:39

标签: db2

我有这样的查询:

insert into mySchema.myTable (award_id, cust_id) values ('blahblah', 12345)
where not exists (select * from mySchema.myOtherTable where cust_id = 12345);

我收到错误:

ILLEGAL USE OF KEYWORD WHERE.  TOKEN FOR <END-OF-STATEMENT> NOT ATOMIC WAS EXPECTED SQL 
Code: -199, SQL State: 42601

我已经看到一堆类似的查询被接受为答案,我不明白为什么它会发现这个问题。

3 个答案:

答案 0 :(得分:5)

这将有效:

insert into mySchema.myTable (award_id, cust_id) 
select 'blahblah', 12345
from sysibm.sysdummy1
where not exists (select * from mySchema.myOtherTable where cust_id = 12345);

sysibm.sysdummy1的替代方法是:

insert into mySchema.myTable (award_id, cust_id) 
select 'blahblah', 12345
from ( values (1) )
where not exists (select * from mySchema.myOtherTable where cust_id = 12345);

答案 1 :(得分:1)

我不确定,但你可以像下面这样做

INSERT INTO mySchema.myTable (award_id, cust_id) 
SELECT 'blahblah', 12345
WHERE NOT EXISTS
(
SELECT 1 FROM mySchema.myOtherTable WHERE cust_id = 12345
)

(OR)

IF NOT EXISTS (SELECT 1 FROM mySchema.myOtherTable WHERE cust_id = 12345)
BEGIN
    INSERT INTO mySchema.myTable (award_id, cust_id) 
    VALUES('blahblah', 12345)
END

答案 2 :(得分:1)

insert into mySchema.myTable (award_id, cust_id) 
(select 'blahblah', 12345 from sysibm.sysdummy1
where not exists (select * from mySchema.myOtherTable where cust_id = 12345));

这最终起作用了。