或者在原表上加个行号字段,你自己写个触发器来维护行号字段--自已做行号的例子:--创建得到最大id的函数 create function f_getid() returns int as begin declare @id int select @id=max(id) from tb set @id=isnull(@id,0)+1 return(@id) end go--创建表 create table tb(id int default dbo.f_getid() primary key,name varchar(10)) go--创建触发器,在删除表中的记录时,自动更新记录的id(**如果不要此功能,则删除此触发器) create trigger t_delete on tb AFTER delete as declare @id int,@mid int select @mid=min(id),@id=@mid-1 from deleted update tb set id=@id,@id=@id+1 where id>@mid go--插入记录测试 insert into tb(name) values('张三') insert into tb(name) values('张四') insert into tb(name) values('张五') insert into tb(name) values('张六') insert into tb(name) values('张七') insert into tb(name) values('张八') insert into tb(name) values('张九') insert into tb(name) values('张十')--显示插入的结果 select * from tb--删除部分记录 delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果 select * from tb--删除环境 drop table tb drop function f_getid/*--测试结果 id name ----------- ---------- 1 张三 2 张四 3 张五 4 张六 5 张七 6 张八 7 张九 8 张十(所影响的行数为 8 行)id name ----------- ---------- 1 张三 2 张四 3 张六 4 张九(所影响的行数为 4 行) --*/
from 表 a
如果表中没有标识字段/主键/唯一键,或数据量大,建议用临时表,或在前台处理中生成行号
create function f_getid()
returns int
as
begin
declare @id int
select @id=max(id) from tb
set @id=isnull(@id,0)+1
return(@id)
end
go--创建表
create table tb(id int default dbo.f_getid() primary key,name varchar(10))
go--创建触发器,在删除表中的记录时,自动更新记录的id(**如果不要此功能,则删除此触发器)
create trigger t_delete on tb
AFTER delete
as
declare @id int,@mid int
select @mid=min(id),@id=@mid-1 from deleted
update tb set id=@id,@id=@id+1 where id>@mid
go--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tb--删除部分记录
delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果
select * from tb--删除环境
drop table tb
drop function f_getid/*--测试结果
id name
----------- ----------
1 张三
2 张四
3 张五
4 张六
5 张七
6 张八
7 张九
8 张十(所影响的行数为 8 行)id name
----------- ----------
1 张三
2 张四
3 张六
4 张九(所影响的行数为 4 行)
--*/
而且,你强调的是无论怎么排序数据,行号不变,"我主要是想得到该记录在表中物理行号",你要显示的数据不一定是按物理顺序,那用行号来翻页,可以吗?再说效率,用触发器,影响的是数据存储的速度,不是数据读出的速度
drop procedure [dbo].[p_splitpage]
GO--利用SQL未公开的存储过程实现分页
create procedure p_splitpage
@sql nvarchar(4000), --要执行的sql语句
@currentpage int=2, --要显示的页码
@pagesize int=10, --每页的大小
@pagecount int=0 out --总页数
as
set nocount on
declare @p1 intexec sp_cursoropen @p1 output,@sql,@scrollopt=1,@ccopt=1,@rowcount=@pagecount outputselect @pagecount=ceiling(1.0*@pagecount/@pagesize)
,@currentpage=(@currentpage-1)*@pagesize+1
select @currentpage
exec sp_cursorfetch @p1,16,@currentpage,@pagesize
exec sp_cursorclose @p1
go--调用示例
exec p_splitpage 'select id,name from sysobjects',2
类似:
select top m * from
(
select top n from table1 order by field1 desc
)
a order by dield1
select 行号=(select count(*) from 表 where 主键<=a.主键),*
from 表 a中
行号就是实际数据的存储位置呀,有时我也这么用!
--利用SQL未公开的存储过程实现分页
create procedure p_splitpage
。。