TSQL generation sequence, date sequence

Source: Internet
Author: User

--Method 1SETNOCOUNT on UsetempdbGOIF object_id('dbo.nums') is  not NULLDROP TABLEdbo.nums;GOCREATE TABLEDbo.nums (nINT  not NULL PRIMARY KEY);DECLARE @max  as INT,@rc  as INTSET @max=1000000SET @rc=1INSERT  intoNumsVALUES(1); while @rc*2<=@maxBEGIN    INSERT  intoNumsSELECTN+@rc  fromNums; SET @rc=@rc*2END--SELECT [email protected] from nums WHERE [email protected]<[email protected]GODECLARE @s  as DATETIME,@e  as DATETIMESET @s='20140501'SET @e='20141231'SELECT @s+N-1  asDt fromNumsWHEREN<=DATEDIFF( Day,@s,@e)+1SELECT *  fromNumsWHEREN<TenGO--Method 2IF object_id('fn_nums') is  not NULLBEGIN     PRINT 'dropping function Fn_nums'    DROP FUNCTIONfn_numsIF @ @ERROR = 0 PRINT 'Function Fn_nums dropped'ENDGoCREATE FUNCTIONFn_nums (@n  as BIGINT) RETURNS TABLE     as RETURN withL0 as(SELECT 1  asCUNION  All SELECT 1), L1 as(SELECT 1  asC fromL0 asA,l0 asb), L2 as(SELECT 1  asC fromL1 asA,l1 asb), L3 as(SELECT 1  asC fromL2 asA,l2 asb), L4 as(SELECT 1  asC fromL3 asA,l3 asb), L5 as(SELECT 1  asC fromL4 asA,l4 asb), Nums as(SELECTRow_number () Over(ORDER  byC asN fromL5)SELECTN fromNumsWHEREN<=@nGODECLARE @s  as DATETIME,@e  as DATETIMESET @s='20140501'SET @e='20141231'SELECT @s+N-1  asDt fromDbo.fn_nums (DATEDIFF( Day,@s,@e)) asnums;SELECT *  fromDbo.fn_nums (Ten) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.