By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .
access_time 12 years ago visibility355 comment 0 language中文

个人感觉这个存储过程写得比较的好:

转载自:http://www.cnblogs.com/eme/archive/2006/12/19/597275.html

/*
过程说明:对表按任意字段进行排序分页
创建时间:2005年7月24日
作者: chen gang
*/
CREATE PROCEDURE dbo.GetPagingRecord
    (
        @tablename varchar(100),--表名或视图表
        @fieldlist varchar(4000)='*',--欲选择字段列表
        @orderfield varchar(100),--排序字段
        @keyfield varchar(100),--主键
        @pageindex int,--页号,从1开始
        @pagesize int=20,--页尺寸
        @strwhere varchar(4000),--条件
        @ordertype varchar(1)='1'--排序,1,降序,0,升序
    )
AS

    SET NOCOUNT ON
    declare @sqlstr varchar(6000)
    declare @orderstr varchar(100)
    if(@keyfield=@orderfield)
    begin
set @orderstr = ''
    end
    else
    begin
set @orderstr = ','+@keyfield+' asc'
    end
    if @pageindex<=0
set @pageindex=1
    --处理SQL中危险字符,并且将条件处理成易嵌入的形式
    set @strwhere=replace(@strwhere,'''','''''')
    set @strwhere=replace(@strwhere,'--','')
    set @strwhere=replace(@strwhere,';','')
    set @sqlstr='declare @CurPageNum int;'
    set @sqlstr=@sqlstr+'declare @nextpagenum int;'
    set @sqlstr=@sqlstr+'set @curpagenum='+cast(@PageIndex-1 as varchar)+'*'+cast(@Pagesize as varchar)+';'
    set @sqlstr=@sqlstr+'set @nextpagenum='+cast(@PageIndex as varchar)+'*'+cast(@Pagesize as varchar)+';'
    set @sqlstr=@sqlstr+'declare @sqlstr varchar(6000);'
    if @ordertype='1'
    begin
    set @sqlstr=@sqlstr+'set @sqlstr=''select '+@fieldlist+' from ( select top ''+cast(@nextpagenum as varchar)+'' * from

'+@tablename+' where 1=1 '+@strwhere+' order by '+@orderfield+' desc' + @orderstr+') as a where 1=1 and '+@keyfield+' not in (

select top ''+cast(@curpagenum as varchar)+'' '+@keyfield+' from '+@tablename+' where 1=1 '+@strwhere+'

order by '+@orderfield +' desc'+ @orderstr +') order by ' + @orderfield +' desc'';'
    end
    else
    begin
    set @sqlstr=@sqlstr+'set @sqlstr=''select '+@fieldlist+' from ( select top ''+cast(@nextpagenum as varchar)+'' * from

'+@tablename+' where 1=1 '+@strwhere+' order by '+@orderfield+' asc' + @orderstr+' ) as a where 1=1 and '+@keyfield+' not in (

select top ''+cast(@curpagenum as varchar)+'' '+@keyfield+' from '+@tablename+' where 1=1 '+@strwhere+'

order by '+@orderfield +' asc' + @orderstr+') order by '+@orderfield +' asc'';'
    end
    set @sqlstr=@sqlstr+'execute( @sqlstr)'
    print @sqlstr
execute(@sqlstr)
GO

info Last modified by Raymond at 6 years ago * This page is subject to Site terms.

More from Kontext

local_offer pyspark local_offer spark-2-x local_offer teradata local_offer SQL Server

visibility 59
thumb_up 0
access_time 18 days ago

In my previous article about  Connect to SQL Server in Spark (PySpark) , I mentioned the ways t...

open_in_new View open_in_new Spark + PySpark

local_offer python local_offer SQL Server

visibility 171
thumb_up 0
access_time 2 months ago

Python JayDeBeApi module allows you to connect from Python to databases using Java JDBC drivers.

open_in_new View open_in_new Code snippets

Spark Read from SQL Server Source using Windows/Kerberos Authentication

local_offer pyspark local_offer SQL Server local_offer spark-2-x

visibility 141
thumb_up 0
access_time 3 months ago

In this article, I am going to show you how to use JDBC Kerberos authentication to connect to SQL Server sources in Spark (PySpark). I will use  Kerberos connection with principal names and password directly that requires  ...

open_in_new View open_in_new Spark + PySpark

local_offer SQL Server local_offer hive

visibility 1359
thumb_up 0
access_time 12 months ago

In one of my previous post, I showed how to configure Apache Hive 3.0.0 in Windows 10. Apache Hive 3.0.0 Installation on Windows 10 Step by Step Guide ...

open_in_new View open_in_new Hadoop

info About author

Kontext dark theme mode

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward
Kontext Column

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.

Learn more arrow_forward
info Follow us on Twitter to get the latest article updates. Follow us