查询分页显示,前台有若干个条件,查询条件是可选可不选的。(是多个表联合查询)
怎么写这个存储过程啊?

解决方案 »

  1.   

    -- 处理动态条件的示例create procedure test
     @fd1 varchar(100),@value1 varchar(100)
    ,@fd2 varchar(100),@value2 varchar(100)
    ,@fd3 varchar(100),@value3 varchar(100)
    ,@fd4 varchar(100),@value4 varchar(100)
    ,@tj varchar(1000) output
    as
    select @tj='',@tj=@tj
        +case isnull(@value1,'') when '' then ''
              else ' and ['+@fd1+']='''+@value1+'''' end
        +case isnull(@value2,'') when '' then ''
              else ' and ['+@fd2+']='''+@value2+'''' end
        +case isnull(@value3,'') when '' then ''
              else ' and ['+@fd3+']='''+@value3+'''' end
        +case isnull(@value4,'') when '' then '' 
              else ' and ['+@fd4+']='''+@value4+'''' end
    if @tj<>'' 
       set @tj=' where '+right(@tj,len(@tj)-5)
    go--聆彸
    declare @tj varchar(1000)
    exec test 'col1','aa'
       ,'col2',''
     ,'col3',cc'
       ,'col4','bb'
     ,@tj output
    print @tjgo
    drop procedure test
      

  2.   

    -- 通用的分页if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_PageView]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[sp_PageView]
    GO/*--利用SQL未公开的存储过程实现分页

    方法简单且效率高,已知的问题就是要多返回一个空的记录集 解决的方法是在前台调用时,用 set recordset=recordset.nextrecordset
    的方法跳过第一个记录集

    此方法由J9988提供,我只是将它改成了方便调用的存储过程--邹建 2004.05(引用请保留此信息)--*//*--调用示例

    declare @PageCount int
    exec sp_PageView 
    @sql='select * from sysobjects',
    @PageCurrent=2,
    @PageCount=@PageCount out
    SELECT @PageCount
    --*/
    CREATE PROC sp_PageView   
    @sql         ntext,     --要执行的sql语句
    @PageCurrent int=1,     --要显示的页码
    @PageSize    int=10,    --每页的大小
    @PageCount   int OUTPUT --总页数
    AS
    SET NOCOUNT ON
    DECLARE @p1 int
    --初始化分页游标
    EXEC sp_cursoropen 
    @cursor=@p1 OUTPUT,
    @stmt=@sql,
    @scrollopt=1,
    @ccopt=1,
    @rowcount=@PageCount OUTPUT--计算总页数
    IF ISNULL(@PageSize,0)<1 
    SET @PageSize=10
    SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
    IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
    SET @PageCurrent=1
    ELSE
    SET @PageCurrent=(@PageCurrent-1)*@PageSize+1--显示指定页的数据
    EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize--关闭分页游标
    EXEC sp_cursorclose @p1
    GO
      

  3.   

    /*
    模块名称: ProcGetPage
    程序开发:
    开发时间:
    功能说明: 实现分页
    参数说明:
    返回说明: @SQLSTR 查询字符串
    @PAGECOUNT 第N页
    @PAGESIZE 每页行数
    */
    CREATE PROCEDURE ProcGetPage 
    @SQLSTR NVARCHAR(4000)
    ,@PAGECOUNT INT=1
    ,@PAGESIZE INT=99999999
    AS
    SET NOCOUNT ON
    DECLARE @P1 INT
    SET @PAGECOUNT=(@PAGECOUNT-1)*@PAGESIZE+1
    EXEC sp_cursoropen @P1 OUTPUT,@SQLSTR
    EXEC sp_cursorfetch  @P1,16,@PAGECOUNT,@PAGESIZE
    EXEC sp_cursorclose @P1GO
      

  4.   

    djh=trim(request("djh"))
    wpmc=trim(request("wpmc"))sql="select * djb,wpjbx where djb.djh=wpjbx.djb " if djh<>"" then
      sql =sql & " and djb.djh='"&djh&"'" 
     end if  if wpmc<>"" then
      sql =sql & " and djb.wpmc='"&wpmc&"'" 
     end if就是把上面的这个写成分页的存储过程
      

  5.   

    zjcxc(邹建) -- 通用的分页
    在asp中怎么用这个存储过程啊,怎么显示"首页""后页"这些东西啊。
      

  6.   

    回复人:liangjianshi(两件事) ( 一级(初级)) 信誉:100  2006-9-18 17:10:23  zjcxc(邹建) -- 通用的分页
    在asp中怎么用这个存储过程啊,怎么显示"首页""后页"这些东西啊。
    --------------------------
    这个问题和通用存储过程是 两件事 ^_^