Before insert check if record exists? RRS feed

  • Question

  • User-1104215994 posted

    Hi guys,

    I have a console app and I am retrieving data from an API every 2 hours. I would like to insert data if only new into this table but I wonder if there is a way to check data before inserting into table. If I have over 100 records every fetch from this API and I don't want to manually check one record at a time to see if data in a certain column already exist, how do I write the query to check if data in my records already exist before inserting?

    Thanks in advance.

    Saturday, February 27, 2021 6:53 AM

All replies

  • User475983607 posted

    You did not share the code so we can only guess.  The following is basic T-SQL.

    IF( NOT EXISTS(SELECT (1) FROM [Production].[Product] WHERE [ProductNumber] = 'AR-5382'))
    	print 'Does not exist'

    Saturday, February 27, 2021 12:19 PM
  • User-1104215994 posted

    I will get these data below from the API;

    id, customerId, firstName, lastName, phone, productCode, orderDate (unix epoch time)

    I will use raw SQL in my app, it's much simpler for me.

    Saturday, February 27, 2021 3:08 PM