SQLSERVER无排序生成序号

--实现方式:ROW_NUMBER()
SELECT RowID=(ROW_NUMBER() OVER(ORDER BY(SELECT 0))) FROM dbo.tbl_name

--实现方式:IDENTITY
SELECT RowID=IDENTITY(INT,1,1) INTO #tb1 FROM dbo.tbl_name
SELECT * FROM #tb1
DROP TABLE #tb1

/*无排序表1-表2,单价统计*/

--实现方式:ROW_NUMBER()
SELECT TOP 10 Price=a.Price-b.Price FROM
(SELECT RowID=(ROW_NUMBER() OVER(ORDER BY(SELECT 0))),Price FROM dbo.tbl_name) a 
LEFT JOIN
(SELECT RowID=(ROW_NUMBER() OVER(ORDER BY(SELECT 0))),Price FROM dbo.tbl_name WHERE CID>1000) b ON a.RowID=b.RowID


--实现方式:IDENTITY
SELECT RowID=IDENTITY(INT,1,1),Price INTO #tb1 FROM dbo.tbl_name
SELECT RowID=IDENTITY(INT,1,1),Price INTO #tb2 FROM dbo.tbl_name WHERE CID>1000
SELECT TOP 10  Price=#tb1.Price-#tb2.Price FROM #tb1,#tb2 WHERE #tb1.RowID=#tb2.RowID
DROP TABLE #tb1,#tb2


你可能感兴趣的:(sql,server)