title: Return value in SQL Server stored procedure
body: <p>I have a stored procedure that has an if statement in it. If the number of rows counted is greater than 0 then it should set the only output parameter <code>@UserId</code> to 0</p>
  
  <p>However it only returns a value in the second part of the query.</p>
  
  <pre><code>@EmailAddress varchar(200),
  @NickName varchar(100),
  @Password varchar(150),
  @Sex varchar(50),
  @Age int,
  @EmailUpdates int,
  @UserId int OUTPUT
  IF 
      (SELECT COUNT(UserId) FROM RegUsers WHERE EmailAddress = @EmailAddress) &gt; 0
      BEGIN
          SET @UserId = 0
      END
  ELSE
      BEGIN
          INSERT INTO RegUsers (EmailAddress,NickName,PassWord,Sex,Age,EmailUpdates) VALUES (@EmailAddress,@NickName,@Password,@Sex,@Age,@EmailUpdates)
          SELECT SCOPE_IDENTITY()
      END
  
  END
  </code></pre>