2

我的表包含大约 5000 行。我想知道表格中的特定行位置。我的存储过程是:

 CREATE PROC Sp_getrownumber(@MemberID INT) 
AS 
    SELECT Row_number() 
             OVER (  ORDER BY joiningdate ASC) AS Rownumber, 
           M.memberid, 
           P.name, 
           M.joiningdate 
    FROM   tblmemberdata M 
           JOIN tblpersonaldetails P 
             ON M.memberid = P.memberid 

但是当我尝试 2 获取特定行时,行号显示为 1。代码是:

 CREATE PROC Sp_getrownumber(@MemberID INT) 
AS 
    SELECT Row_number() 
             OVER (  ORDER BY joiningdate ASC) AS Rownumber, 
           M.memberid, 
           P.name, 
           M.joiningdate 
    FROM   tblmemberdata M 
           JOIN tblpersonaldetails P 
             ON M.memberid = P.memberid 
    WHERE  M.memberid = @MemberID

如何获取确切的行号?

4

3 回答 3

2

将其修改为:

alter proc SP_GetRowNumber
(@MemberID int)
as
SELECT * FROM (
  SELECT
ROW_NUMBER() OVER (ORDER BY JoiningDate ASC) AS Rownumber,
M.MemberID,P.Name,M.JoiningDate
FROM tblMemberData M join tblPersonalDetails P on M.MemberID=P.MemberID
) AS Counts
WHERE MemberID = @MemberID
于 2013-04-24T12:12:09.070 回答
2

您所看到的是因为ROW_NUMBER()适用于本地,而不适用于整个桌子!SELECT

试试这个:

CREATE PROC Sp_getrownumber(@MemberID INT) 
AS
    SELECT *
    FROM (
        SELECT Row_number() 
                 OVER (ORDER BY joiningdate ASC) AS Rownumber, 
               M.memberid, 
               P.name, 
               M.joiningdate 
        FROM   tblmemberdata M 
        JOIN tblpersonaldetails P 
               ON M.memberid = P.memberid
    ) tblRowCounter
    WHERE M.memberid = @MemberID;

此代码完成的是对保留原始行号的内存表执行过滤。

然而!请注意, SQL中没有行编号。SQL 只知道有一大堆数据属于一个表。子句ORDER BY joiningdate ASC里面的语句非常重要!更改该列或添加更多列可能会更改行号。OVER

于 2013-04-24T12:14:46.737 回答
1
CREATE PROC Sp_getrownumber(@MemberID INT) 
AS 
    ;WITH Numbered AS (
      SELECT Row_number() 
               OVER (  ORDER BY joiningdate ASC) AS Rownumber, 
             M.memberid, 
             P.name, 
             M.joiningdate 
       FROM   tblmemberdata M 
           JOIN tblpersonaldetails P 
             ON M.memberid = P.memberid 
    )
    SELECT *
    FROM Numbered
    WHERE memberid = @MemberID
于 2013-04-24T12:14:47.237 回答