IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME = 'StuWebName')DROP TABLE StuWebNameGOCREATE TABLE StuWebName(StuId INT IDENTITY(1,1) PRIMARY KEY,StuName VARCHAR(50) NOT NULL,StuPassword VARCHAR(50) NOT NULL,StuSex CHAR(2) DEFAULT('男'),StuNo INT NOT NULL)GODECLARE @ID INTSET @ID=1WHILE(@ID<100)BEGININSERT INTO StuWebName(StuName,StuPassword,StuSex,StuNo)VALUES('張三'+CONVERT(VARCHAR(50),@ID),'123','男',0902)SET @ID=@ID+1ENDGOselect * from StuWebNameGO-------------------------------------------------------幾種最常見的排序排序(第一頁每頁五條資料)-------------------------------------------------------先用row_number函數在每列加序號然後排序SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY StuId) AS b,* FROM StuWebName) AS aWHERE b>(2-1)*5 and b<=2*5 GO--先查詢前10行,然後先倒序在取前5行,最後在倒序(將資料倒回來)SELECT * FROM (SELECT TOP 5 * FROM (SELECT TOP 10 * FROM StuWebName ORDER BY StuId ASC) AS T ORDER BY StuId DESC)AS T ORDER BY StuId ASCGO--先查詢前6行,讓後取StuId的最大值SELECT TOP 5 *FROM StuWebNameWHERE (StuId >=(SELECT MAX(StuId)FROM (SELECT TOP ((5*1)+1) StuIdFROM StuWebName ORDER BY StuId) AS T))ORDER BY StuIdGO