最大的網(wǎng)站源碼資源下載站,
存儲過程 為 sql server 2000版本,請打開sql server 2000 的查詢分析器執(zhí)行下面的sql 語句。
程序用到的存儲過程(僅支持主鍵排序)
 if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sys_quicksortpaging]') and objectproperty(id, n'isprocedure') = 1)
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sys_quicksortpaging]') and objectproperty(id, n'isprocedure') = 1) drop procedure [dbo].[sys_quicksortpaging]
drop procedure [dbo].[sys_quicksortpaging] go
go
 set quoted_identifier off
set quoted_identifier off  go
go set ansi_nulls on
set ansi_nulls on  go
go
 create procedure  sys_quicksortpaging
create procedure  sys_quicksortpaging (
(  @table nvarchar(4000),            --表名(必須)
    @table nvarchar(4000),            --表名(必須) @primarykeyfield nvarchar(50),        --表的主鍵字段
    @primarykeyfield nvarchar(50),        --表的主鍵字段 @field nvarchar (4000)='*',         --需要返回字段名(必須)
    @field nvarchar (4000)='*',         --需要返回字段名(必須)         @where nvarchar(1000)=null,        --where 條件(可選)
    @where nvarchar(1000)=null,        --where 條件(可選) @groupby nvarchar(1000) = null,     --分組
             @groupby nvarchar(1000) = null,     --分組 @orderby nvarchar(1000)=null,     --排序用到的字段()
    @orderby nvarchar(1000)=null,     --排序用到的字段() @pagenumber int = 1,             --要返回的頁(第x頁) (默認為第一頁)
    @pagenumber int = 1,             --要返回的頁(第x頁) (默認為第一頁) @pagesize int = 10,            --每頁大小(默認為5)
    @pagesize int = 10,            --每頁大小(默認為5) @recordcount int output        --返回記錄總數(shù)
    @recordcount int output        --返回記錄總數(shù) )
) as
as  set nocount on
    set nocount on declare @sorttable nvarchar(100)
    declare @sorttable nvarchar(100) declare @sortname nvarchar(100)
    declare @sortname nvarchar(100) declare @strsortcolumn nvarchar(200)
    declare @strsortcolumn nvarchar(200) declare @operator nvarchar(50)
    declare @operator nvarchar(50) declare @type varchar(100)
    declare @type varchar(100) declare @prec int
    declare @prec int
 if @orderby is null or @orderby = ''
    if @orderby is null or @orderby = '' set @orderby =  @primarykeyfield
    set @orderby =  @primarykeyfield     
    
 /**//* 獲取用于定位的字段*/
    /**//* 獲取用于定位的字段*/                 if charindex('desc',@orderby)>0
    if charindex('desc',@orderby)>0                         begin
        begin set @strsortcolumn = replace(@orderby, 'desc', '')
            set @strsortcolumn = replace(@orderby, 'desc', '') set @operator = '<='
            set @operator = '<=' end
        end else
    else begin
        begin if charindex('asc', @orderby) = 0
            if charindex('asc', @orderby) = 0 set @strsortcolumn = replace(@orderby, 'asc', '')
            set @strsortcolumn = replace(@orderby, 'asc', '') set @operator = '>='
            set @operator = '>=' end
        end 
     if charindex('.', @strsortcolumn) > 0
    if charindex('.', @strsortcolumn) > 0                         begin
        begin set @sorttable = substring(@strsortcolumn, 0, charindex('.',@strsortcolumn))
            set @sorttable = substring(@strsortcolumn, 0, charindex('.',@strsortcolumn)) set @sortname = substring(@strsortcolumn, charindex('.',@strsortcolumn) + 1, len(@strsortcolumn))
            set @sortname = substring(@strsortcolumn, charindex('.',@strsortcolumn) + 1, len(@strsortcolumn)) end
        end else
    else begin
        begin set @sorttable = @table
            set @sorttable = @table set @sortname = @strsortcolumn
            set @sortname = @strsortcolumn end
        end
 select @type=t.name, @prec=c.prec
    select @type=t.name, @prec=c.prec from sysobjects o
    from sysobjects o  join syscolumns c on o.id=c.id
    join syscolumns c on o.id=c.id join systypes t on c.xusertype=t.xusertype
    join systypes t on c.xusertype=t.xusertype where o.name = @sorttable and c.name = @sortname
    where o.name = @sorttable and c.name = @sortname 
     if charindex('char', @type) > 0
    if charindex('char', @type) > 0 set @type = @type + '(' + cast(@prec as nvarchar) + ')'
    set @type = @type + '(' + cast(@prec as nvarchar) + ')'
 declare @strstartrow nvarchar(50)
    declare @strstartrow nvarchar(50) declare @strpagesize nvarchar(50)
    declare @strpagesize nvarchar(50) declare @strwhere nvarchar(1000)
    declare @strwhere nvarchar(1000) declare @strwhereand nvarchar(1000)
    declare @strwhereand nvarchar(1000) declare @strgroupby nvarchar(1000)
    declare @strgroupby nvarchar(1000)
 if  @pagenumber < 1
    if  @pagenumber < 1 set @pagenumber = 1
    set @pagenumber = 1 
     set @strpagesize = convert (nvarchar(50), @pagesize)
    set @strpagesize = convert (nvarchar(50), @pagesize) set @strstartrow = convert ( nvarchar(50), (@pagenumber - 1)*@pagesize + 1)
    set @strstartrow = convert ( nvarchar(50), (@pagenumber - 1)*@pagesize + 1) 
     if @where is not null and @where !=''
    if @where is not null and @where !='' begin
    begin set @strwhere = ' where '+ @where
        set @strwhere = ' where '+ @where  set @strwhereand= ' and ' + @where
        set @strwhereand= ' and ' + @where  end
    end else
    else begin
    begin set @strwhere =  ''
        set @strwhere =  '' set @strwhereand=''
        set @strwhereand='' end
    end 
     if @groupby is not null and @groupby != ''
    if @groupby is not null and @groupby != '' begin
    begin set @strgroupby = ' group by ' + @groupby
        set @strgroupby = ' group by ' + @groupby end
    end else
    else begin
    begin set @strgroupby = ''
        set @strgroupby = '' end
    end
 declare @strsql nvarchar(4000)
    declare @strsql nvarchar(4000) set @strsql= ' select  @recordcount = count (*)  from ' + @table + @strwhere + ' '  + @strgroupby
    set @strsql= ' select  @recordcount = count (*)  from ' + @table + @strwhere + ' '  + @strgroupby exec   sp_executesql   @strsql,n'@recordcount   int   output',@recordcount   output--計算總頁數(shù)
    exec   sp_executesql   @strsql,n'@recordcount   int   output',@recordcount   output--計算總頁數(shù)   
 exec
    exec (
        (  '
        ' declare @sort ' + @type + '
        declare @sort ' + @type + ' set rowcount ' + @strstartrow + '
        set rowcount ' + @strstartrow + ' select  @sort = ' + @strsortcolumn + '  from ' + @table + @strwhere + ' '  + @strgroupby + ' order by ' + @orderby + '
        select  @sort = ' + @strsortcolumn + '  from ' + @table + @strwhere + ' '  + @strgroupby + ' order by ' + @orderby + '  set rowcount ' + @strpagesize + '
        set rowcount ' + @strpagesize + '  select '+@field+'  from ' + @table + ' where ' +
        select '+@field+'  from ' + @table + ' where ' + )
        ) go
go set quoted_identifier off
set quoted_identifier off  go
go set ansi_nulls on
set ansi_nulls on  go
go
新聞熱點
疑難解答
圖片精選