0

I have a program that will load a database file but the data only will be inserted in a new table. My question is how to check if the database file is the same as inserted in the database how to reject it and just give a message "data exists".

My plan is to create a if exists query but it seems it doesn't work or my code doesn't work. by the way the table doesn't have an id /primary key.

"IF EXISTS(SELECT COUNT(*) FROM TABLE1) " & vbCrLf & 
"BEGIN" & vbCrLf & 
     "SELECT * FROM TABLE1" & vbCrLf & 
"END" & vbCrLf & 
"ELSE" & vbCrLf & 
"BEGIN" & vbCrLf &
    "INSERT INTO TABLE1 SELECT * FROM TABLE1_TEMP" & vbCrLf & 
"END"
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Welcome to Stack Overflow! For tips on writing great questions visit [How to ask](https://stackoverflow.com/help/how-to-ask). If you have any questions about the site, you can visit the [Help Center](https://stackoverflow.com/help) or visit [Meta Stack Overflow](https://meta.stackoverflow.com/) –  Jul 25 '17 at 03:22
  • https://stackoverflow.com/questions/108403/solutions-for-insert-or-update-on-sql-server – andy Jul 25 '17 at 11:33

1 Answers1

0

if you dont have primary key then you should choose field that will be your key then compare that with new data that you want to insert.

ex:

INSERT INTO DB

SELECT 'a','b' FROM DB

WHERE field1 + field2 <> 'a' + 'b'

this will check if db(field1 and field2) same as data that you want to insert a and b, if no then a and b will be inserted

note: if your field is integer then you should convert it first into string.

Denny Sutedja
  • 538
  • 4
  • 15