We are trying to upgrade our classic asp search engine to protect it from SQL injection. We have a VB 6 function that builds a query dynamically, combining a query together based on various search parameters. We converted this to a stored procedure using dynamic sql for all parameters except keywords.
The problem with the keywords is that the user has variable numeric words, and we want to find several columns for each keyword. Since we cannot create a separate parameter for each keyword, how can we create a secure query?
Example:
@CustomerId AS INT
@Keywords AS NVARCHAR(MAX)
@sql = 'SELECT event_name FROM calendar WHERE customer_id = @CustomerId '
--(loop through each keyword passed in and concatenate)
@sql = @sql + 'AND (event_name LIKE ''%' + @Keywords + '%'' OR event_details LIKE ''%' + @Keywords + '%'')'
EXEC sp_executesql @sql N'@CustomerId INT, @CustomerId = @CustomerId
What is the best way to handle this and maintain protection against SQL injection?