Execute SQL File
DBCode allows you to run SQL script files directly against your database, 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
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
Step 1: Locate Your Target Database
- Open the Database Explorer view in the sidebar
- Connect to your database server if not already connected
- Expand the server connection to show available databases
- Locate the specific database you want to execute the SQL file against
Step 2: Execute the SQL File
- Right-click on the target database
- Select Execute SQL File from the context menu
- In the file picker dialog that appears, browse to and select your
.sql
file - Click Open to confirm your selection
Step 3: View Results
- DBCode will execute the SQL file against the selected database
- The Results panel will open automatically to display the output
- Each statement’s results will be shown sequentially
- Any errors encountered during execution will be displayed
Best Practices
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
- 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
- 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
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
-- 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
-- Example cleanup scriptDELETE FROM audit_logs WHERE created_at < DATEADD(month, -6, CURRENT_DATE);VACUUM audit_logs;
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
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.