title: If Else If In a Sql Server Function
body: <p>I have this function I am trying to create. When I parse it, it works fine, but to actually create the function in the database it says my column names are invalid. That is not true, I spelled them correctly. Here is the code:</p>
  
  <pre><code>ALTER FUNCTION [dbo].[fnTally] (@SchoolId nvarchar(50))
  RETURNS int
  
  AS 
  
  BEGIN 
  
  DECLARE @Final nvarchar
  IF EXISTS (
  
      SELECT 
          question, 
          yes_ans, 
          no_ans, 
          na_ans, 
          blank_ans 
      FROM dbo.qrc_maintally 
      WHERE school_id = @SchoolId 
  
  )
  
      IF yes_ans &gt; no_ans AND yes_ans &gt; na_ans 
      BEGIN
          SET @Final = 'Yes'
      END
  
      ELSE IF no_ans &gt; yes_ans AND no_ans &gt; na_ans 
      BEGIN
          SET @Final = 'No'
      END
  
      ELSE IF na_ans &gt; yes_ans AND na_ans &gt; no_ans 
      BEGIN
          SET @Final = 'N/A'
      END
  
  RETURN @Final
  
  END
  </code></pre>