Insert only if no rows match???

  • Thread starter Thread starter Noozer
  • Start date Start date
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top