As you haven't said which version of SQL Server you are targetting, I'm assuming it's a later version - which is handy because they provide a very useful
MERGE[
^] command which allows you to perform what are sometimes known as upserts.
An upsert is a SQL command that can either be an
insert
or an
update
, depending on the selection criteria.
An extra point - never concatenate strings to create SQL statements. You leave yourself wide open to SQL Injection attacks.