儲存過程實現模糊查詢分頁

2021-04-19 06:53:29 字數 2007 閱讀 5391

if exists(select 1 from sysobjects where name = 'getnewscount' and type = 'p')

drop proc getnewscount

gocreate procedure getnewscount

@title nvarchar

asselect count(*) from cdt_news_news where title like '%'+@title+'%'

goselect 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

gocreate procedure getnewscount

@title nvarchar

asselect count(*) from cdt_news_news where title like '%'+@title+'%'

goselect 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

gocreate procedure getnewscount

@title varchar(200)

asselect count(*) from cdt_news_news where title like '%'+@title+'%'

goselect 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

gocreate proc getnewsbypage

@title varchar(300),--要查詢的標題

@pageviewcount int,--這個是頁面顯示的條數

@pagesize int  --這個當前頁

asdeclare @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  --這個就正確了!!

儲存過程實現分頁查詢

以學生資訊表為例 一 建立分頁儲存過程 create proc proc findstudentsplitpage 宣告引數 pagesize int,每頁記錄數 輸入引數 curpage int,當前是第幾頁 輸入引數 totalpage int out,總頁數 輸出引數 totalcount i...

儲存過程實現模糊查詢問題

環境sql server 2008 查詢器 create proc lyf a hto nvarchar 50 null asselect ordponumer from orderline like n hto go執行 exec lyf a 洛陽 查詢字段結果很多?的資訊 我用查詢語句測試 se...

模糊查詢(儲存過程)

set ansi nulls on set quoted identifier on goalter procedure dbo searchvague description varchar 1000 descriptionlength int,pagenumber tinyint,product...