I'm facing a very strange behavior of a like search within a sp_executesql:
This statement returns 0 rows:
exec sp_executesql N'SELECT * FROM MyTable WHERE Name LIKE ''%'' +
@Name + ''%''',N'@Name nvarchar(7)',@Name=N'100024'
When this equivalent returns the desired row:
DECLARE @Name nvarchar(7)=N'100024'
SELECT * FROM MyTable WHERE Name LIKE '%' + @Name + '%'
What's wrong with exec sp_executesql command? if I use it to search other row such as "100033" it finds the row, so syntax must be fine (in fact the query was taken from SQL Profiler when trying to debug why my asp.net page didn't found this particular element)