title: How can I use if statement after a CTE (SQL Server 2005)
body: <p>Last night I was writing a simple T-SQL program something like this</p>
  
  <pre><code>DECLARE @ROLEID AS INT
  
  SELECT @ROLEID = [ROLE ID] FROM TBLROLE
  
  ;WITH CTE
  AS
  ( 
      SELECT * FROM SOMETABLE
  )
  IF (@ROLEID  = 1) 
  BEGIN
        //SOMECODE
  END
  ELSE IF(@ROLEID  = 2) 
  BEGIN
        //SOMECODE
  END
  ELSE
  BEGIN 
        //SOMECODE
  END
  </code></pre>
  
  <p>I found after compilation that it is throwing error something like "Incorrect statement near if" </p>
  
  <p>What is wrong?</p>
  
  <p>However, I did that by using some other way. But I wanted to know why it did not work!</p>