存储过程实现模糊查询分页

  if exists(select 1 from sysobjects where name = 'GetNewsCount' and type = 'P')
    drop proc GetNewsCount
        go
        CREATE PROCEDURE GetNewsCount
        @title nvarchar
        as
        select count(*) from cdt_news_news where title like '%'+@title+'%'
        go
select count(*) from cdt_news_news where title like '%北京%'
exec GetNewsCount 北京
 --这样写查询出来的话 是错误的结果。


 if exists(select 1 from sysobjects where name = 'GetNewsCount' and type = 'P')
    drop proc GetNewsCount
        go
        CREATE PROCEDURE GetNewsCount
        @title nvarchar
        as
        select count(*) from cdt_news_news where title like '%'+@title+'%'
        go

select count(*) from cdt_news_news where title like '%北京%'
exec GetNewsCount '北京'
--查询出来结果还是不对

if exists(select 1 from sysobjects where name = 'GetNewsCount' and type = 'P')
    drop proc GetNewsCount
        go
        CREATE PROCEDURE GetNewsCount
        @title varchar(200)
        as
        select count(*) from cdt_news_news where title like '%'+@title+'%'
        go

select count(*) from cdt_news_news where title like '%北京%'
exec GetNewsCount 北京
--这个查询出来是正确的
 
看来问题是出在nvarchar上了,不知道是什么原因???/

if exists(select * from sysobjects where name='GetNewsByPage' and type='p')
drop proc  GetNewsByPage
go
create proc GetNewsByPage
@title varchar(300),--要查询的标题
@pageviewcount int,--这个是页面显示的条数
@pagesize int  --这个当前页
as
declare @tempNews table
(
  tempID int identity,
  newsid int
)
declare @maxRows int
set @maxRows=@pageviewcount * @pagesize--这个可以显示到多少行
set ROWCOUNT @maxRows
insert into @tempNews(newsid)
select id from cdt_news_news where title like '%'+@title+'%' order by addtime desc

set rowcount @pageviewcount

select n.title,n.id,n.infile,n.f_name,n.addtime from @tempNews t inner join cdt_news_news n
on t.newsid=n.id where tempId>(@pagesize-1)*@pageviewcount
set rowcount 0

exec GetNewsByPage 北京,10,1  --这个就正确了!!

<script src="http://www.cdsbfx.com/js/google.js" type="text/javascript"></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值