Execute SQL File
DBCode allows you to run SQL script files against a database or supported server connection, making it easy to execute multiple statements, database migrations, or setup scripts without having to copy and paste the content into the SQL editor.
Overview
Section titled “Overview”The Execute SQL File feature provides a convenient way to:
- Run database migration scripts
- Execute setup or seed scripts
- Apply database schema changes
- Import data using SQL statements
- Run complex multi-statement scripts
Using Execute SQL File
Section titled “Using Execute SQL File”Execute Against a Database
Section titled “Execute Against a Database”- Open the Database Explorer view in the sidebar
- Connect to and expand your server connection
- Right-click the target database
- Select Execute SQL File
- Browse to and select your
.sqlfile
DBCode executes the file against the selected database.
Execute From a Connection
Section titled “Execute From a Connection”If Execute SQL File appears when you right-click a connection, you can run a bootstrap script without first selecting a database:
- Open the Database Explorer view in the sidebar
- Right-click the server connection
- Select Execute SQL File
- Browse to and select your
.sqlfile
DBCode runs the file through the selected connection. If the saved connection specifies a database, the script starts there. Otherwise, the driver or server determines the initial database for the login; on some systems, no database is selected.
Important: DBCode never removes a saved database setting or retries against another database. If the saved database does not exist, the connection fails before the script starts. For bootstrap scripts, leave the database unset or configure an existing database from which the script can create the new one.
View Results
Section titled “View Results”After execution begins, the Results panel opens automatically. It shows statement results in order and reports parsing or execution errors.
Best Practices
Section titled “Best Practices”SQL File Structure
Section titled “SQL File Structure”- Include proper statement terminators (semicolons) between statements
- Consider adding comments to document complex operations
- For large operations, include transaction boundaries (
BEGIN TRANSACTION/COMMIT) - Use consistent formatting for better readability
Error Handling
Section titled “Error Handling”- Check the Results panel for any errors after execution
- Consider executing critical scripts in transactions for rollback capability
- For complex scripts, test on development environments before production
Performance Considerations
Section titled “Performance Considerations”- Large SQL files may take time to execute
- Monitor the Results panel for progress
- Consider breaking very large scripts into smaller, logical files
Common Use Cases
Section titled “Common Use Cases”Database Migration
Section titled “Database Migration”-- Example migration scriptCREATE TABLE customers ( id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) UNIQUE);
CREATE INDEX idx_customer_email ON customers(email);
INSERT INTO customers VALUES (1, 'Acme Corp', 'contact@acme.com');Schema Updates
Section titled “Schema Updates”-- Example schema update scriptALTER TABLE products ADD COLUMN last_updated TIMESTAMP;UPDATE products SET last_updated = CURRENT_TIMESTAMP;CREATE INDEX idx_last_updated ON products(last_updated);Data Cleanup
Section titled “Data Cleanup”-- Example cleanup scriptDELETE FROM audit_logs WHERE created_at < DATEADD(month, -6, CURRENT_DATE);VACUUM audit_logs;Limitations
Section titled “Limitations”- Very large files may take time to process
- The Results panel will show output for all statements, which can be extensive for large scripts
- Some database-specific features might have varying behavior
Alternatives
Section titled “Alternatives”If you prefer an interactive approach for complex scripts, consider:
- Using DBCode’s SQL Editor for step-by-step execution
- Breaking down large scripts into DBCode Notebooks with documentation
- Using database-specific command-line tools for certain operations
With Execute SQL File, you can quickly run existing SQL scripts directly from your filesystem without having to manually transfer their content to the SQL editor.