Create procedure [dbo]. searchbykey
@ Iskeyword varchar (100 ),
@ Iskf varchar (100 ),
@ Isdy varchar (100 ),
@ Iszj varchar (100 ),
@ Lr_begin varchar (100 ),
@ Lr_end varchar (100 ),
@ Iszt varchar (50 ),
@ Islx varchar (100 ),
@ Isly varchar (100 ),
@ Pagesize int, -- page size
@ Pageindex int -- page number
As
Declare
@ Sqlsel varchar (1000 ),
@ Strsql varchar (1000)
Set @ sqlsel = + 'select * from sickinfo where 1 = 1'
If (@ iskeyword! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and askedinfo like ''' +' % '+ @ iskeyword +' % '''
If (@ iskf! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and kefu = ''' + @ iskf + ''''
If (@ isdy! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and admit = ''' + @ isdy + ''''
If (@ iszj! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and zhuanjia = ''' + @ iszj + ''''
If (@ lr_begin! = ''And @ lr_end! = '')
Set @ sqlsel = @ sqlsel + 'and addtime between ''' + convert (char (10), convert (datetime, @ lr_begin), 21) + ''' and ''' + convert (char (10), convert (datetime, @ lr_end), 21) + ''''
If (@ iszt! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and state = ''' + @ iszt + ''''
If (@ islx! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and istype = ''' + @ islx + ''''
If (@ isly! = '')
Set @ sqlsel = isnull (@ sqlsel, '') + 'and infofrom = ''' + @ isly + ''''
-- Sort by order
-- Set @ strsql = 'select top '+ str (@ pagesize) +' * from ('+ @ sqlsel + ') as t1 where id> (select isnull (max (id), 0) from (select top '+ str (@ pageindex-1) * @ pagesize) + 'id from ('+ @ sqlsel +') as t2 order by id) as t) order by id'
-- Reverse arrange
If (@ pageindex = 1)
Begin
Set @ strsql = 'select top '+ str (@ pagesize) +' * from ('+ @ sqlsel +') as tt order by id desc'
-- Set @ strsql = 'select top '+ str (@ pagesize) +' * from # temp2 order by id desc'
End
Else
Begin
Set @ strsql = 'select top '+ str (@ pagesize) +' * from ('+ @ sqlsel +') as t1 where id <(select isnull (min (id ), 0) from (select top '+ str (@ pageindex-1) * @ pagesize) + 'id from (' + @ sqlsel + ') as t2 order by id desc) as t) order by id desc'
End
Exec (@ strsql)
-- Exec @ strsql
Go