菜鸟求教存储过程!!!!!!!!!!
新建数据库表(track):
trackid(主键自增(1,1)) ,CKId(bigint) , CKm (float型),KKm(float型)
执行查询:
1:select *,square(CKm+kkm)*square(KKm/CKm) as num into # from track order by num asc
select * from # a where not exists(select 1 from # where CKid=a.CKid and num<a.num) order by num
2: 然后对查询出来的这些数据进行分页?
现在把1做存储过程了,如果要进行分页, 该怎么写?
坐等高手。
[解决办法]
GOOGLE. 分页存储过程...
[解决办法]
关于分页的存过(来自CSDN)=========================================================================*/CREATE PROCEDURE [dbo].[Basic_Pagination2005]@tblName nvarchar(200), --表名@fidlelist nvarchar(1000), --要查询字段@fldName nvarchar(100), --排序字段@PageSize int, --页尺寸@PageIndex int, --页码@IsReCount bit , -- 返回记录总数, 非 0 值则返回@OrderType bit, -- 设置排序类型, 非 0 值则降序@strWhere nvarchar(1000) --查询条件ASdeclare @sqlstr nvarchar(4000),@tmpwhere nvarchar(4000),@tmporder nvarchar(100)BEGINif @OrderType != 0beginset @tmporder = @fldName +' desc 'endelsebegin set @tmporder = @fldName +' asc 'endset @tmpwhere='';if(@strWhere!='')beginset @tmpwhere=' where '+@strWhere;endset @sqlstr=N'select * from(select '+@fidlelist+', ROW_NUMBER() OVER(orderby '+@tmporder+') as row from '+@tblName+@tmpwhere+') tmp where row between '+cast(((@PageIndex-1)*@PageSize+1) as nvarchar)+' and '+cast(@PageIndex*@PageSize as nvarchar); exec sp_executesql @sqlstrif @IsReCount != 0beginset @sqlstr=N'select count(*) as Total from '+ @tblName+@tmpwhereexec sp_executesql @sqlstr endEND--返回第三页EXECUTE [Basic_Pagination2005] 'SC','S#','GRADE',10,3,200,1,'GRADE IS NOT NULL' --返回第10页EXECUTE [Basic_Pagination2005] 'SC','S#','GRADE',10,10,200,1,'GRADE IS NOT NULL' DROP PROC [Basic_Pagination2005]
[解决办法]
row_number over(order by)
[解决办法]
关于分页的存过(来自CSDN)
=========================================================================*/
CREATE PROCEDURE [dbo].[Basic_Pagination2005]
@tblName nvarchar(200), --表名
@fidlelist nvarchar(1000), --要查询字段
@fldName nvarchar(100), --排序字段
@PageSize int, --页尺寸
@PageIndex int, --页码
@IsReCount bit , -- 返回记录总数, 非 0 值则返回
@OrderType bit, -- 设置排序类型, 非 0 值则降序
@strWhere nvarchar(1000) --查询条件
AS
declare @sqlstr nvarchar(4000),
@tmpwhere nvarchar(4000),@tmporder nvarchar(100)
BEGIN
if @OrderType != 0
begin
set @tmporder = @fldName +' desc '
end
else
begin
set @tmporder = @fldName +' asc '
end
set @tmpwhere='';
if(@strWhere!='')
begin
set @tmpwhere=' where '+@strWhere;
end
set @sqlstr=N'select * from
(select '+@fidlelist+', ROW_NUMBER() OVER(order
by '+@tmporder+') as row from '+@tblName+@tmpwhere+')
tmp where row between '+cast
(((@PageIndex-1)*@PageSize+1) as nvarchar)+' and '+cast
(@PageIndex*@PageSize as nvarchar);
exec sp_executesql @sqlstr
if @IsReCount != 0
begin
set @sqlstr=N'select count(*) as Total from '+ @tblName+@tmpwhere
exec sp_executesql @sqlstr
end
END
--返回第三页
EXECUTE [Basic_Pagination2005] 'SC','S#','GRADE',10,3,200,1,'GRADE IS NOT NULL'
--返回第10页
EXECUTE [Basic_Pagination2005] 'SC','S#','GRADE',10,10,200,1,'GRADE IS NOT NULL'
DROP PROC [Basic_Pagination2005]
------解决方案--------------------
2000用TOP 2005用ROW_NUMBER()OVER
[解决办法]
http://topic.csdn.net/u/20100203/17/8F916471-597D-481A-B170-83BCEFE3B199.html应一个朋友的要求,贴上收藏的SQL常用分页的办法~~ 表中主键必须为标识列,[ID] int IDENTITY (1,1) 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: SELECT TOP 页记录数量 * FROM 表名 WHERE (ID NOT IN (SELECT TOP (每页行数*(页数-1)) ID FROM 表名 ORDER BY ID)) ORDER BY ID //自己还可以加上一些查询条件 例: select top 2 * from Sys_Material_Type where (MT_ID not in (select top (2*(3-1)) MT_ID from Sys_Material_Type order by MT_ID)) order by MT_ID 2.分页方案二:(利用ID大于多少和SELECT TOP分页) 语句形式: SELECT TOP 每页记录数量 * FROM 表名 WHERE (ID > (SELECT MAX(id) FROM (SELECT TOP 每页行数*页数 id FROM 表 ORDER BY id) AS T) ) ORDER BY ID 例: SELECT TOP 2 * FROM Sys_Material_Type WHERE (MT_ID > (SELECT MAX(MT_ID) FROM (SELECT TOP (2*(3-1)) MT_ID FROM Sys_Material_Type ORDER BY MT_ID) AS T)) ORDER BY MT_ID 3.分页方案三:(利用SQL的游标存储过程分页) create procedure SqlPager @sqlstr nvarchar(4000), --查询字符串 @currentpage int, --第N页 @pagesize int --每页行数 as set nocount on declare @P1 int, --P1是游标的id @rowcount int exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页 set @currentpage=(@currentpage-1)*@pagesize+1 exec sp_cursorfetch @P1,16,@currentpage,@pagesize exec sp_cursorclose @P1 set nocount off 4.总结: 其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。 建议优化的时候,加上主键和索引,查询效率会提高。 通过SQL 查询分析器,显示比较:我的结论是: 分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句 分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句 分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用