|
Use this, high efficiency
The first is based on (unique identifier and primary key, composite primary key), if not through a temporary table
/*--Paging program implemented with stored procedures
Page X showing the specified table, view, query results
For the case of the primary key or identity column in the table, directly fetch the query from the original table, otherwise use the temporary table method
If there is a primary key in the view or query results, this method is not recommended
--Zou Jian 2003.09 (please keep this information for reference)--*/
/*--Call example
exec p_show'Regional information'
exec p_show'Area Information', 5, 3,'Area Number, Area Name, Mnemonic Code','Area Number'
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_show]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_show]
GO
CREATE Proc p_show
@QueryStr nvarchar(4000), --Table name, view name, query statement
@PageSize int=10, - the size of each page (number of lines)
@PageCurrent int=1, --page to be displayed
@FdShow nvarchar (4000)=``, ---list of fields to be displayed, if the query result has an identification field, you need to specify this value, and do not include the identification field
@FdOrder nvarchar (1000)='' --sort field list
as
declare @FdName nvarchar(250)-the primary key in the table or the identity column name in the table, temporary table
,@Id1 varchar(20),@Id2 varchar(20)-the start and end record number
, @Obj_ID int - object ID
- Processing of composite primary keys in the table
declare @strfd nvarchar(2000) - list of composite primary keys
, @strjoin nvarchar(4000) - connect fields
,@strwhere nvarchar(2000) --Query condition
select @Obj_ID=object_id(@QueryStr)
,@FdShow=case isnull(@FdShow,'') when'' then '*'else''+@FdShow end
,@FdOrder=case isnull(@FdOrder,'') when'' then'' else 'order by'+@FdOrder end
,@QueryStr=case when @Obj_ID is not null then ''+@QueryStr else' ('+@QueryStr+') a'end
--If the first page is displayed, you can directly use top to complete
if @PageCurrent=1
begin
select @Id1=cast(@PageSize as varchar(20))
exec('select top'+@Id1+@FdShow+' from'+@QueryStr+@FdOrder)
return
end
--If it is a table, check if there is an identifier or primary key in the table
if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=1
begin
select @Id1=cast(@PageSize as varchar(20))
,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20))
select @FdName=name from syscolumns where id=@Obj_ID and status=0x80
if @@rowcount=0 - If there is no identity column in the table, check if there is a primary key in the table
begin
if not exists(select 1 from sysobjects where parent_obj=@Obj_ID and xtype='PK')
goto lbusetemp - If there is no primary key in the table, use the temporary table
select @FdName=name from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
if @@rowcount>1 --Check if the primary key in the table is a composite primary key
begin
select @strfd=``,@strjoin='',@strwhere=''
select @strfd=@strfd+',['+name+']'
,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'
,@strwhere=@strwhere+' and b.['+name+'] is null'
from syscolumns where id=@Obj_ID and colid in(
select colid from sysindexkeys where @Obj_ID=id and indid in(
select indid from sysindexes where @Obj_ID=id and name in(
select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
)))
select @strfd=substring(@strfd,2,2000)
,@strjoin=substring(@strjoin,5,4000)
,@strwhere=substring(@strwhere,5,4000)
goto lbusepk
end
end
end
else
goto lbusetemp
/*--Use identity column or primary key as a single field processing method--*/
lbuseidentity:
exec('select top'+@Id1+@FdShow+' from'+@QueryStr
+'where'+@FdName+' not in(select top '
+@Id2+''+@FdName+' from'+@QueryStr+@FdOrder
+')'+@FdOrder
)
return
/*--The processing method of composite primary key in the table--*/
lbusepk:
exec('select'+@FdShow+' from(select top'+@Id1+' a.* from
(select top 100 percent * from'+@QueryStr+@FdOrder+') a
left join (select top'+@Id2+''+@strfd+'
from'+@QueryStr+@FdOrder+') b on'+@strjoin+'
where'+@strwhere+') a'
)
return
/*--Method of processing with temporary table--*/
lbusetemp:
select @FdName='[ID_'+cast(newid() as varchar(40))+']'
,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20))
,@Id2=cast(@PageSize*@PageCurrent-1 as varchar(20))
exec('select'+@FdName+'=identity(int,0,1),'+@FdShow+'
into #tb from'+@QueryStr+@FdOrder+'
select'+@FdShow+' from #tb where'+@FdName+' between '
+@Id1+' and'+@Id2
)
GO |
|