目前遇到的问题是查询字符串中含有“【】”后,存储过程Like查询不出结果,原存储过程代码如下:
ALTER PROCEDURE [dbo].[Base_CompanySelect] @PageSize int=0, @CurrentPage int=1, @Identifier int=NULL, @Company_Name nvarchar(50)=NULL AS BEGIN SET NOCOUNT ON; IF (NOT @Company_Name IS NULL) BEGIN SET @Company_Name='%'+@Company_Name+'%' END SELECT * FROM base_Company WHERE company_Name like @Company_Name GO
解决办法,就是先将“【】”进行转译,然后利用SQL模糊查询语句和Escape转义字符中的Escape函数进行Like查询,修改后的代码如下:
ALTER PROCEDURE [dbo].[Base_CompanySelect] @PageSize int=0, @CurrentPage int=1, @Identifier int=NULL, @Company_Name nvarchar(50)=NULL AS BEGIN SET NOCOUNT ON; IF (NOT @Company_Name IS NULL) BEGIN SET @Company_Name='%'+@Company_Name+'%' SET @Company_Name=replace(@Company_Name,'[','\[') SET @Company_Name=REPLACE(@Company_Name,']','\]') END SELECT * FROM base_Company WHERE company_Name like @Company_Name E s c a p e('\') GO