Insert only if no rows match???

N

Noozer

Is there an easy way to add a row to a table when a matching row does not
exist?

ie. INSERT INTO Counters (Month, Forks, Spoons, Knives) Values (5,0,0,0) if
there are no rows with month 5
 
D

Douglas J Steele

Well, if you've got a unique index on Month, you could try doing the insert
and it will fail.

Alternatively, you could have a table that contains all the months. Do a
Left Join between this Months table and Counters, and return those rows
where Month is Null in the Counters table. That'll let you know which months
you have to insert.

BTW, you shouldn't name a field Month: it's a reserved word, and you can run
into all sorts of problems using reserved words for your own purposes.
 
Top