I'm trying to add in a delete statement into an existing IF statement.

I have tried adding the statement at the beginning and at the end but it doesn't work.

This is the existing code I have created for the update and insert statement.

IF EXISTS (SELECT * FROM [int_SocialAccess].[dbo].[Authorisers] where ID = {ID})
update [int_SocialAccess].[dbo].[Authorisers] set 
        Name = {Name},
        Email = {Email},
        Responsibility = {Responsibility},
        IsHeadOfService = {IsHeadOfService},
        DeleteRec = {DeleteRec}
    where ID = {ID}
ELSE
    insert into [int_SocialAccess].[dbo].[Authorisers] 
    values( {Name},
        {Email},
        {Responsibility},
        {IsHeadOfService},
        'no')

I am trying to add in my delete statement below right before the update statement, if the "DeleteRec" field is yes I want to delete the record otherwise update it.

IF {DeleteRec} = 'yes'
    DELETE FROM [int_SocialAccess].[dbo].[Authorisers]
    where ID = {ID}
ELSE

Any indication would be great.

1 Answers

4
FizzBuzz On Best Solutions

If you need more than one stmt in an if-else block, you need to wrap with BEGIN END -

IF EXISTS (SELECT * FROM [int_SocialAccess].[dbo].[Authorisers] where ID = {ID})
BEGIN
    IF {DeleteRec} = 'yes'
        DELETE FROM [int_SocialAccess].[dbo].[Authorisers] where ID = {ID}
    ELSE
         Update
END
ELSE
    insert into [int_SocialAccess].[dbo].[Authorisers] 
    values( {Name},
        {Email},
        {Responsibility},
        {IsHeadOfService},
        'no')