Using Named Parameterized Queries in DataGrip SQL Console
Learn how to write safe, reusable SQL with named placeholders like :emp_id in DataGrip, set values via the Parameters dialog, and verify that a PreparedStatement is used.
20 Aug 2025, 04:48 UTC

Quick answer: run a query with :name placeholders and fill them in the Parameters dialog
In DataGrip you can write SQL that uses named placeholders (e.g., :emp_id) instead of hard‑coding values. When you execute the statement, DataGrip shows a Parameters toolbar button. Clicking it opens a dialog where you supply a value for each placeholder. The IDE then sends the statement to the database as a java.sql.PreparedStatement, which protects against SQL injection and lets the database reuse the execution plan.
Worked example: PostgreSQL console
- Create a data source – Open the Data Sources dialog (
Ctrl+Alt+Shift+Son Windows/Linux,⌥⇧⌘Son macOS), add a PostgreSQL connection, and test it. You need read access to the schema you’ll query. - Open a SQL console – Right‑click the data source and choose
Open Console. The console defaults to the PostgreSQL dialect. - Write a query with named placeholders – In the editor type:
SELECT * FROM employees WHERE id = :emp_id AND department = :dept; - Execute the statement – Press
Ctrl+Enter(or click the run icon). DataGrip does not send the query yet; instead it highlights the Parameters button on the toolbar. - Fill in the parameters – Click the Parameters button. A small window appears with two fields labeled
emp_idanddept. Enter, for example,42foremp_idand'Sales'fordept(note the quotes for string literals). PressOK. - Run the query again** – Now press
Ctrl+Enter(or click the run icon). DataGrip sends the statement as a PreparedStatement with the supplied values bound to the placeholders. - Check the result** – The result grid shows only rows from the
employeestable whereid = 42anddepartment = 'Sales'. If no rows match, you see an empty grid. - Verify a PreparedStatement was used** – With the console still open, click the Explain Plan icon (or press
Ctrl+Shift+Enter). In the plan output look for a node labeledPreparedStatementor for the textParameter: :emp_id, :dept. Its presence confirms that DataGrip did not substitute the placeholders as plain text.
How the mechanism works
When DataGrip detects a colon‑prefixed identifier (:name) in the SQL text, it treats it as a named parameter rather than part of the SQL syntax. The IDE extracts all distinct parameter names, builds a java.sql.PreparedStatement with those names, and then binds the values you entered in the Parameters dialog to the corresponding indices. The driver (e.g., PostgreSQL’s JDBC driver) handles the actual binding; if the driver does not support named parameters, DataGrip falls back to positional binding, which can cause errors if the order of placeholders does not match the order you filled in the dialog.
Limits and version notes
- Driver support – The feature relies on the JDBC driver implementing
java.sql.PreparedStatementwith named parameter support. Recent PostgreSQL, MySQL 8.0+, and Oracle 12c+ drivers work. Older MySQL 5.x or Oracle 11g drivers may ignore the names and treat the statement as positional, leading toInvalid column indexerrors if you mix placeholders. - Dialect sensitivity** – Some dialects (e.g., SQLite) do not recognize the colon syntax as a placeholder; they treat
:nameas a literal string, causing a syntax error. Switching the console dialect may break the query. - Mixing placeholder types** – Do not combine
:namewith positional?placeholders in the same statement. DataGrip cannot map them correctly and will either bind nothing or throw an error. - Forgotten Parameters dialog** – If you run the query without opening the Parameters dialog, DataGrip binds
NULLto every placeholder, often returning zero rows or causing constraint violations. - JDBC batch execution** – Enabling
Use JDBC batch executionin the data source settings can hide individual parameter values in the console log, making debugging harder. Disable it when you need to inspect each execution.
Common mistakes and how to avoid them
- Assuming the placeholder works everywhere** – Test the query in a console for the target dialect before reusing it in a script or application.
- Entering values with wrong quoting** – Remember that the Parameters dialog expects raw values; you do not add SQL quotes around strings. Adding them results in literal quotes being stored (e.g.,
'"Sales"'). - Running the statement twice without resetting** – After the first execution, the Parameters dialog retains the last values. If you edit the SQL but forget to update the dialog, you may run the query with stale data. Always verify the dialog before executing.
- Misinterpreting the Explain Plan** – Some plans show the original SQL with placeholders; look specifically for the
PreparedStatementlabel or theParameter:line to be sure binding occurred.
Practical verification steps
To confirm that named parameters are working as expected:
- Run a simple query with a single placeholder, e.g.,
SELECT :val AS test;. - Open the Parameters dialog, set
valto123, and execute. - Check that the result grid returns a single column
testwith value123. - Open Explain Plan and verify the plan mentions a PreparedStatement or shows the bound value.
- Change the value in the dialog to
999and run again; the result should update accordingly, proving the statement is re‑used with new bindings.
If any of these steps fail, revisit the driver version, dialect setting, or check for mixed placeholder types.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.