edit: i like this comment on the original answer: sackoverflow.com/questions/418898/… – zapl 7 mins ago. I didn't know you could do that. It's very efficient looking....
i did this quick. hopefully you get the idea... so those variables and stuff would be in the non sql code. i suggest using a stored procedure or something like that if you can.... or separate out all the sections entirely (see if it exists, based on that do this sql, or if it doesn't do this sql).
with just the update statement.... nothing will update if the variables don't exist in the table. and with just the insert statement... nothing will insert if the variables do exist in the table. besides those two statements, there doesn't need to be an if or anything to actually check to see if something exists in answers or not.
create table #answers (question_id int, player int, level int, other_field int)
insert into #answers values (1,1,1,1)
insert into #answers values (2,1,1,1)
declare @question_id int
declare @player int
declare @level int
declare @other_field int
set @question_id=1
set @player=1
set @level=1
set @other_field=1
-- if it exists already
update a
set other_field=@other_field
from #answers as a
where QUESTION_ID=@question_id and
PLAYER=@player and
other_field<>@other_field
set @question_id=4
set @player=4
set @level=1
set @other_field=1
-- if it doesn't exist
insert into #answers (question_id, player, level, other_field)
select x.QUESTION_ID, x.player, @level, @other_field
from #answers a
right outer join
(select @question_id as QUESTION_ID,
@player as player) as x
on x.QUESTION_ID=a.QUESTION_ID and
x.player=a.player
where a.player is null and a.question_id is null
or this for the if it doesn't exist (messier but shorter)
-- if it doesn't exist
insert into #answers (question_id, player, level, other_field)
select distinct @QUESTION_ID, @player, @level, @other_field
from #answers
where not exists (select 1 from #answers where
QUESTION_ID=@question_id and
PLAYER=@player )