多條件儲存過程 (條件拼接SQL語句)

2021-10-23 10:37:39 字數 1810 閱讀 2011

alter proc sp_pagedbyconditions

( @countrycode

nvarchar(20

),--國家編號

@cid

int,

--城市的id

@pageindex

int=1,

--當前要顯示的頁碼

@pagesize

int=3,

--每頁要顯示的資料條數(頁大小)

@totalpages

intout

--總頁數,乙個輸出引數)as

begin

--定義的變數,一定要給初始值

declare @sqlwhere nvarchar

(max)=''

,@sqlpaged nvarchar

(max)=''

if(@countrycode!=

'')

begin

set @sqlwhere+

=' and countrycode='

''+@countrycode+

''''

--'and countrycode='

''+chn+

''''

endif(@cid<

>0)

begin

set @sqlwhere+

=' and id='

+cast

(@cid

asnvarchar(20

))end

set @sqlpaged =

'select top('

+cast

(@pagesize

asnvarchar(20

))+')*

from

(select

row_number()

over

(order by id)

as 行號,

*from city where1=

1'+@sqlwhere+'

) t where 行號》'+

cast((

(@pageindex-1)

*@pagesize)

asnvarchar(20

))exec sp_executesql @sqlpaged

--*****=

*****=

====取得要分頁的總資料量,計算總頁數===

*****=

*****=

*****=

*****=

*****=

*****=

*****=

*****=

*****=

*****=

= declare @sqlcount int=0

,@sqlstr nvarchar

(max)=''

set @sqlstr=

'select @c= count(*) from city where 1=1'

+@sqlwhere

exec sp_executesql @sqlstr,n'@c int out'

,@sqlcount out

set @totalpages=

ceiling

(@sqlcount*

1.0/@pagesize)

endgo

declare @c int

exec sp_pagedbyconditions 'chn',0

,1,5

,@c out

select @c

phpsql拼接 PHP拼接多條件SQL語句

判斷是否新增where語句 param sql param has where return string sql has where and where return sql 檢索資料 param params return activedataprovider public function s...

SQL多條件查詢拼接in中條件方法

前段時間做了乙個功能,就是前台可以選擇同一字段下面的多個不同的條件,這就涉及到了sql語句拼接的問題。下面是乙個用正則來處理查詢條件的方法,是我目前所見到最簡單的方法,現將它分享給大家。using system.text using system.text.regularexpressions st...

多條件檢索SQL語句的拼接

需求分析 在使用多條件查詢的時候,比如這樣的乙個圖書查詢頁面 如果使用sql語句 select from book where bookname name and author author and address address 但是,在不知道哪一欄會被輸入進去,where和and 的使用,有點不...