|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, May 23, 2012 6:47 AM
Points: 15,
Visits: 134
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 9:45 AM
Points: 2,163,
Visits: 2,151
|
|
Interesting question, I got it right by process of elimination. (Too lazy to do the math tonight.)
However, I probably wouldn't use that method to populate a date table. The numbers table or a cross join are probably faster. (And I think easier.)
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, June 07, 2013 2:42 PM
Points: 1,662,
Visits: 1,710
|
|
This example shows how to quickly populate a table with successive dates? Not really, it shows a pretty convoluted way of doing it, not quick. Any time there is a while loop in T-SQL script, such a script is probably not efficient because SELECT is already a loop, and any attempts to use the slow while loop instead of a native select typically lead to performance issues. How about using a simple select, i.e.
declare @dtFrom datetime; declare @dtTo datetime; declare @diff int;
select @dtFrom = '2010-01-01T00:00:00.000', @dtTo = '2010-12-31T00:00:00.000', @diff = datediff(day, @dtFrom, @dtTo) + 1; create table #Dates ( [Date] datetime not null primary key );
-- this happily inserts dates exactly between first and last date, no more and no less insert into #Dates select top (@diff) dateadd(day, row_number() over (order by [object_id]) - 1, @dtFrom) from sys.all_columns; select * from #Dates;
drop table #Dates; Oleg
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, June 13, 2013 8:50 PM
Points: 3,208,
Visits: 4,178
|
|
Very interesting technique.
But inserting extra data and then deleting them is not the best solution, because delete operations are expensive. It's better to add a condition in order to insert only necessary data:
INSERT INTO #Dates ( [Date], [SomeValueForDate] ) SELECT DATEADD(DAY, @n, [Date]) AS [Date], @n AS [SomeValueForDate] FROM #Dates WHERE DATEADD(DAY, @n, [Date]) <= @dtTo -- No need to delete data after that
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 6:47 AM
Points: 847,
Visits: 770
|
|
Nice question, thanks also Oleg's script looks great. Here is something that has the same result:
DECLARE @dtFrom DATETIME DECLARE @dtTo DATETIME SET @dtFrom = '2010-01-01T00:00:00.000' SET @dtTo = '2010-12-31T00:00:00.000'
DECLARE @Diff INT SET @Diff = DATEDIFF(DAY, @dtFrom, @dtTo)
CREATE TABLE #Dates ( [Date] DATETIME NOT NULL PRIMARY KEY, [SomeValueForDate] INT NOT NULL )
CREATE TABLE #Digits(digit INT NOT NULL PRIMARY KEY);
INSERT INTO #Digits(digit) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
SELECT DATEADD( DAY, D3.digit * 100 + D2.digit * 10 + D1.digit , @dtFrom) AS n FROM #Digits AS D1 CROSS JOIN #Digits AS D2 CROSS JOIN #Digits AS D3 WHERE D3.digit * 100 + D2.digit * 10 + D1.digit <= @Diff ORDER BY n;
DROP TABLE #Digits DROP TABLE #Dates
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 9:21 PM
Points: 6,388,
Visits: 8,290
|
|
While reading through this, I kept asking myself "What is the author trying to teach?". Unfortunately, I never did get a good answer to my question...
Wayne Microsoft Certified Master: SQL Server 2008 If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it! Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines, CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:45 PM
Points: 5,297,
Visits: 7,240
|
|
WayneS (12/9/2010) While reading through this, I kept asking myself "What is the author trying to teach?". Unfortunately, I never did get a good answer to my question... He did tell us in the expanation: "This example shows how to quickly populate a table with successive dates."
Unfortunately, this is not the best method. Fortunately, several better alternatives have been included in the discussion, so the objective of the author is achieved anyway
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: 2 days ago @ 12:30 AM
Points: 1,788,
Visits: 3,330
|
|
I don't like questions like this, and not because I got it wrong. It teaches me nothing about Sql Server or T-SQL (except debugging peculiar T-SQL), and to be able to answer it correctly, without cheating, you will have to manually write down how many rows are inserted in each run of the loop in order to keep track of the number in the meaningless "SomeNumberForDate" column.
64% got it right. Yeah, right!! How many cheated and ran the T-SQL code?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 9:58 PM
Points: 1,063,
Visits: 4,260
|
|
I think as long as people learn does it matter if they ran the code? 
This WHILE was very convoluted so I ran the code to see if I could understand what it was doing and looked at the values in #Dates before the DELETE.
So I hope I learnt that a) there are ways of populating test data quickly and b) not to use complicated WHILE loops!
|
|
|
|