title: Why am I getting the error message "TOP or FETCH clause contains an invalid value"?
body: <p>I am using <code>SQL Server 2012</code> and I have the following <code>T-SQL</code> query. However, it is giving me an error message when executed.</p>
  
  <p>The error message is : </p>
  
  <pre><code>Msg 1014, Level 15, State 1, Line 2
  A TOP or FETCH clause contains an invalid value.
  </code></pre>
  
  <p>My <code>T-SQL</code> query stands as follows:</p>
  
  <pre><code>;WITH N(N)AS 
  (SELECT 1 FROM(VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))M(N)),
  tally(N)AS(SELECT 1 FROM N,N a,N b,N c,N d)
  
  INSERT INTO Allot4
  
  SELECT
         b.Date as [TDate],
         Season,
         RN,
         TAProfileID,
         TOName,
         Market,
         RoomType,
         Property,     
         TOType
  
      FROM Allot3 a
      CROSS APPLY
      ( 
        SELECT top(datediff(d,Datefrom,case when DateTo &gt;= DateFrom
                   then dateadd(d, 1, DateTo) else DateFrom end))
          DATEADD(d,row_number()over(order by 1/0)-1, DateFrom) Date
        FROM tally
      ) b
  </code></pre>
  
  <p>What's wrong here and how do I solve it?</p>