Skip to content
The latest version of DbVisualizer was released 2024-03-11DOWNLOAD HERE ->

Editing SQL Scripts

The SQL Commander contains an SQL editor, used to edit SQL scripts.

The editor area looks like this:

Screenshot of the SQL Commander editor interface

Above the editor is a toolbar with buttons related both to execution of scripts and to editing. The editing related buttons are covered below.

The left margin shows the line numbers.

Below the editor, you see a Status Bar with the following fields, from left to right:

  1. Position: the current caret position in the format: <line>:<column> [<position from top>] </br>The last figure, within square brackets, is the caret position from the top. This can be useful when you get an error message executing a script that contains this information rather than a line/column location.
  2. Insert/Overwrite Mode: INS if characters you type will be inserted at the caret position or OVR if they will overwrite the current text at the caret position. You can toggle this mode using the Toggle Typing Mode keyboard shortcut, by default bound to the Insert key.
  3. Macros: this field is only visible when working with macros, as described in the Recording and Playing Edit Macros section.
  4. File Format: the format of the file as detected when the file was loaded (if any). Click it to select which format to use when saving the file; Windows, Unix/Linux/macOS, or Old MacOS.
  5. Auto Commit Status: shows whether or not Auto Commit is enabled.
  6. Character Set: the character encoding as detected when the file was loaded (if any). Click it to select which encoding to use when saving the file.
  7. File: The name of the loaded file (if any). You can click on the filename to copy the file path or open the OS file chooser for the directory holding the file. If you just type into the editor without loading a file, the filename "Untitled" is shown instead. An asterisk (*) after the filename indicates that there are unsaved edits.

The SQL Editor is like any editor you're used to when it comes to typing, scrolling etc. But it also offers additional features to help you specifically with editing SQL scripts. These are described in the following sections.

You can change how to display in the Tools->Tool Properties dialog, in the General / Appearance category. This is explained in more detailed below.

Row Delimiter

This is how the SQL editor handles row delimiter:

  1. Linefeed (LF) is used as the line break between rows.
  2. Loaded files/texts are scanned for carriage return (CR) or carriage return, line feed (CRLF). The first found line break that is not a LF is set as the line break style for the document.
  3. Drop of a file to an existing document does not change the line break.
  4. Paste text do not change the line break.
  5. Copy text do not transfer the line break.
  6. The setting in the SQL Editor determines the line break used at next save.
  7. The setting in tool properties determines line break for new SQL Commanders (files).

In the editor status bar, the current line delimiter is shown. Click it to modify what line delimiter should be used at next save.

Screenshot showing how SQL editor handles row delimiter

Font Settings

In the Appearance/Fonts category, you can select the font for Text Editors to control the font in the SQL Editor (Monospaced Fonts are recommended). The Grids settings controls which font to use in all result set grids while the Application font controls the rest of the user interface.

Screenshot of font settings in SQL Commander editor

Editor Styles

An SQL script consists of keywords, operators, object identifiers, quoted text, etc. It may also contain comments. To make it easier to see at a glance what is what, the SQL Editor displays words using different font styles depending on their classification. For instance, keywords are displayed with a bold blue font, while quoted text is displayed with a regular type red font.

In the Appearance/Editor Styles category you can select colors for the different kinds of words, as well as the editor selection background color, the current line highlight color and the editor background color, and more.

The SQL Editor Column Guide is an optional visual guide that appears as a thin vertical line at specified column (this works best if you use monospaced fonts).

Screenshot of SQL editor styles settings

Comments

The editor uses the Tool Properties settings from the SQL Commander/Comments category under the General tab to detect comments.

Screenshot of comment settings in SQL Commander editor

Charsets and Fonts

You can also change the SQL Editor font family, which is useful and necessary in order to display characters for languages like Chinese, Japanese, etc., in Tool Properties in the Appearance/Fonts category to set the font for the SQL Editor (see Internationalization and Localization (i18N and L10N) for more information).

Screenshot of charsets and fonts settings in SQL editor

Loading and Saving Scripts

The SQL editor supports loading statements from a file and saving the content of the editor to a file. Use the standard file operations, Open, Save and Save As in the File main menu or the main toolbar to accomplish this. Loading a file loads it into a new SQL Commander tab or activates the tab that already holds it.

The name of the loaded file is listed in the status bar of the editor, with the full file path shown in the window title. The editor tracks any modifications and indicates changes with an asterisk (*) after the filename. When you close the SQL Commander tab or exit DbVisualizer, you are asked what to do if there are any pending edits that need to be saved.

The File->Open Recent submenu lists the recently loaded files. How many recent files to keep track of can be specified in the Tool Properties dialog, in the SQL Commander category under then General tab.

You can also use the Quick File Open feature to open recent files as well as Bookmarks and History entries. By default, it is bound to the Ctrl+Alt+O key combination, and is also available via a main toolbar button as well as in the main File->Quick File Open menu.

Screenshot showing Quick File Open feature in SQL Commander

Stale Files Warning

SQL Commander monitors open files to detect external changes, for example if an open file is modified by an external editor, reloaded from a network connection or repository, etc. If a file is externally modified, you will see a warning ribbon at the top of the editor area, presenting options to handle the situation:

Screenshot showing a stale files warning in SQL Commander

You can turn off this control in Tool Properties/SQL Commander:

Screenshot of stale file warning settings in Tool Properties

This does not affect the check when you save the file; DbVisualizer will always check and warn if you're trying to save a file that was externally modified.

Drag and Drop a File

You can also select a file in the platform's file browser and drop it somewhere in the DbVisualizer window. If you drop it in an editor, the file content is inserted at the caret position in the editor. If you instead drop it in the toolbar area, the file is opened in a new SQL Commander tab.

Drag and Drop Database Objects

If you want to include an object shown in the database objects tree, you can select the node and drop it in the editor where you want it inserted. The Script Object dialog is shown where you can select exactly what you want to insert in the editor.

Screenshot of dialog box for dragging and dropping database objects in SQL editor

First of all, you can select to insert an SQL statement based on the dropped object, e.g. a SELECT statement or a CREATE statement. You can also choose to just insert the object name. The choices available depends on the type of object you drop.

In the Options area, you can opt to format the SQL before it is inserted and use qualifiers and quoted identifiers, and even change which statement delimiter to use.

The Output Destination is set to the SQL Commander tab you dropped the object on by default, but you can change your mind and pick another destination. If you stick with an SQL Commander as the destination, you can tell where in the editor to insert the text.

You can also open this dialog from the Databases tab, from the object's right-click menu.

If you just want to insert the object names in the editor, hold down the Ctrl key (or the Alt key on macOS) while dragging and dropping.

This behavior can be reversed in Tool Properties, in the SQL Commander category, so that dropping without pressing a key inserts the names and pressing the key launches the dialog."

Loading and Saving Bookmarks and Monitors

Bookmarks and Monitors are also files, but with special meaning. See the Managing Frequently Used SQL for how to create and edit them in the SQL Editor.

When you execute a script, DbVisualizer saves it as a history entry, see the Re-Executing SQL Statements section for details. You can use the Previous and Next buttons in the editor toolbar to navigate between (load) these entries.

Screenshot showing how to navigate between highlighted matches in SQL editor

By selecting the script filename in the right bottom corner it is possible to copy its path or locate it in the file system or in the Scripts tab.

Screenshot highlighting how to navigate to script location in SQL editor

Confirming Overwriting Unsaved Changes

By default, you have to confirm overwriting unsaved changes in an editor, e.g. when navigating between history entries, and when closing an SQL Commander tab with unsaved edits. You can disable these confirmation popups in the Tool Properties dialog, under the SQL Commander category under the General tab.

Finding and Replace in the Editor

The Edit main menu and the editor right-click menu contains the Find (Ctrl+F) and Replace (Ctrl+R) actions.

Find shows a pane at top of the editor where you can enter what to look for, either as text, a regular expression or using wildcards. You can also limit the search to the current selection and use other options for a more precise search. Use Find Next and Find Previous to navigate to other matches, by default mapped to the F3 and Shift-F3 keys, or Down and Up keys.

Find next and find previous

Matches are highlighted while typing in the search field.

When in Replace mode, a second field is displayed used to enter the replacement text.

Replace text

When you use a regular expression with group expressions in the Find what field, you can reference the captured text in the Replace with field using the dollar sign ($) plus the group number (e.g. $1 for the first group).

Regular Expression Example:

Text in editor:

SELECT
    firstname,
    lastname
FROM
    person;

Find what:

^(\s+)(\w+)(,?)$

Replace with:

$1person.$2$3

Deselect Match whole word, select Entire scope and Use Regular Expression.

When you click Find and choose All in the following dialog, you get the following result:

Resulting text:

SELECT
    person.firstname,
    person.lastname
FROM
    person;

SQL Formatting

Only in DbVisualizer Pro

This feature is only available in the DbVisualizer Pro edition.

Auto Completion

Only in DbVisualizer Pro

This feature is only available in the DbVisualizer Pro edition.

Auto completion is a convenient feature used to assist you when editing SQL statements and DbVisualizer commands. By default, you activate auto completion with the key binding Ctrl-SPACE, but you can also configure it to activate as you type (in the Tool Properties dialog, in the SQL Editor/Auto Completion category under the General tab).

With the caret in any place in a statement where you can type something other than a table name or a column name, and at least one character just before the caret, activating auto completion displays a list of keywords that starts with the letters you have typed so far. As you continue to type, the list narrows.

Screenshot of auto completion list showing keywords in SQL editor

The list of keywords is database specific, selected based on the database type for the connection currently selected in the Database Connection list above the editor.

With the caret placed where a table or view name may be typed in a supported SQL statement type, the auto completion list shows a list of tables and views from the currently selected database connection, assuming you are actually connected to the database. The following figure shows the completion pop up with table names that contain the letter S.

Screenshot of auto completion list showing tables and views in SQL editor

A completion pop-up showing column names is shown when the caret is placed where a column name may be typed.

Screenshot showing auto completion for column names in SQL editor

DbVisualizer provides auto completion for table and columns names for the following DML commands:

  • SELECT
  • INSERT
  • UPDATE
  • DELETE

Auto completion for DbVisualizer commands is very similar. Activating it after a partial command name lists all matching commands. If you activate it after a complete command name, you get a list of all valid parameters for the command. After a parameter name, you can select from a list of valid values.

Screenshot showing auto completion for DbVisualizer commands in SQL editor

For the @export set command, the parameter list is adapted for the specified output format after you have entered the Format parameter setting, for instance only showing parameters that are valid for the CSV format.

Screenshot showing auto completion for @export set command in SQL editor

To display the completion pop-up, use the key binding Ctrl-SPACE (by default). You select an entry in the pop-up menu with a mouse double-click, the ENTER key, or the TAB key. To cancel the pop-up, press the ESC key.

If there are several SQL statements in the editor, make sure to separate them using the statement delimiter character (the default is ";").

In order for the column name completion pop-up to appear, you must first make sure there are table names in the statement. All table names that have been listed in the completion pop-up are cached by DbVisualizer to make sure subsequent displays of the pop-up is performed quickly without asking the database. The cache is cleared only when doing a Refresh in the database objects tree or reconnecting the database connection. The Database and Schema lists above the editor are used to limit the list of tables in the auto complete pop-up to those in the selected database and/or schema. To include all tables, select the blank entries in these lists. The default selections for the lists can be set as connection properties, in the SQL Commander category.

It is possible to fine-tune how auto completion works in the connection properties.

  • Enable or disable the use of identifier qualifiers (i.e. qualifying table names with the schema name) in the [Database Type]/Qualifiers category,
  • Enable or disable the use of delimited identifiers (e.g. quotes around a table name) in the [Database Type]/Delimited Identifiers category.

Sorting, when to show the popup, upper/lower case transformation, etc. can be configured in the Tool Properties dialog, in the SQL Editor/Auto Completion category under the General tab.

Recording and Playing Edit Macros

If you repeatedly need to run a sequence of edit operation, you can record them as a macro and play it as many times as needed during an editing session. The editor status bar indicates when a recording is in progress and when a macro is available to play.

As an example, suppose you have some plain text that you need to convert into INSERT statements:

12345 123456
89012 890123
45678 456789

Place the caret at the beginning of the first line and start the macro recording, using the right-click menu or the corresponding key binding, and then type text and use key bindings to perform the following operations:

  1. Type insert into mytable values('
  2. Insertion Point to Next Word
  3. Type ',
  4. Insertion Point to Next Word
  5. Type '
  6. Insertion Point to Next Word
  7. Type ');
  8. Insertion Point Down
  9. Insertion Point to Beginning of Line

Then stop the recording. You now have a macro for converting a single line to an INSERT statement. To convert the remaining lines, just use Play Macro for each line. The result will look like this:

insert into mytable values('12345', '123456');
insert into mytable values('89012', '890123');
insert into mytable values('45678', '456789');

The Find operation, by default mapped to the Find key and Ctrl-F key stroke, can not be recorded. You must instead use Find Selection, Find with Dialog, Find Next and Find Previous. Mouse gestures are also not recorded, only key strokes and menu selections.

Folding

If you work with a large script containing many individual statements, it can sometimes be helpful to hide some. You can do so using the Folding feature. Folding->Toggle Folding in the right-click menu.

Here's an unfolded script with the statements visible:

Screenshot of an unfolded script in SQL editor

Here is the same script with statements folded. Use the button in the row number area to expand/collapse a statement.

Screenshot of a folded script in SQL editor

Highlighting Matches

Instead of searching for occurrences of a text string and navigating to each occurrence, it is sometimes useful to get all occurrences highlighted. To do this, select a text string that is at least three characters long and contains at least one letter or digit. You can use the Tool Properties dialog to enable or disable this feature (in the General / SQL Commander category) or change the colors used (in the General / Appearance / Editor Styles category).

Screenshot showing how to enable Show Special Characters feature in SQL editor

Tab Key Treatment

Pressing the TAB key in the editor inserts eight (8) space characters by default. If you instead want a TAB character to be inserted, or want to insert another number of space characters, you can specify this in the Tool Properties dialog, in the General / SQL Commander category under the General tab.

Show Special Characters

Sometimes it is useful to render characters that are not visually represented such as whitespace, tab, newline, etc. To do this, make sure an SQL editor is active and then open the Edit meny and toggle Show Special Characters.

With Show Special Characters enabled:

Key Bindings

The editor shortcuts, or key bindings, can be redefined in the Tool Properties dialog, in the Key Bindings category under the General tab (see Changing Keyboard Shortcuts). Expand the Editor Commands node to manage all editor actions and the Main Menu/Edit node to manage the key bindings for the edit operations in the right-click editor menu and the main window Edit menu.