Quick revision
The short answer
Concatenating parameters into SQL text inside a procedure is SQL injection, even when the Java code calls the procedure with a PreparedStatement, because the injection happens inside the database when the string is built. The safe rewrite passes values through ? placeholders with PREPARE and EXECUTE ... USING, validates identifiers such as sort columns against an allow-list because they cannot be bound, and runs the procedure with SQL SECURITY INVOKER or least-privilege definer rights, with the application granted only EXECUTE on the procedures it needs.
What to remember
In this answer, we saw that concatenating parameters into dynamic SQL inside a MySQL procedure is SQL injection, regardless of how safely Java calls the procedure, because the string is parsed as SQL inside the database. The rewrite binds values with ? placeholders and EXECUTE ... USING, maps identifiers such as sort columns through an allow-list to fixed SQL, and rejects anything else. We also reviewed SQL SECURITY: DEFINER procedures run with their creator's rights, so injection there is worse, and the application should hold only EXECUTE on the procedures it needs and least privilege on tables.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Concatenating parameters into SQL text inside a procedure is SQL injection, even when the Java code calls the procedure with a PreparedStatement, because the injection happens inside the database when the string is built. The safe rewrite passes values through ? placeholders with PREPARE and EXECUTE ... USING, validates identifiers such as sort columns against an allow-list because they cannot be bound, and runs the procedure with SQL SECURITY INVOKER or least-privilege definer rights, with the application granted only EXECUTE on the procedures it needs.
Why this question is asked
Security reviews often flag dynamic SQL in stored procedures, and developers are sometimes surprised, because the Java side already uses prepared statements. Interviewers ask this to check that we know where injection actually happens, the difference between values and identifiers, and how MySQL's privilege model can turn a small bug into full database access. In this answer, we will look at the vulnerable procedure and an attack on it, see why the Java prepared statement does not help, rewrite it safely with placeholders and an allow-list, and then fix the privileges.
1The Vulnerable Procedure
Here is a typical reporting procedure. It filters orders by region and lets the caller choose the sort column, building the query as a string with CONCAT and running it with PREPARE and EXECUTE, MySQL's way of running a statement whose text is built at runtime.
Notice that the attacker's text closes the string literal with a quote, adds its own query, and comments out the rest. The procedure faithfully builds and runs it.
2Why the Java PreparedStatement Does Not Help
On the Java side, the code probably looks safe: CallableStatement with {call sales_report(?, ?)} and setString for both parameters. That does protect the CALL statement: the values reach the procedure as data, exactly as the user typed them. But inside the procedure, CONCAT pastes that data into a new SQL string, and PREPARE parses the string as code. The protection ends at the procedure's boundary, and the injection happens one step later, entirely inside MySQL.
So the rule is the same everywhere: any time a string becomes SQL text, whether in Java, in a procedure, or in a reporting tool, values must be bound as parameters, never concatenated.
5 messages in order. Play them, or step through them one at a time.
Binding at the CALL protects only the CALL; the procedure builds new SQL from the value.
3Rewrite: Bind Values, Allow-List Identifiers
Values go through ? placeholders. MySQL's PREPARE accepts ? markers in the statement text, and EXECUTE stmt USING @var supplies the values, which are sent as data and never parsed as SQL. In MySQL, EXECUTE ... USING takes user variables, so we copy the parameter into one first.
Identifiers are different. A column name in ORDER BY is part of the query's structure, so it cannot be a placeholder: ORDER BY ? would sort by a constant string and do nothing useful. For identifiers we validate the input against an allow-list of known names and map each allowed choice to a fixed piece of SQL that we wrote. Anything else is rejected with an error. The same applies to sort direction and table names.
Notice that the only strings concatenated into the SQL now are constants written in the procedure. The attack string is still passed in, but it is compared with region values as data, matches nothing, and returns no rows. Often the best rewrite has no dynamic SQL at all: a fixed query per sort option, or ORDER BY CASE expressions, avoid PREPARE completely. Dynamic SQL is worth it only when the number of combinations really is large.
4Privileges: DEFINER Versus INVOKER
In MySQL, a stored procedure has an SQL SECURITY characteristic, and the default is DEFINER. A definer procedure runs with the privileges of the account that created it, often a powerful administrative account, whatever the caller is allowed to do. That is useful for granting narrow access, but it makes injection much worse: the injected UNION SELECT FROM users ran with the definer's rights, even if the application account could not read users. With SQL SECURITY INVOKER, the procedure runs with the caller's privileges, so an injection can do no more than the caller already could.
Whichever we choose, least privilege applies. The application account gets EXECUTE on the procedures it needs and SELECT on the tables those procedures read under INVOKER security, and nothing else: no access to users, no DDL, no FILE privilege. If we do need DEFINER, for example to expose a report without granting table access, the definer should be a dedicated account with exactly the privileges the procedure needs, not root.
5Follow-up questions
Can QUOTE() make concatenation safe?
QUOTE() escapes a string so it can be embedded as a literal, and it does stop the quote-breaking attack for values. But it is easy to forget in one place, it does nothing for identifiers, and binding with USING is clearer. We use placeholders for values and allow-lists for identifiers, and treat QUOTE() as a last resort.
How would you find other vulnerable procedures?
Search the routine definitions in information_schema.routines for PREPARE and CONCAT together, review each one, and add a check in code review or a linter for new procedures. Procedures with SQL SECURITY DEFINER go first, because injection there runs with more privileges.
Does the same apply to PostgreSQL?
Yes. In PL/pgSQL we use EXECUTE ... USING for values and format() with %I for identifiers, which quotes them safely, and SECURITY DEFINER functions also need a fixed search_path so callers cannot redirect object names to their own objects.
6Conclusion
In this answer, we saw that concatenating parameters into dynamic SQL inside a MySQL procedure is SQL injection, regardless of how safely Java calls the procedure, because the string is parsed as SQL inside the database. The rewrite binds values with ? placeholders and EXECUTE ... USING, maps identifiers such as sort columns through an allow-list to fixed SQL, and rejects anything else. We also reviewed SQL SECURITY: DEFINER procedures run with their creator's rights, so injection there is worse, and the application should hold only EXECUTE on the procedures it needs and least privilege on tables.
Open with
The risk is SQL injection inside the database. The Java side may use a prepared statement for the CALL, but that only protects the call itself. Inside the procedure, CONCAT pastes the parameter into a string and PREPARE parses that string as SQL, so a region value like x quote UNION SELECT from users becomes part of the query.
Then, if they want more
To fix it, I would bind every value with a question mark placeholder and run it with EXECUTE stmt USING a user variable, so the value is sent as data and never parsed.
Identifiers, like the sort column, cannot be bound, so I map the input through an allow-list, a CASE with the known options, each producing a fixed piece of SQL I wrote, and I raise an error with SIGNAL for anything else. Often I can avoid dynamic SQL entirely with a fixed query per option.
Then privileges: MySQL procedures default to SQL SECURITY DEFINER, which runs them with the creator's privileges, so an injection there can read tables the application could never touch. I would switch it to INVOKER, or if definer rights are needed, use a dedicated low-privilege definer account.
And the application account gets only EXECUTE on the procedures it uses plus the minimum table privileges. Finally I would search information_schema.routines for other procedures that combine CONCAT and PREPARE.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A reporting procedure in MySQL 8.4 builds dynamic SQL from parameters with CONCAT and...": https://scrivio-interview-library.netlify.app/databases/stored-code/60a1d1d342/