6

I have a query in access that is suppose to check whether the item already exists in the database before inserting it:

INSERT INTO FinalizedPrintedStickers
Values('0000846043-481-9-0',
'48IG - 1F Straight Panel                                                        ',
'481                               ',
'0',
'0',
'',
'210',
'Printed')
 WHERE NOT EXISTS(SELECT [SN] FROM FinalizedPrintedStickers Where SN = '0000846043-481-9-0')

Now, I've gotten this error before but usually it's when there's no table for example if you "select * from test table" and you type "Select *" and leave out the from clause you get the same error. But I have a table ? Perhaps my where not exists syntax is wrong?

enter image description here

Edit:

Ok, I've added a table "Dual" as suggested with code copy pasted from this question : Table-less UNION query in MS Access (Jet/ACE)

Attempting to add a constraint as shown gave me this error : enter image description here

after i press ok it highlights the word "Check"

I've never really dealt with constraints (in access atleast..) my syntax is probably wrong

Edit 2:

Adding constraints using ctrl G command

enter image description here

And when I press enter...

enter image description here

Adding constraints using ADO:

enter image description here

And when i press run...

enter image description here

4

2 回答 2

7

这是Dual表格可以提供帮助的情况之一。表是一个Dual单行表,当您并不真正需要源表但 SQL 解析器坚持存在时,可以在查询的 FROM 子句中使用它。

一些数据库系统(例如,Oracle)提供了一个Dual虚拟表作为“标准设备”,但在 Access 中我们需要创建自己的。有关该过程的出色描述,请查看 HansUp在此处的回答。

因此,一旦您的 [Dual] 表就位,即

id
--
 1

...然后您可以使用此查询执行您的 INSERT(或不...):

INSERT INTO FinalizedPrintedStickers
    (
        SN,
        Field2
    )
    SELECT 
        "0000846043-481-9-0" AS SN,
        "48IG - 1F Straight Panel" AS Field2
    FROM Dual
    WHERE DCount("SN","FinalizedPrintedStickers","SN=""0000846043-481-9-0""")=0
于 2013-05-24T13:58:56.573 回答
0

试试这个,你可以使用简单的 where 子句

INSERT INTO FinalizedPrintedStickers
    Values('0000846043-481-9-0',
    '48IG - 1F Straight Panel                                                        ',
    '481                               ',
    '0',
    '0',
    '',
    '210',
    'Printed')
     WHERE SN Not In(SELECT [SN] FROM FinalizedPrintedStickers Where SN = '0000846043-481-9-0');
于 2013-05-24T11:44:53.970 回答