可以写出用多个条件实现模糊查询并且能分页的存储过程!!急!!!

CREATEprocUP_Tm_LeaveChildren_GetModels(@Ccorpvarchar(30),--幼儿园@Classnamevarchar(20),... CREATE proc UP_Tm_LeaveChildren_GetModels
(
@Ccorp varchar(30), --幼儿园
@Classname varchar(20), --班级名称
@Cname varchar(40), --幼儿姓名
@LeaveSDate varchar(30), --起始时间
@LeaveEDate varchar(30), --终止时间
@LeaveWhy varchar(20), --离园类型
@PageIndex int, --页码
@PageSize int --每页显示的数据条数
)
as
begin
declare @SqlText varchar(1024)
set @SqlText='select top ' +cast(@PageSize as varchar(12))+'Ccorp,Classname,Childid,Cname,Csex,convert(char(10),Cbirth,120) as Cbirth,
convert(char(10),InDate,120) as InDate,Address,Hphone,convert(char(10),LeaveDate,120) as LeaveDate,LeaveWhy
from (select * from Tm_LeaveChildren
where Ccorp ='+''''+@Ccorp+''''+'and Classname like '+''''+'%'+@Classname+'%'+''''+ 'and Cname like' +''''+ '%'+@Cname+'%' +''''+ 'and LeaveDate <= '+''''+convert(char(10),@LeaveSDate,120)+''''+'and LeaveDate <= '+''''+
convert(char(10),@LeaveEDate,120)+''''+'and LeaveWhy like'+''''+ '%'+@LeaveWhy+'%' +''''+')
Tm_LeaveChildren
where Childid
not in
(
select top '+cast((@PageIndex-1)*@PageSize as varchar(32)) +'Childid from (select * from Tm_LeaveChildren
where Ccorp ='+''''+@Ccorp+''''+'and Classname like ' +''''+ '%'+ @Classname+'%' +''''+ 'and Cname like'+''''+'%'+ @Cname +'%'+''''+ 'and LeaveDate <= '+''''+convert(char(10),@LeaveSDate,120)+''''+'
and LeaveDate <= '+''''+convert(char(10),@LeaveEDate,120)+''''+'and LeaveWhy like'+''''+ '%'+@LeaveWhy+'%'+'''' +')
Tm_LeaveChildren order by Childid desc
)
order by Childid desc'

exec(@SqlText)
end
GO

我写的存储过程不能实现模糊查询!该怎么修改啊!!
展开
 我来答
gloomyd
2008-06-13 · TA获得超过186个赞
知道小有建树答主
回答量:209
采纳率:0%
帮助的人:205万
展开全部
你可以先通过模糊查询把查询的结果放入临时表中
然后再对临时表进行操作
这样的话 结构很清晰 不容易出错
select * into #temp from Tm_LeaveChildren
where Ccorp ='+''''+@Ccorp+''''+'and Classname like '+''''+'%'+@Classname+'%'+''''+ 'and Cname like' +''''+ '%'+@Cname+'%' +''''+ 'and LeaveDate <= '+''''+convert(char(10),@LeaveSDate,120)+''''+'and LeaveDate <= '+''''+
convert(char(10),@LeaveEDate,120)+''''+'and LeaveWhy like'+''''+ '%'+@LeaveWhy+'%' +''''+')

select top @PageSize * from #temp where Childid
not in
( select top (@PageIndex-1)*@PageSize from #temp order by Childid desc
)
order by Childid desc
我没有验证上面的sql是否正确 你自己看看吧
xinxiu198471
2008-06-25
知道答主
回答量:16
采纳率:0%
帮助的人:2.6万
展开全部
/* 创建者雨 只需要把SQL语句,页码,每页显示的数量执行就可以*/
CREATE PROCEDURE [dbo].[les_AllowPaging]
@pageindex int,
@PageSize int,
@tsql varchar(4000)
as

Declare @AllowPagingSql varchar(4000)

set @AllowPagingSql=
'select * from
(
SELECT ROW_NUMBER() OVER(ORDER BY orderbyID DESC) AS AllowPagingId, * FROM ( select *, 1 as orderbyID from ( '+@tsql +' ) as tbs1 )
as Tabl1
) as table2
where AllowPagingId between '
+convert(varchar(10),((@pageindex-1) * @PageSize+1))+' and '
+convert(varchar(10), @pageindex * @PageSize)
set @AllowPagingSql= replace( @AllowPagingSql,'$','''')
exec (@AllowPagingSql)

set @AllowPagingSql='select
case
when count(*)%'+convert(varchar(10),@PageSize)+'=0 then count(*)/'+convert(varchar(10),@PageSize)+'
when count(*)%'+convert(varchar(10),@PageSize)+'<>0 then count(*)/'+convert(varchar(10),@PageSize)+'+1
end as pageCount,count(*) as RowsCount from ('+replace(@tsql,'$','''')+') as tab1'
exec (@AllowPagingSql)
/*
exec [les_AllowPaging] 15,100,' select * from sys.sysobjects '
exec [les_AllowPaging]%2

参考资料: SQL 存储过程分页

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式