The query entry must contain at least one table or query

I have an access request that would suggest checking if an item 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 got this error earlier, but usually it is when there is no table, for example, if you "select * from the test table" and you type "Select" and leave the from clause, you will get the same error. But do I have a table? Maybe mine, where there is no syntax, is wrong?

enter image description here

Edit:

Ok, I added the "Double" table, as suggested with a copy of the code inserted from this question: UNION query without tables in MS Access (Jet / ACE)

Trying to add a restriction, as shown, gave me this error: enter image description here

after I click OK, it highlights the word "Check"

I have never encountered restrictions (access at least ..) my syntax is probably incorrect

Edit 2:

Adding Constraints Using the 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 click run ...

enter image description here

+6
source share
2 answers

This is one of those cases where it is useful to use the Dual table. The Dual table is a single row table that can be used in the FROM clause of a query when you really don't need the source table, but the SQL parser insists that it is.

Some database systems (such as Oracle) provide the Dual virtual table as β€œstandard hardware,” but in Access we need to create our own. For a great description of the process, check out the HansUp answer here .

So, as soon as you have the [Double] table, i.e.

 id -- 1 

... then you can use this query by executing your INSERT (or not ...):

 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 
+7
source

try this you can use simple where clause

 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'); 
0
source

Source: https://habr.com/ru/post/945772/


All Articles