sql存储过程定义多条件查询加分页 10

CREATEPROCEDUREZy_Users_GetList@pagesizeint,@pageindexintasbeginwithtemptblas(SELECTR... CREATE PROCEDURE Zy_Users_GetList
@pagesize int,
@pageindex int
as

begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY RegDateTime desc)AS Row, * from Users O )
SELECT * FROM temptbl where Row between (@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize
end
如果我想给上面的sql动态的加个where呢。比如像下面的存储过程那样定义个@where。怎么写?
ALTER PROCEDURE [dbo].[GetTableCount]
@tablename nvarchar(100),
@where nvarchar(50)
AS
declare @fstr char(4000)
if(@where='')
begin
set @fstr='select count(*) from ' + @tablename
end
else
begin
set @fstr='select count(*) from ' + @tablename + ' where ' + @where
end
exec (@fstr)
展开
 我来答
百度网友1214fdcbb
2010-12-06 · TA获得超过1240个赞
知道小有建树答主
回答量:316
采纳率:0%
帮助的人:374万
展开全部
给你一个强大至极的分页存储过程(输入对应的参加即可),以此共勉,希望你也能进步:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Pagination]
@Page int = 1, -- 当前页码
@PageSize int = 10, -- 每页记录条数(页面大小)
@Table nvarchar(500), -- 表名或视图名,甚至可以是嵌套SQL:(Select * From Tab Where ID>1000) Tab
@Field nvarchar(800) = '*', -- 返回记录集字段名,","隔开,默认是"*"
@OrderBy nvarchar(100) = 'ID ASC', -- 排序规则
@Filter nvarchar(500), -- 过滤条件
@MaxPage smallint output, -- 执行结果 -1 error, 0 false, maxpage true
@TotalRow int output, -- 记录总数 /* 2007-07-12 22:11:00 update */
@Descript varchar(100) output -- 结果描述
AS
BEGIN

-- =============================================
-- Author: Jimmy.Yu
-- Create date: 2007-5-11
-- Description: SQL 2005 以上版本 通用分页存储过程
-- =============================================

Set ROWCOUNT @PageSize;

Set @Descript = 'successful';
-------------------参数检测----------------
IF LEN(RTRIM(LTRIM(@Table))) !> 0
Begin
Set @MaxPage = 0;
Set @Descript = 'table name is empty';
Return;
End

IF LEN(RTRIM(LTRIM(@OrderBy))) !> 0
Begin
Set @MaxPage = 0;
Set @Descript = 'order is empty';
Return;
End

IF ISNULL(@PageSize,0) <= 0
Begin
Set @MaxPage = 0;
Set @Descript = 'page size error';
Return;
End

IF ISNULL(@Page,0) <= 0
Begin
Set @MaxPage = 0;
Set @Descript = 'page error';
Return;
End
-------------------检测结束----------------

Begin Try
-- 整合SQL
Declare @SQL nvarchar(4000), @Portion nvarchar(4000);

Set @Portion = ' ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') AS ROWNUM FROM ' + @Table;

Set @Portion = @Portion + (CASE WHEN LEN(@Filter) >= 1 THEN (' Where ' + @Filter + ') AS tab') ELSE (') AS tab') END);

Set @SQL = 'Select TOP(' + CAST(@PageSize AS nvarchar(8)) + ') ' + @Field + ' FROM (Select ' + @Field + ',' + @Portion;

Set @SQL = @SQL + ' Where tab.ROWNUM > ' + CAST((@Page-1)*@PageSize AS nvarchar(8));

-- 执行SQL, 取当前页记录集
Execute(@SQL);
--------------------------------------------------------------------

-- 整合SQL
Set @SQL = 'Set @Rows = (Select MAX(ROWNUM) FROM (Select' + @Portion + ')';

-- 执行SQL, 取最大页码
Execute sp_executesql @SQL, N'@Rows int output', @TotalRow output;
Set @MaxPage = (CASE WHEN (@TotalRow % @PageSize)<>0 THEN (@TotalRow / @PageSize + 1) ELSE (@TotalRow / @PageSize) END);
End Try
Begin Catch
-- 捕捉错误
Set @MaxPage = -1;
Set @Descript = 'error line: ' + CAST(ERROR_LINE() AS varchar(8)) + ', error number: ' + CAST(ERROR_NUMBER() AS varchar(8)) + ', error message: ' + ERROR_MESSAGE();
Return;
End Catch;

-- 执行成功
Return;
END
牧之卉0j1
2010-12-06 · TA获得超过1150个赞
知道大有可为答主
回答量:1689
采纳率:0%
帮助的人:1723万
展开全部
declare @nsql nvarchar(8000)
declare @where nvarchar(1000)
set @where = N'and age<30'----------
set @nsql=N'
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY RegDateTime desc)AS Row, * from Users O where 1=1 '+@where+' )
SELECT * FROM temptbl where Row between (@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize'
exec sp_executesql @nsql,N'@pageindex int,@pagesize int',@pageindex,@pagesize
大致就是这样
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式