我试图在每次执行时选择并将X行插入到临时表中,我已经对行进行了编号。但是,我尝试使用PERCENT和TOP但没有得到满意的结果。
注意:我并不是要在特定数字之间获取特定行数。我只需要获得一个修正号码,如果可用行数低于选择请求,那么得到更少但不要超过。
我分配了这样的行号:
WITH GetRecordByIncrement AS
(
SELECT
[RowNumber] = ROW_NUMBER() OVER (ORDER BY UserID ASC)
,[Column1]
,[Column2]
,[Column3]
FROM MySchema.MyTable
)
我如何获得,例如:100行或更少行?使用行号时无效。
答案 0 :(得分:0)
这样的事情会起作用:
DECLARE @table TABLE ( c1 INT, c2 INT, c3 INT );
;
WITH MyRows
AS ( SELECT TOP 100
ROW_NUMBER() OVER ( ORDER BY message_id ) AS RowNum ,
message_id ,
severity
FROM sys.messages
)
INSERT INTO @Table
( c1 ,
c2 ,
c3
)
SELECT RowNum ,
message_id ,
severity
FROM MyRows;
SELECT *
FROM @table;
答案 1 :(得分:0)
试试这个:
WITH GetRecordByIncrement AS
(
SELECT
[RowNumber] = ROW_NUMBER() OVER (ORDER BY UserID ASC)
,[Column1]
,[Column2]
,[Column3]
FROM MySchema.MyTable
)
select * from GetRecordByIncrement where [RowNumber]<=desirednumber
WITH GetRecordByIncrement AS
(
SELECT
[RowNumber] = ROW_NUMBER() OVER (ORDER BY UserID ASC)
,[Column1]
,[Column2]
,[Column3]
FROM MySchema.MyTable
)
select * from GetRecordByIncrement where [RowNumber]<=desirednumber