How to use LIKE in a t-sql dynamic statement in a stored procedure?

后端 未结 5 547
耶瑟儿~
耶瑟儿~ 2021-01-13 06:47

I\'m trying to use the LIKE keyword with the % wildcards wrapping the parameter, but I\'m not sure how to get the % characters into the statement without breaking it. Right

5条回答
  •  一生所求
    2021-01-13 07:29

    SET @SQLQuery = 'SELECT * from [tblApps] WHERE [firstName] LIKE ''%'' + @search + ''%'''
    exec sp_executesql @query=@SQLQuery, @params=N'@search nvarchar(96)', @search=@search
    

    The only difference from this version as compared to the others is that the dynamic execution of the sql is in fact parameterized which mitigates sql injection a bit.

提交回复
热议问题