# SQL templates

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/go/2026.3/llms.txt).

A structural search lets you find code fragments that match a user-defined search template. If the template matches part of the source code, the corresponding expression is highlighted. A search template is essentially a query made up of text, variables, filters, scopes, and context. Using these elements, you can refine your search and achieve more precise results.

## Search tools

A variable is a sequence of characters enclosed in dollar signs, for example, `$name$` or `$field$`.

The screenshot below shows a template used to search for structs.

![Variable without filters](https://resources.jetbrains.com/help/img/idea/2026.3/go_structural_search_variables_without_filters.png)

Modifiers are user-defined conditions that can be applied to variables and search templates. They allow you to:

* check how many times a variable occurs,

* validate the variable’s type,

* match the variable against plain text or a regular expression,

* apply a Groovy script.

For the complete list of available modifiers, see [Search templates, modifiers, and script constraints](search-templates.html).

The next example shows the same template as before, but with the `$name$` variable using the Text modifier set to `Employee`. Notice how the highlighted search results in the editor have changed.

![Variable with Text filter applied](https://resources.jetbrains.com/help/img/idea/2026.3/go_structural_search_variables_with_text_filter.png)

If we apply the type modifier with the `[string]` value, we will find all the string values in our `SELECT` statements. But to find all the string types in the table definition (the `CREATE TABLE` statement), you need to apply the types context. To set context, you also use filters. But in contrast to variables, where the filter is applied to a specific variable, context filters are applied to the whole search template.

![Structural Search with filters](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_filters.png)

Contexts are built-in internal templates that help the search engine identify the variable. Contexts are used only for SQL to eliminate language ambiguities. By using context, you can tell the search engine where and what variable to search. You can set the following contexts for search templates:

* expressions: one or more values, operators, or SQL functions that result in to a value. With the expressions context, IDE will look for `$expression_1$` and `$expression_2$` in structures like `SELECT $expression_1$ FROM table_name where $expression_2$`.

* types: the type of value that can be stored in a table column (for example, `varchar(100)`). With the types context, IDE will look for `$type_1$` and `$type_2$` in structures like `CREATE TABLE table_name (id_1 $type_1$, id_2 $type_2$)`.

* query clauses: query clauses like `DISTINCT`, `FROM`, `WHERE`, `ORDER BY`, `GROUP BY`, or `HAVING`. In the query clauses context, you need to use a clause keyword with a variable. For example, the IDE will look for `where $variables$` in structures like `select * from table_name $variables$;`. If you have an expression like `a + b` in the `WHERE` clause, you can specify your variables explicitly like `where $variable_1$ + $variable_2$;`. And define any variable as a search target.

![Context in structural search](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_context.animated.gif)

The Search target is a list in which you can specify what part of your template the IDE should search: a single variable or the whole template. For example, you can use search targets to find the `$b$` value from the `where $a$ + $b$` search template with `context = query clause`.

![Search targets in structural search](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_search_targets.png)

The Scope defines sources of information where you want to perform your search. It can be open or current files, directories in the   Project tool window  (`Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+Shift+E` (VSCode), `⌘ ⇧ E` (VSCode (macOS))) , recently changed files, and other sources. To search in the specific directory, select Directory, press the Browse button.

![Scopes in structural search](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_scopes.png)

The File type defines dialects of files in which you want to search.

![Select the file dialect](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_select_dialect.png)

## How do SQL templates for structural search work

Let's see how the search engine processes search templates. Consider that you have the `WHERE $a$` search template in the search field, and you set `context = query clause`.

1. GoLand takes the definition of the query clauses context template. For the  query clauses  context template, the definition is `SELECT id from dummy $pattern$`. You can see all existing context templates under the Existing Templates menu item.

2. The IDE replaces the `$pattern$` variable with the value that you defined in the search template. In our case, it is `WHERE $a$`. The replacement result will look like: `SELECT id from dummy WHERE $a$`.

3. The IDE parses the final `SELECT` statement and searches for `WHERE $a$` in the syntax tree.

4. When `WHERE $a$` is found, the IDE highlights it in the editor.

![Algorithm for structural search](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_algorithm.svg)

## Search and replace structurally

Procedure: Search structurally

1. Click `Edit | Find | Search Structurally…`.

2. From the File type list, select a language that you want to use in your search.

3. In the Search template field, type a search template or select the existing template.

Do not select templates under the `SQL | Common | Contexts` node, these templates are used internally by the search engine to eliminate language ambiguities. Saved SQL templates are under the `User Defined` node.

4. If the modifier panel is hidden, click the Toggle Modifier Panel button (![the Toggle Modifier Panel button](https://resources.jetbrains.com/help/img/idea/2026.3/app.general.filter_dark.svg)).

5. Click each template variable and set a search modifier. To set the modifier, click the Add modifier link.

GoLand instantly highlights the found code occurrences in the editor.

6. (SQL only) Add the SQL context.

In the Search template field, place the caret at the beginning of the template and click the Add modifier link. From the list, select  Context.

7. (Optional) From the Scope list, select where you want to search: in a project, module, directory, or within a custom scope.

8. (Optional) From the Search target list, select what part of the template you want to search for: separate variables or the whole template (Complete match).

9. Click Find.

![Animation that shows how to search structurally](https://resources.jetbrains.com/help/img/idea/2026.3/db_structural_search_existing_templates.animated.gif)

Procedure: Replace structurally

1. In the main menu, go to `Edit | Find | Replace Structurally`.

2. In the Search template field, type a search template or select the existing template.

Do not select templates under the `SQL | Common | Contexts` node, these templates are used internally by the search engine to eliminate language ambiguities. Saved SQL templates are under the `User Defined` node.

3. In the Replace template field, type a search template or select the existing template.

4. If you need to add a modifier for the variable in the replace template, place the caret at the variable and use the modifier area to manage modifiers.

5. Click Find. GoLand displays the results in the Find tool window.

6. In the Find tool window, you can work with the results further, replacing found items one by one, all of them at once, or previewing your potential changes.

![Replace a target structurally](https://resources.jetbrains.com/help/img/idea/2026.3/db_replace_target_structurally.png)

