Help home > Running SQL

Running SQL

Write and run SQL in a SQL tab, and view the results and execution plans. Editing features such as formatting and commenting out are included.

What you can do

TaskHow
Open a SQL tabπŸ“ New SQL on the top bar (Ctrl+N), double-click a connection in the tree, or right-click it β†’ "New SQL Tab"
Change the connectionThe drop-down at the top left of the SQL tab

Getting started: running SQL (3 steps)

  1. Open a SQL tab

    Double-click a connection in the tree to open a SQL tab for it.
  2. Write SQL

    Table names are suggested as you type. You can also drag table and column names from the tree.
  3. Execute

    Press Ctrl+Enter to execute the statement at the cursor. The results appear below.

How to execute

ActionWhat is executed
β–Ά Execute, Ctrl+EnterThe selection if there is one, otherwise the statement at the cursor
β–Άβ–Ά Execute All, F5The selection if there is one, otherwise every statement in the editor

Statements run in order, and execution stops at the first error. Before running DROP (of tables, views, etc.), TRUNCATE, ALTER TABLE that drops a column, or UPDATE / DELETE without a WHERE clause, Musubi SQL lists the affected operations and asks you to confirm (you can turn this off in Settings).

Results are loaded up to the "Max" row count (default 1000). If there are more rows, the result tab gets a "+" and "(truncated at … rows)" appears below the grid.

Stopping execution

While SQL is running, a β–  Stop button appears. Press it to cancel the running SQL; the remaining statements in the script are not executed. The message "Execution was cancelled" appears.

Connection typeHow it is cancelled
SQL ServerCancels the running request (the connection stays open)
PostgreSQLCalls pg_cancel_backend from a separate connection
MySQL / MariaDBRuns KILL QUERY from a separate connection
OracleInterrupts the running call (ORA-01013)
JDBCUses the driver's cancel (Statement.cancel)

SQLite, SQL Server Compact, LocalDB and ODBC cannot be cancelled (the button is disabled). In manual commit mode, changes made by statements before the cancelled one remain in the transaction.

Variables (enter values at run time)

If your SQL contains variables, a dialog asks for their values when you execute it. This is handy when you run the same SQL repeatedly with different values.

SyntaxHow the value is embeddedExample
:nameNumbers as is, everything else as 'text'. You can also choose "NULL" or "As is"WHERE id = :id AND name = :name
${name}Exactly as typed (for table names, parts of conditions, etc.)SELECT * FROM ${table}

Statement separators

Results and messages

Commit and rollback

Turn off "Auto-commit" to switch to manual commit: nothing is committed until you press Commit or Rollback. The status bar shows "Manual commit". This setting is per connection.

Execution plans

Choose from Execution Plan β–Ύ. The plan is for the statement at the cursor (or the first statement in the selection).

TypeKeyDescription
Estimated execution planCtrl+EShows the plan without executing the SQL.
Actual execution planCtrl+Shift+EExecutes the SQL and measures row counts, loops and so on.
Actual execution plans for data-modifying SQLThe SQL runs after you confirm. With auto-commit, the changes are rolled back afterwards. In manual commit mode, the changes remain in the current transaction.
DatabaseEstimatedActual
SQL ServerSHOWPLAN_ALL (table with estimated costs)STATISTICS PROFILE
PostgreSQLEXPLAINEXPLAIN (ANALYZE, BUFFERS)
MySQLEXPLAIN FORMAT=TREEEXPLAIN ANALYZE
OracleDBMS_XPLAN.DISPLAYDBMS_XPLAN.DISPLAY_CURSOR (SELECT only)
SQLiteEXPLAIN QUERY PLAN (shown as a tree)Not available

Tree view and problem spots

The plan is shown as a tree of operations (use Tree / Raw Output at the top right of the result to switch to the output exactly as the database returned it).

FindingTypical cause / fix
Full scan repeated inside a loopThe join column has no index
Full scan despite a filter conditionThe WHERE column has no index
Estimated and actual row counts differ greatly (actual plan only)Statistics are stale. Shown only on the operation where the gap starts
Sort or hash does not fit in memoryNot enough working memory (such as PostgreSQL's work_mem)
No join condition (Cartesian product)A missing join condition
Key lookup (SQL Server)Can be fixed with an index that INCLUDEs the missing columns
Missing statistics and other warnings (SQL Server)Warnings from SQL Server, shown as is
Findings are only a guideA full scan of a small table, for example, is often harmless. MySQL plans in the old format (before 8.0.16, and MariaDB) are shown as is, not as a tree.

Editing features

Use them from the "Edit" menu, the editor's context menu, or the keyboard. Conversions and formatting apply to the selection if there is one, otherwise to the whole editor.

FeatureKey
Find / Find Next / Find PreviousCtrl+F / F3 / Shift+F3
ReplaceCtrl+H (Ctrl+R also works)
Go to LineCtrl+G
Comment Out / UncommentCtrl+K / Ctrl+Shift+K
Format SQLCtrl+Q (Ctrl+Shift+F also works)
Format SQL as One Line (line comments become /* */)Ctrl+Shift+Q
To Uppercase / To Lowercase / Keywords to Uppercase"Edit" menu β†’ "Convert"
Temporary Font Size"Edit" menu β†’ "Temporary Font Size"
Copy Entire Current Result Set / Copy All Result Sets"Edit" menu (tab-separated, with headers)
Copy SQL with Formatting (Colors and Indentation)Ctrl+Shift+C, the editor's context menu, or the "Edit" menu

Copy with formatting (for pasting into Excel design documents and the like)

Copies the selected SQL (or all of it if nothing is selected) with its syntax coloring (keywords, strings, comments, etc.) and indentation intact. Format it with Ctrl+Q first for a more readable result.

Find and replace

Works the same as in VS Code. Ctrl+F shows the find box above the editor, and Ctrl+H also shows the replace box (you can toggle it with β–Έ / β–Ύ on the left). If text is selected when you open it, that text is searched for.

Marking likely errors (Check)

Musubi SQL checks the SQL as you type and marks likely errors with a squiggle and a marker at the start of the line (red: error, yellow: warning). Hover over a mark to see the reason. Toggle it with "Lint" on the toolbar or in Settings.

The check is only a guideIt does not fully parse the SQL, so it cannot find every mistake. For procedure definitions and the like, only parentheses and strings are checked.

Source strings and code stripper

Generate Source String turns SQL into code that embeds it in a program (C#, Java, VB.NET, JavaScript / TypeScript, Python, PHP, Delphi). Example (C#):

Original SQLSELECT 1
FROM t
C#var sql = new StringBuilder();
sql.AppendLine("SELECT 1");
sql.AppendLine("FROM t");

Code Stripper does the reverse: it extracts the strings from program code and turns them back into SQL.

Executing stored procedures

In the tree, right-click a stored procedure or function β†’ "Execute..." to open a dialog for its parameters. Enter the values and press Execute to run it in a SQL tab and show the results. Values of OUTPUT (OUT) parameters are also shown as results (SQL Server, MySQL). Show SQL (Do Not Execute) only generates the call statement. Right-click β†’ "Open Call Statement in SQL Tab" opens a template with every parameter set to NULL.

How values are passedIf you enter digits for a numeric parameter, it is passed as a number; anything else is passed as a string. Turn on NULL to pass NULL.

Execution history

Every SQL statement you execute is recorded automatically, whether it succeeds or fails (up to 2000 entries, newest first). Open the history with History in the SQL tab, πŸ•˜ History on the top bar, Ctrl+Shift+H, or by right-clicking a connection in the tree β†’ "SQL Execution History...".

TaskHow
SearchType part of the SQL or a connection name in the box at the top. Separate words with spaces to show only entries that contain all of them. You can also filter with "This connection only" and "Errors only"
Insert into the editorDouble-click the row, press Enter, or press Insert into Editor (when opened from a SQL tab; inserts at the cursor)
Reopen or re-executeOpen in New Tab / Execute in New Tab (uses the connection recorded with the entry)
DeleteDelete (the selected entry) / Delete All

The list shows the date, connection, SQL, result (rows fetched, rows updated or error) and elapsed time. The full SQL and error details of the selected entry appear below.

Saving and opening files

Save with Ctrl+S and open with Ctrl+O. "File" β†’ "Recent Files" lets you quickly reopen recently opened or saved SQL files and ER diagrams (up to 10). You can also drag and drop .sql files (and .txt and .msqer) from Explorer or elsewhere anywhere onto the window to open them (if a file is already open, its tab is shown). The encoding is detected automatically: UTF-8 (with or without BOM), UTF-16 or Shift_JIS. Tabs with unsaved changes are marked with ●, and you are asked to confirm when you close them.

FAQ

The result stops partway through

It is truncated at the "Max" row count. Increase "Max" on the toolbar, or use "Fetch all rows again and export them" in the export dialog.

SET and similar settings do not carry over to the next execution

They do carry over within the same SQL tab (the same connection). They are reset when you disconnect.

β–² Back to top

← Musubi SQL website (download)