Q: T-SQL and decision making constructs Please help me with the syntax of this query. It doesn't compile. It says there's a syntax error near the keyword END. 
Obviously, I've got these BEGINs and ENDS mixed up.
I am using Microsoft SQL Server 2008 R2. I am not sure of the syntax of these BEGINS and ENDs.
Please don't mind the condition 1 = 0. That's something that will be replaced with a proper predicate later.
IF EXISTS (SELECT * FROM StringCategory WHERE ResourceKeyId = 18134 AND CategoryId = 0)
    BEGIN
        UPDATE StringCategory
        SET CategoryId = 0
        WHERE ResourceKeyId = 18134
    END
ELSE
    BEGIN
        IF 1 = 0
            BEGIN
            DELETE FROM StringCategory WHERE ResourceKeyId = 18134
            END
        ELSE
            BEGIN
            INSERT INTO StringCategory
            VALUES(18134, 0)
            END
    END
END


A: Your last END is an extra.  You can think of the BEGINs and ENDs like { and } in C# for the IF constructs (They serve to mark the beginning and end of the block to be executed in the IF/ELSE statement).

A: Here is a much simpler way to do what you appear to be attempting.
  insert into StringCategory
  (ResourceKey, CategoryId)
  select 18134, 0
  where not exists (
  SELECT * 
  FROM StringCategory 
  WHERE ResourceKeyId = 18134 
  AND CategoryId = 0)

