title: Using while loop in T-SQL function
body: <p>Non-database programmer here. It happens so, that I need to create a function in T-SQL which returns workdays count between given dates. I believe that the easiest how it's done is with <strong>while</strong> loop. Problem is, that as soon as I write something like</p>
  
  <pre><code>while @date &lt; @endDate
  begin
  
  end
  </code></pre>
  
  <p>the statement won't execute, claiming "incorrect syntax near the keyword 'return'" (not very helpful). Where's the problem?</p>
  
  <p>P.S. Full code:</p>
  
  <pre><code>ALTER FUNCTION [dbo].[GetNormalWorkdaysCount] (
  @startDate DATETIME,
  @endDate DATETIME
  )   
  RETURNS INT
  
  AS
  BEGIN
      declare @Count INT,
              @CurrDate DATETIME
      set @CurrDate = @startDate
  
      while (@CurrDate &lt; @endDate)
      begin
  
      end
  
      return @Count
  END
  GO
  </code></pre>