0

Here is the simplified query that doesn't work.

SET @abc = CONCAT('%','string','%');

SET @query = CONCAT('SELECT * 
FROM table 
WHERE column LIKE ',@abc);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

I need to use CONCAT with SELECT because there lots of other variables in real query.

Real query works fine when I use some simple COLUMN=xyz in WHERE clause. But nothing works when I try to use LIKE %xyz%...

2
  • Let me guess, do you use instead of 'string' another column name? Commented Oct 20, 2015 at 13:46
  • The feature you require is "dynamic SQL" - stackoverflow.com/questions/190776/… Commented Oct 20, 2015 at 13:47

1 Answer 1

1

Use it like this

SET @abc = CONCAT('"%','string','%"');

SET @query = CONCAT('SELECT * 
 FROM table 
WHERE column LIKE ',@abc);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Check the first line I have added " to show @abc like "%string%"

Sign up to request clarification or add additional context in comments.

1 Comment

How does this prevent against SQL injections?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.