title: t sql "select case" vs "if ... else" and explaination about "begin"
body: <p>I have few experiences with t sql and I have to write a stored.</p>
  
  <p>This is my stored:</p>
  
  <pre><code>USE myDatabase
  GO
  
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  
  CREATE PROCEDURE [dbo].[myStored]
  (
  @myPar1 INT,
  @myPar2 SMALLDATETIME
  )
  
  AS
  
  BEGIN
    SET NOCOUNT ON
  
    IF EXISTS (
              SELECT 
              1 
  
              FROM 
              myTable1 
  
              WHERE 
              myPar1 = @myPar1
              AND myPar2 = @myPar2
              )
  
      BEGIN
        DELETE FROM  
        myTable1  
  
        WHERE 
        myPar1 = @myPar1
        AND myPar2 = @myPar2
      END
  
    ELSE 
  
      IF EXISTS (
                SELECT 
                1 
  
                FROM 
                myTable2 
  
                WHERE 
                myPar2 = @myPar2
                )
  
        BEGIN
          INSERT INTO  
          myTable1
          (myField1, myField2, myField3, myField4)
  
          VALUES
          (@myPar1, @myPar2, '', 1)
        END    
  
      ELSE
  
        IF EXISTS (
                  SELECT 
                  1 
  
                  FROM 
                  myTable3 
  
                  WHERE 
                  myPar2 = @myPar2
                  )
  
          BEGIN
            INSERT INTO  
            myTable1
            (myField1, myField2, myField3, myField4)
  
            VALUES
            (@myPar1, @myPar2, '', 1)
          END
  END
  </code></pre>
  
  <p>And these are my questions:</p>
  
  <p>1 - Are there macroscopic errors?</p>
  
  <p>2 - Someone suggest to use "SELECT CASE" someone else to use "IF ... ELSE", what's the difference? And what is the best option for my stored?</p>
  
  <p>3 - I'm not sure about the use of the "BEGIN ... END" statement, in particular in combination with "IF ... ELSE" statement. What does it mean? Is it necessary to put "BEGIN ... END" inside the "IF ... ELSE" statement? Also for executing a single instruction?</p>