SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO alter proc sp_SelectInfomationByKeyWord--创建一个存储过程 --定义变量 @keyWord nvarchar(20),--查询关键字 @min int ,--当前页面显示数据最小编号 @max int --当前页面显示数据最大编号 as declare @str nvarchar(1000);--定义一个字符串 --对字符串@str赋值 set @str = 'select * from (select ROW_NUMBER() over(order by ' + @keyWord + ') as oid ,* from Books) T where oid between ' + CAST(@min as nvarchar(100)) + ' and ' + CAST(@max as nvarchar(100)) --定义一个字符串,将sql语句赋值给该字符串 exec (@str)--执行该字符串
以前,学习Oracle的时候,印象里貌似是接触过一个ROWNUM的辅助列,通过这个参数可以对数据库信息进行分页,
哈哈哈哈,Sql Server也有辅助列。ROW_NUMBER() over(order by 需要用来排序的字段)
在尝试了很多次的select top n* from TABLE where id not in (select top n id from TABLE) 这样的子查询语句之后,我茫然了,要写一条sql语句都这么难...
猛然回首,突然发现,还是有一些可以用来温存的东西。
分页sql代码1:
select top [pageSize每页显示的个数] id, c_name,c_oname,c_id from Vjf_adimg where
id not in (select top ([pageSize每页显示的个数]*( [pageIndex当前页数] -1)) id from Vjf_adimg where
pt_oneId=(select id from p_type where pt_typeoname='feiliao') and len(a_picUrl)>0
order by id desc)
and pt_oneId=(select id from p_type where pt_typeoname='feiliao')
order by
id desc
分页sql代码2:
SELECT id, c_name,c_oname,c_id FROM
(SELECT ROW_NUMBER() OVER
( ORDER BY id desc)
AS OID , * FROM Vjf_adimg
WHERE pt_oneId=(select id from p_type where pt_typeoname='feiliao')) T
WHERE OID
BETWEEN ([pageSize每页显示的个数]*( [pageIndex当前页数] -1))+1
AND ([pageSize每页显示的个数]* [pageIndex当前页数]
代码1和代码2运行的结果是相同的!
哈哈,有更方便的代码了啊!