1

I have a LeaveCounter table like such :

Login | ActivityId | Value | Date

And when I load my page it fills it up with a new list, but I am looking for a way to avoid duplicate INSERTs.

Here is my SQL Server 2008 statement with the variables :

string sql = @"INSERT INTO LeaveCounter VALUES ('" + employees[y] + "', '" + ActivityId[z] + "', 0 , " + yearlist[i] + ")";

Or simply :

INSERT INTO LeaveCounter VALUES (LOGIN, ACTIVITYID, 0, DATE)

How may I alter this call to make it so that it won't insert twice a row such as :

chris | 2 | 0 | 2015

So that It will add any new members of my list and simply skip existing ones?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Make unique index on those columns – Giorgi Nakeuri Nov 12 '15 at 10:26
  • Write a stored-procedure. You should also create a unique index with the relevant columns. – Tim Schmelter Nov 12 '15 at 10:26
  • Then I will learn about stored-procedures, but what call should I use to see if the INSERT is a duplicate? –  Nov 12 '15 at 10:28
  • 3
    [SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - you should **not** concatenate together your SQL statements - use **parametrized queries** instead to avoid SQL injection – marc_s Nov 12 '15 at 10:31
  • 2
    Possible duplicate of [SQL Server Insert if not exist](http://stackoverflow.com/questions/20971680/sql-server-insert-if-not-exist) – Jiri Tousek Nov 12 '15 at 10:56

0 Answers0