求一分页存储过程、

著名天才___ 2011-11-25 05:11:43
情况是这样子的、
有表A B
我现在要取B表数据 但是当时设计的时候很变态、B表木有主键 木有ID自动编号

比如我现在取数据 是这样取的
select * from Group_LicenceNo l inner join Group_Drug d on l.GroupID=d.id

Group_LicenceNo 为表B、显示的结果也按B表来 有多少条就数据就显示多少条、 因为B表的Group_LicenceNo.GroupID 是可能重复的、


就是说 有的数据啊
Group_LicenceNo.GroupID Group_LicenceNo.Name
123 aaaa
123 bbbb
这里的Group_LicenceNo.Name是唯一的、

求一分页存储过程、
不知道这样子的设计能不能写出来分页哦
...全文
124 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ynn0705 2011-11-29
  • 打赏
  • 举报
回复
我习惯用分页控件!
angelababa~ 2011-11-28
  • 打赏
  • 举报
回复
http://www.netxk.cn/?p=85
这个不错,分页存储过程,效率还挺高的
heycoder 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sandy945 的回复:]

最后不行你可以创建带主键的临时表,用临时表来分页。
[/Quote]

完全可以考虑这个办法.
qjy5277 2011-11-28
  • 打赏
  • 举报
回复
= =建议别用通用分页存储过程太不好用了。
Sampson890727 2011-11-28
  • 打赏
  • 举报
回复
http://www.cnblogs.com/liguanghui/archive/2011/11/01/2231928.html
著名天才___ 2011-11-28
  • 打赏
  • 举报
回复
自己已经解决、分享一下吧

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



---获取药店集团列表
ALTER PROCEDURE [dbo].[Get_Group_LicenceNo]
(
@GroupName nvarchar(100),
@LicenceNo nvarchar(10),
@topNum int,
@startIndex int,
@endIndex int,
@docount bit
)
AS
declare @TmpSelect NVarchar(1000)
set nocount on
if(@docount=1)
begin
set @TmpSelect = 'select count(*) AS Counts from Group_LicenceNo inner join on Group_Drug where 1 = 1 and Group_LicenceNo.GroupID=Group_Drug.ID '

if @GroupName <> ''
set @TmpSelect = @TmpSelect + ' And Group_Drug.GroupName like ''%'+ @GroupName +'%'''
if @LicenceNo <> ''
set @TmpSelect = @TmpSelect + ' And Group_LicenceNo.LicenceNo = '''+ @LicenceNo +''''


end
else
begin

Set rowcount @endIndex
if @topNum > 0
begin

set @TmpSelect = 'select * from ('
set @TmpSelect = @TmpSelect + ' Select top('+cast(@topNum as nvarchar(10))+') row_number() OVER(ORDER BY ID DESC) AS rownum ,* '
set @TmpSelect = @TmpSelect + ' from Group_LicenceNo,Group_Drug where Group_LicenceNo.GroupID=Group_Drug.ID '

if @GroupName <> ''
set @TmpSelect = @TmpSelect + ' And Group_Drug.GroupName like ''%'+ @GroupName +'%'''
if @LicenceNo <> ''
set @TmpSelect = @TmpSelect + ' And Group_LicenceNo.LicenceNo = '''+ @LicenceNo +''''

set @TmpSelect = @TmpSelect + ') As Group_LicenceNo where rownum between '''+ cast(@startIndex as nvarchar(10)) +''' and '''+ cast(@endIndex as nvarchar(10)) +''' '
End

end

--print @TmpSelect
execute sp_executesql @TmpSelect
set nocount off

dongt1 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sandy945 的回复:]
最后不行你可以创建带主键的临时表,用临时表来分页。
[/Quote]自己写临时表
阿非 2011-11-28
  • 打赏
  • 举报
回复
最后不行你可以创建带主键的临时表,用临时表来分页。
著名天才___ 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 generhappy 的回复:]

如下是存储过程
SQL code

CREATE PROCEDURE Pages
@tblname VARCHAR(255), -- 表名
@strGetFields nvarchar(1000) = "*", -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 2, -- 页尺寸
@PageIndex int = 1,……
[/Quote]

只能查询一张表的、不能查询两张表的数据
约修亚 2011-11-25
  • 打赏
  • 举报
回复
http://www.4guysfromrolla.com/webtech/042606-1.shtml
generhappy 2011-11-25
  • 打赏
  • 举报
回复
如下是存储过程

CREATE PROCEDURE Pages
@tblname VARCHAR(255), -- 表名
@strGetFields nvarchar(1000) = "*", -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 2, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回, 非0 值则返回记录总数
@OrderType bit = 0, -- 设置排序类型, 非0 值则降序
@strWhere varchar(1500) = '' -- 查询条件(注意: 不要加where)
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型

if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 '+ @strWhere
else
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
end --以上代码的意思是如果@doCount传递过来的不是,就执行总数统计。以下的所有代码都是@doCount为的情况:
else
begin
if @OrderType != 0--降序
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' ' + @strGetFields + ' from [' + @tblName + '] where 1=1 ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' ' + @strGetFields + ' from [' + @tblName + '] ' + @strOrder--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields + ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where 1=1 ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and 1=1 ' + @strWhere + ' ' + @strOrder
end
if @strWhere !='' --得到记录的总行数
set @strSQL =@strSQL+ '; select count(*) as Total from [' + @tblName + '] where 1=1 '+ @strWhere
else
set @strSQL =@strSQL+ '; select count(*) as Total from [' + @tblName + ']'
end
exec (@strSQL)
RETURN
GO

约修亚 2011-11-25
  • 打赏
  • 举报
回复
BEGIN
2 DECLARE @PageLowerBound int
3 DECLARE @PageUpperBound int
4
5 -- Set the page bounds
6 SET @PageLowerBound = 10000
7 SET @PageUpperBound = 10020
8
9 -- Create a temp table to store the select results
10 Create Table #PageIndex
11 (
12 [IndexId] int IDENTITY (1, 1) NOT NULL,
13 [Id] varchar(18)
14 )
15
16 -- Insert into the temp table
17 declare @SQL as nvarchar(4000)
18 SET @SQL = 'INSERT INTO #PageIndex (Id)'
19 SET @SQL = @SQL + ' SELECT'
20 SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
21 SET @SQL = @SQL + ' m_id'
22 SET @SQL = @SQL + ' FROM dbo.mem_member'
23 SET @SQL = @SQL + ' ORDER BY NameC'
24
25 -- Populate the temp table
26 exec sp_executesql @SQL
27
28 -- Return paged results
29 SELECT O.*
30 FROM
31 dbo.mem_member O,
32 #PageIndex PageIndex
33 WHERE
34 PageIndex.IndexID > @PageLowerBound
35 AND O.[m_Id] = PageIndex.[Id]
36 ORDER BY
37 PageIndex.IndexID
38
39drop table #PageIndex
40 END
generhappy 2011-11-25
  • 打赏
  • 举报
回复
CREATE PROCEDURE Pages
@tblname VARCHAR(255), -- 表名
@strGetFields nvarchar(1000) = "*", -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 2, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回, 非0 值则返回记录总数
@OrderType bit = 0, -- 设置排序类型, 非0 值则降序
@strWhere varchar(1500) = '' -- 查询条件(注意: 不要加where)
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型

if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 '+ @strWhere
else
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
end --以上代码的意思是如果@doCount传递过来的不是,就执行总数统计。以下的所有代码都是@doCount为的情况:
else
begin
if @OrderType != 0--降序
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' ' + @strGetFields + ' from [' + @tblName + '] where 1=1 ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' ' + @strGetFields + ' from [' + @tblName + '] ' + @strOrder--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields + ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where 1=1 ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and 1=1 ' + @strWhere + ' ' + @strOrder
end
if @strWhere !='' --得到记录的总行数
set @strSQL =@strSQL+ '; select count(*) as Total from [' + @tblName + '] where 1=1 '+ @strWhere
else
set @strSQL =@strSQL+ '; select count(*) as Total from [' + @tblName + ']'
end
exec (@strSQL)
RETURN
GO

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧