[SQL Server] How to obtain the serial number

Source: Internet
Author: User

-- SQL2000

 

-- 1

Select number from Master... spt_values where type = 'P' -- 0-255

 

-- 2
Select top 10000 id = identity (INT,) into # T from sysobjects, syscolumns

 

-- Sql2005 generate series numbers (row numbers)
-- 1. Recursive test with CTE
; With T
(
Select 1 as num
Union all
Select num + 1
From t
Where num <100000
)
Select * from t
Option (maxrecursion 0)

-- 2. Generate a row number using the system table
Select top 100000 num = row_number () over (order by getdate ())
From syscolumns A, syscolumns B

-- 3. Generate a digital table with High Efficiency
Create Function DBO. fn_nums (@ n as bigint)
Returns table
As
Return
With
T1 as (select 1 as C Union all select 1 ),
T2 as (select 1 as C from T1 as A, T1 as B ),
T3 as (select 1 as C from T2 as A, T2 as B ),
T4 as (select 1 as C from T3 as A, T3 as B ),
T5 as (select 1 as C from T4 as A, T4 as B ),
T6 as (select 1 as C from T5 as A, T5 as B ),
T7 as (select row_number () over (order by C) as N from T6)
Select n From T7 where n <@ n;
Go
-- Test

Select * From DBO. fn_nums (1000)

Related Article

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.