Create SQL Query in function or PROCEDURE
Don't set @countRows twice.
Don't combine two queries into a single "statement".
Watch out for syntax errors when concatenating without a space.
SELECT @str_query; for debugging.