我老大让我做个gridview分页的功能。本来表有个ID增长列。我就用
select top页大小* from 表 where(ID>(select max(id) from(select top页大小*页数id FROM表)). 实现了翻页功能,
然后他说如果表没有增长ID列怎么办! 然后我只有换种方法 用 主键 not in的方法
select top 1 * from standard1 where 主键 not in (select top " + (int.Parse(Label3.Text) - 1) + " 主键 from standard1)。
这样也实现了翻页的功能,
然后他说 如果是多表联合查询怎么办! 我又用临时表把要查的数据放在一个临时表里面给临时表加了个ID列然后用第1种方法做。
然后他说不行!也不说为什么不行, 让我自己想! 我是实在想不出来了呀.... 请各位帮帮忙!!
还有就是他说直接用sql语句做不用存储过程,存储过程我也看不懂- -#
希望各位帮帮忙! 把做法跟语句写下来
select top页大小* from 表 where(ID>(select max(id) from(select top页大小*页数id FROM表)). 实现了翻页功能,
然后他说如果表没有增长ID列怎么办! 然后我只有换种方法 用 主键 not in的方法
select top 1 * from standard1 where 主键 not in (select top " + (int.Parse(Label3.Text) - 1) + " 主键 from standard1)。
这样也实现了翻页的功能,
然后他说 如果是多表联合查询怎么办! 我又用临时表把要查的数据放在一个临时表里面给临时表加了个ID列然后用第1种方法做。
然后他说不行!也不说为什么不行, 让我自己想! 我是实在想不出来了呀.... 请各位帮帮忙!!
还有就是他说直接用sql语句做不用存储过程,存储过程我也看不懂- -#
希望各位帮帮忙! 把做法跟语句写下来
select top 50 * from (select top 100 * from table order by datetime desc)A order by A.datetime asc
联合2个表的standard.standardtype, bsc.city_id(
select standard1.standardtype,bsc.city_id from standard1 join bsc on standard1.id=bsc.id )
standardtype city_id
2 758
1 758
3 750
4 750
3 758
1 750
1 750
2 758
4 668
2 758
3 750
... ....
表大概就是这个样子! 怎么分页 上一页,下一页,不能放入临时表加自增ID列 - -#
请教了!!
select top 50 * from (
select top 100 A.* from (standard.standardtype, bsc.city_id(
select standard1.standardtype,bsc.city_id from standard1 join bsc on standard1.id=bsc.id )) A order by city_id asc ) B order by B.city_id desc --分第一页,50条记录
随便写的,不晓得能不能行,试试咯,思路就是这样的了
select top 2 * from (
select top 5 * from (
standard1.standardtype, bsc.city_id( --不行呀 这里出错。好像不能这样写呀
select standard1.standardtype,bsc.city_id from standard1 join bsc on standard1.id=bsc.id ) ) A order by city_id asc ) B order by B.city_id desc 服务器: 消息 170,级别 15,状态 1,行 3
第 3 行: ',' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,行 6
第 6 行: ')' 附近有语法错误。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[bigtable](
[id] [int] IDENTITY(1,1) NOT NULL,
[code] [nchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[name] [nchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_bigtable1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]--数据脚本 declare @maxrowlen int
set @maxrowlen=10000000set nocount on
while(@maxrowlen>0)
begin
insert into bigtable(code,[name])
values(cast(rand()*1000 as varchar(10)),
cast(rand()*1000 as varchar(10)))
set @maxrowlen=@maxrowlen-1
end--产生的mdf文件有1.7g --从此表中选择第31-40条纪录--方法一select top 10 id from
bigtable
where id>(select max(id)
from (select top 30 id from bigtable order by id )b
)
order by id --方法二,只sqlserver2005以上支持--从一千万行的表中,找出第四百万-四百万零10条纪录--只要2s.在我的笔记本上with rows
as
(
select row_number() over(order by id) as rownum
from bigtable)
select * from rows where rownum between 4000000 and 4000010 --速度比较declare @start datetime
declare @end datetime
declare @message varchar(50)
set @start=getdate()
;
with rows
as
(
select row_number() over(order by id) as rownum
from bigtable)
select * from rows where rownum between 4000000 and 4000010
;set @end=getdate()select datediff(millisecond,@start,@end)set @start=getdate()
select top 10 id from
bigtable
where id>(select max(id)
from (select top 4000000 id from bigtable order by id )b
)
order by id
set @end=getdate()
select datediff(millisecond,@start,@end) --row_numer() 花2016 ms--top max 方法 花 2160 ms--row_number()胜出
还是谢谢楼上的!
bigtable
where id>(select max(id)
from (select top 30 id from bigtable order by id )b
)
order by id
select standard1.standardtype,bsc.city_id from standard1 join bsc on standard1.id=bsc.id )
___________这句是我复制你的代码,你看你原先的联合查询咋写的阿,替换成你的查询代码撒,LZ笨死了
row_number()
这个方法好像是 sqlserver2005才有的吧?
我的数据库是sqlserver2000的。
回复楼上的 我也想不甩他,没得办法呀!他现在要求就是对
standardtype city_id
2 758
1 758
3 750
4 750
3 758
1 750
1 750
2 758
4 668
2 758
3 750
... ....
这样的两个表关联的查询分页。还不能加一个自增ID列。还有什么方法么?
这就是我的联合查询呀。 上面的数据就是查询后的。 我的两个表舅是 standard表和bsc表。 你说的那个我没看懂!
好像不能把查询结果的表定义别名把?
MaximumRowsParameterName
StartRowIndexParameterName
SelectCountMethod
@FromTable nvarchar(2000),
@Fields nvarchar(300),
@WhereClause nvarchar(3600),
@OrderBy nvarchar(100),
@PageIndex int,
@PageSize int,
@PageTotal int output
AS
BEGINSET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTEDDECLARE @PageLowerBound int
DECLARE @PageUpperBound intSET @PageLowerBound = @PageSize * (@PageIndex-1)
SET @PageUpperBound = @PageLowerBound + @PageSize + 1declare @SQL as nvarchar(4000)IF (LEN(@WhereClause) <= 0)
SET @WhereClause = '1=1'IF (LEN(@OrderBy) <= 0)
BEGIN
RAISERROR (N'<必须提供Order By参数>', 11, 1)
RETURN 99
END set @PageTotal=0
select @sql='select @PageTotal=count(1) from ' + @FromTable + ' WHERE ' + @WhereClause
exec sp_executesql @sql, N'@PageTotal int output',@PageTotal output
SET @SQL = 'WITH t AS ('
+ ' SELECT ' + @Fields + ', ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') AS RowNumber'
+ ' FROM ' + @FromTable
+ ' WHERE ' + @WhereClause
+ ')' + CHAR(13)SET @SQL = @SQL + 'SELECT * FROM t WHERE RowNumber > ' + CAST(@PageLowerBound AS nvarchar) + ' AND RowNumber < ' + CAST (@PageUpperBound AS nvarchar)exec sp_executesql @SQLSET NOCOUNT OFF
END
我说了我的是SQLserver2000的,2000没有ROW_NUMBER()方法。
不过还是谢谢你!