To execute a SQL script, you run its .sql file through a database-specific tool—sqlcmd for SQL Server, the mysql client for MySQL, or SQL Workshop for Oracle.
A SQL script is just a text file with statements—but double-clicking it won’t do anything. You have to feed it to a database client that understands the dialect. The right tool changes with your engine, and using the wrong one is the most common reason a script fails. This guide covers the exact commands and menu paths for SQL Server, MySQL, MariaDB, and Oracle APEX, plus the safety checks that keep a script from damaging production data.
What Is A SQL Script?
A SQL script is a text file containing multiple SQL statements, procedural blocks, or client-specific commands. Oracle scripts may include PL/SQL and SQL*Plus commands; SQL Server scripts can contain sqlcmd directives and scripting variables. Scripts are typically used to create, alter, or drop database objects and to change data with INSERT, UPDATE, and DELETE. A good script runs from start to finish without manual intervention—as long as it’s executed in the right environment.
How Do I Execute A SQL Script In SQL Server?
SQL Server gives you two primary ways to run a script—the sqlcmd command-line utility and the SSMS graphical editor. The sqlcmd method is more automatable, while SSMS is better for debugging and reviewing the script first.
Using sqlcmd (command line):
- Open a Command Prompt window.
- Navigate to your script’s directory or use the full file path.
- Run the command:
sqlcmd -S myServer\instanceName -i C:\scripts\myScript.sql - To save the output to a file, add the
-oflag:sqlcmd -S myServer\instanceName -i C:\scripts\myScript.sql -o C:\EmpAdds.txt
Per Microsoft’s official sqlcmd documentation, the script file can include Transact-SQL statements, sqlcmd commands, and scripting variables—confirming that this tool handles more than raw SQL.
Using SQL Server Management Studio (SSMS):
- Open SSMS and connect to the target server.
- Go to File > Open > File and select your
.sqlfile. - Click Execute in the toolbar (or press F5).
SSMS does not execute scripts by file path in the same way sqlcmd does. If your script relies on batch separators like GO or scripting variables, enable SQLCMD Mode in the Query menu within SSMS, or use sqlcmd directly.
What About MySQL, MariaDB, And Oracle?
MySQL and MariaDB use a similar redirect pattern on the command line, while Oracle APEX provides a web-based script runner. Picking the right interface depends on whether you need automation or a visual review.
MySQL / MariaDB command line:
- Open a terminal or command prompt.
- Run the redirect command:
mysql -u [username] -p [database name] < [script.sql] - Enter your password when prompted.
If you’re already connected to the MySQL client, use the source command: source /path/to/script.sql. For GUI users, MySQL Workbench offers File > Run SQL Script, which prompts you for the file and connection details.
Oracle APEX (SQL Workshop):
- Log into your Oracle APEX workspace.
- Navigate to SQL Workshop > SQL Scripts.
- Click Create, give the script a name, and paste or type your statements.
- Click Save, then Run from the Script Editor.
Oracle scripts may include PL/SQL blocks and SQL*Plus commands, which are supported inside this interface but will fail if run through a MySQL client.
Common Execution Mistakes And How To Avoid Them
A script that works in one environment can fail silently in another. These three issues cause the most problems at the command line and in GUI tools.
| Database Engine | Tool / Interface | Core Execution Command / Action |
|---|---|---|
| SQL Server | sqlcmd |
sqlcmd -S server\instance -i script.sql -o output.txt |
| SQL Server | SSMS (GUI) | File > Open > File > Execute (F5) |
| MySQL / MariaDB | mysql CLI |
mysql -u user -p db < script.sql or source script.sql; |
| MySQL | MySQL Workbench | File > Run SQL Script |
| Oracle APEX | SQL Workshop | SQL Scripts > Create > Run |
| MariaDB | MariaDB CLI | mysql -u user -p db < script.sql (same client) |
| 4D | SQL EXECUTE SCRIPT |
Pass file path to SQL EXECUTE SCRIPT command |
| Creatio | IDE Configuration | Add SQL script element, set Code/DBMS type/Installation type |
Wrong target database. Connecting to the wrong database or forgetting a USE statement is the fastest way to create objects in the wrong place. Add USE [DatabaseName]; at the top of SQL Server scripts, or use the -d flag in sqlcmd. For MySQL, pass the database name directly in the command.
Vendor syntax mix-up. SQL Server’s GO batch separator will crash a MySQL client. Oracle’s PL/SQL blocks and EXECUTE syntax will not parse in SQL Server. Match the script’s dialect to the engine.
Insufficient permissions. The user account must have the relevant DDL or DML permissions. A script that runs perfectly on a developer’s local instance often throws permission errors in staging or production because the connected user lacks the rights to create tables or alter schemas.
| Symptom | Likely Cause | Check / Fix |
|---|---|---|
| "Syntax error" for another engine's syntax | Vendor-specific syntax (e.g., GO with MySQL) |
Confirm script dialect matches target database |
| Script runs but no tables/objects appear | No USE DATABASE or connected to wrong DB |
Add USE [DatabaseName]; at script start or -d flag |
| Permission denied | User lacks DDL or DML privileges | Review server/DB permissions for the connecting user |
| "File not found" error in CLI | Incorrect path or missing file extension | Use absolute path; check .sql extension |
| Stored procedures fail to compile | Missing DELIMITER (MySQL/MariaDB) |
Set DELIMITER // before CREATE PROCEDURE |
| Script runs forever / hangs | Uncommitted transaction or lock | Check SHOW PROCESSLIST or @@TRANCOUNT |
Final SQL Script Execution Checklist
Run through this checklist before pressing Enter or Execute – it takes two minutes and prevents the most common failures.
- Identify the target database engine (SQL Server, MySQL, MariaDB, Oracle).
- Choose the correct execution tool (CLI or GUI).
- Verify the script’s dialect matches the engine.
- Connect to the intended database and user account.
- Review the script for destructive
DROPorTRUNCATEcommands. - Execute the script using the engine-specific command or menu path.
- Check the output for errors and validate the results with a quick
SELECTorEXEC sp_help.
References & Sources
- Microsoft Learn. "Run Transact-SQL Script Files Using sqlcmd" Covers the official syntax and prerequisites for executing scripts in SQL Server.
