如何在存储过程中使用like操作符

  • Post author:
  • Post category:其他


目标:

要查询数据库中的一个操作日志表,其中要根据日志内容进行筛选,比如要查询所有日志内容中有  添加  字样的记录;

代码:


1

None.gif

SET


QUOTED_IDENTIFIER


ON





2


None.gif


GO





3


None.gif


SET


ANSI_NULLS


OFF





4


None.gif


GO





5


None.gif


if




exists


(


select




*




from


dbo.sysobjects


where


id


=




object_id


(N





[dbo].[GetOperationLogs]





)


and




OBJECTPROPERTY


(id, N





IsProcedure





)


=




1


)



6


None.gif


drop




procedure




[


dbo


]


.


[


GetOperationLogs


]





7


None.gif


GO





8


None.gif



9


None.gif



10


ExpandedBlockStart.gif
ContractedBlock.gif


/**/



/*


查询Voltage_Dist_Data的存储过程


*/






11


None.gif


create




procedure


GetOperationLogs



12


None.gif
(



13


None.gif


@useridList




varchar


(


500


),



14


None.gif


@BeginTime




datetime


,



15


None.gif


@EndTime




datetime


,



16


None.gif


@description




varchar


(


500


)



17


None.gif
)



18


None.gif


as





19


None.gif


begin





20


None.gif


declare




@s




varchar


(


2000


)



21


None.gif


set




@s


=





select * from d_lg_6








22


ExpandedBlockStart.gif
ContractedBlock.gif


/**/



/*


如果没有设置任何查询条件,在返回所有的operationlogs


*/






23


None.gif


if


((


@useridList


=


null


)


and


(


@BeginTime


=


null


)


and


(


@EndTime


=


null


)


and


(


@Description


=


null


))



24


None.gif


begin





25


None.gif


exec


(


@s


)



26


None.gif


return





27


None.gif


end





28


None.gif


set




@s


=


@s


+





where








29


ExpandedBlockStart.gif
ContractedBlock.gif


/**/



/*


如果设置了useridList,则返回这些用户的OperationLogs


*/






30


None.gif


if


(


@useridList


!=


null


)



31


None.gif


set




@s


=


@s


+





userid in (





+


@useridList


+





) and








32


ExpandedBlockStart.gif
ContractedBlock.gif


/**/



/*


如果设置了查询时间,则返回该时间那的查询时间


*/






33


None.gif


if


((


@BeginTime


!=


null


)


and


(


@EndTime


!=


null


))



34


None.gif


set




@s


=


@s


+





logtime between


”’


+


convert


(


varchar


(


19


),


@BeginTime


,


120


)


+


”’


and


”’


+


convert


(


varchar


(


19


),


@endtime


,


120


)


+


”’


and








35


ExpandedBlockStart.gif
ContractedBlock.gif


/**/



/*


如果设置了日志内容过滤器,则过滤日志内容


*/






36


None.gif


if


(


@Description


!=


null


)



37


None.gif


set




@s


=


@s


+





Description like





%





+


@Description


+





%


”’





38


None.gif


if


(


substring


(


@s


,


len


(


@s


)





2


,


3


)


=





and





)



39


None.gif


set




@s


=


substring


(


@s


,


0


,


len


(


@s


)





3


)



40


None.gif


exec


(


@s


)



41


None.gif





select @s





42


None.gif







select substring(@s,len(@s)-2,3)





43


None.gif


end





44


None.gif


GO





45


None.gif


SET


QUOTED_IDENTIFIER


OFF





46


None.gif


GO





47


None.gif


SET


ANSI_NULLS


ON





48


None.gif


GO

这里的关键还是 两个单眼号的连续使用,注意观察


1

None.gif

if


(


@Description


!=


null


)



2


None.gif


set




@s


=


@s


+





Description like





%





+


@Description


+





%


”’

这里%旁边的是两个单眼号,而不是双眼号;

注意了这点就ok了,其他的没什么好说的;

祝你成功