Keywords: DBeaver | SQL script execution | database connection | auto-sync | parameter binding
Abstract: This article provides an in-depth exploration of various methods for executing SQL scripts in DBeaver database management tool, with particular focus on the core technology of script execution through auto-sync connection functionality. The content systematically analyzes key aspects including setting active connections, executing partial or complete scripts, utilizing keyboard shortcuts, and managing execution results. Additional coverage includes advanced features such as parameter binding, variable configuration, and script execution settings, offering database developers a comprehensive SQL script execution solution. Through systematic operational guidance and detailed technical analysis, the article assists users in efficiently leveraging DBeaver for database development tasks.
Fundamental SQL Script Execution Operations
Executing SQL scripts in DBeaver requires ensuring that script files are opened in the SQL editor. The crucial step involves enabling the Auto-sync connection feature, which is available in the dropdown menu of the Set active connection from database navigator connection button. Once activated, this feature automatically establishes association between the SQL editor and the connection selected in the database navigator.
In certain scenarios, enabling auto-sync connection immediately activates the SQL console within the SQL editor panel. If automatic activation does not occur, users need to manually select the target working schema in the Database Navigator. This step ensures that SQL scripts execute within the correct database environment.
Partial Script Execution Techniques
DBeaver supports flexible partial script execution capabilities. Users can select specific segments within SQL scripts, such as individual query statements, and execute the selected content using the Ctrl+Enter keyboard shortcut. This approach is particularly useful for debugging complex scripts or when only specific code sections need execution.
Beyond keyboard shortcuts, users can also right-click selected SQL text and choose the Execute SQL Statement option from the context menu. This method provides a more intuitive interface, especially suitable for users unfamiliar with keyboard shortcuts.
Complete Script Execution Solutions
For scenarios requiring execution of entire SQL scripts, DBeaver offers multiple solutions. Using the Alt+X shortcut quickly executes all SQL statements in the current editor. DBeaver parses and sequentially executes all queries according to statement delimiters (semicolon by default).
An alternative method involves using the Execute SQL Script function available through the main toolbar or main menu under SQL Editor options. This approach suits situations requiring more precise control over the execution process.
Advanced Execution Functionality Analysis
DBeaver provides native script execution functionality activated through the Alt+N shortcut or corresponding menu options. This feature launches a setup wizard allowing users to configure script execution parameters in native clients like PLSQL, MySQL, or SQLPlus. Execution results display in console output format, particularly beneficial for functions unsupported by DBeaver drivers or scenarios requiring faster clients.
For situations requiring parallel query execution, users can employ the Ctrl+Alt+Shift+X shortcut or select the Execute Statements In Separate Tabs option. This functionality executes all queries in the script simultaneously, with each query running in separate threads and displaying outputs across multiple result tabs.
Execution Result Management
SQL query execution generates result sets presented through tab interfaces. Single queries may produce multiple result sets, each corresponding to a separate tab. These tabs maintain association with the queries that generated them.
Users can manage result tabs through various methods: close individual tabs using Ctrl+Shift+\, close all tabs except the current one via tab context menu, or close all result tabs from the same query. Tabs support drag-and-drop movement and pinning operations, with pinned tabs stacking on the left side and remaining immune to overwriting by new query results.
Parameter and Variable Configuration
DBeaver supports parameter usage within SQL queries as placeholders, prompting users for input values during execution. Parameter behavior configuration is accessible through Window->Preferences->Editors->SQL Editor->SQL Processing path.
Key configuration options include: enabling SQL parameters, anonymous SQL parameters, named parameter prefix (default :), control command prefix (default @), among others. Users can also define variables using the @set command, then insert variable values into SQL queries using ${varname} or :varname syntax.
Execution Configuration Optimization
Within script execution settings, users can configure commit types: commit at script end, commit after each query (autocommit), commit after every N queries, or no commit. Error handling options include: stop + rollback, stop + commit, or ignore errors and continue execution.
Other significant configurations include: fetching query results, maximizing editor during script execution, displaying statistical information, closing included script tabs after execution, etc. Delimiter settings allow custom statement delimiters and configuration of whether to treat blank lines as delimiters.
External File Execution Solutions
For large SQL files, recommended practice involves editing files in external editors, then loading and executing files in DBeaver by right-clicking database connections and selecting Tools/Execute script option. This method typically proves more efficient than editing large files internally within DBeaver.
When internal file handling within DBeaver is necessary, users must first set target databases in the active datasource selector, then load SQL files through File/Open file, and finally execute complete scripts using Alt+X.