存储过程中使用Like
文章来自:CSDN-xiayuzhongdexiaotan的专栏
SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS OFF GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetOperationLogs]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[GetOperationLogs] GO /* 查询Voltage_Dist_Data的存储过程 */ create procedure GetOperationLogs ( @useridList varchar(500), @BeginTime datetime, @EndTime datetime, @description varchar(500) ) as begin declare @s varchar(2000) set @s='select * from d_lg_6' /* 如果没有设置任何查询条件,在返回所有的operationlogs */ if ((@useridList=null)and(@BeginTime=null)and(@EndTime=null)and(@Description=null)) begin exec(@s) return end set @s=@s+' where ' /*如果设置了useridList,则返回这些用户的OperationLogs */ if (@useridList!=null) set @s=@s+' userid in ('+@useridList+') and ' /*如果设置了查询时间,则返回该时间那的查询时间*/ if ((@BeginTime!=null)and(@EndTime!=null)) set @s=@s+' logtime between '''+convert(varchar(19),@BeginTime,120)+''' and '''+convert(varchar(19),@endtime,120)+''' and ' /*如果设置了日志内容过滤器,则过滤日志内容 */ if (@Description!=null) set @s=@s+' Description like ''%'+@Description+'%''' if (substring(@s,len(@s)-2,3)='and') set @s=substring(@s,0,len(@s)-3) exec(@s) --select @s --select substring(@s,len(@s)-2,3) end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
这里的关键还是 两个单眼号的连续使用,注意观察
if (@Description!=null) set @s=@s+' Description like ''%'+@Description+'%'''
这里%旁边的是两个单眼号,而不是双眼号;
select * from Book where BookName like '%'+@BookName+'%'

浙公网安备 33010602011771号