Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A current query I have has been causing me grief today. For any result set that is less than or equal to 2810 rows the query works, but it seems to cutoff processing at that rowcount.

Am I missing something in how this works?
SQL
DECLARE @fromDate DATE
DECLARE @toDate DATE

SET @fromDate = '2007-09-04'
SET @toDate = '2016-06-28'

SELECT DATEADD(DAY, nbr - 1, @FromDate), nbr -1, DATEDIFF(DAY, @FromDate, @ToDate)
FROM   (
          SELECT  ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS nbr
          FROM sys.columns c
       ) nbrs
WHERE nbr - 1 <= DATEDIFF(DAY, @FromDate, @ToDate)


What I have tried:

Not sure what to try. I can't seem to find a solution other than breaking the query into little bits which seems a little silly to me in this situation.
Posted
Updated 28-Jun-16 9:02am

1 solution

The sys.columns table contains a row for each column in each table in your database. If your query won't produce more than 2810 rows, that means you only have 2810 columns defined in your database.

There are various ways you could solve this. I'd be inclined to use Christian Graus's solution:
SQL Wizardry Part Eight - Tally Tables[^]
SQL
WITH E1(N) AS 
(
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
    -- 1*10^1 or 10 rows
),
E2(N) AS 
(
    SELECT 1 
    FROM E1 a, E1 b
     -- 1*10^2 or 100 rows
),
E4(N) AS 
(
    SELECT 1 
    FROM E2 a, E2 b
     -- 1*10^4 or 10000 rows
),
E8(N) AS 
(
    SELECT 1 
    FROM E4 a, E4 b
     -- 1*10^8 or 100000000 rows
),
E(N) AS
(
    SELECT Row_Number() OVER (ORDER BY n) 
    FROM E8
)
SELECT 
    DateAdd(day, N - 1, @FromDate), 
    N - 1, 
    DateDiff(day, @FromDate, @ToDate)
FROM
    E
WHERE 
    N < DateDiff(day, @FromDate, @ToDate)
;
 
Share this answer
 
Comments
Marcus Kramer 28-Jun-16 15:18pm    
Thanks Richard. I had completely forgotten about the tally tables method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900