| title | Run Transact-SQL Script Files Using sqlcmd | Microsoft Docs | |
|---|---|---|
| ms.custom | ||
| ms.date | 07/15/2016 | |
| ms.prod | sql-non-specified | |
| ms.prod_service | sql-tools | |
| ms.service | ||
| ms.component | ssms-scripting | |
| ms.reviewer | ||
| ms.suite | sql | |
| ms.technology |
|
|
| ms.tgt_pltfrm | ||
| ms.topic | article | |
| helpviewer_keywords |
|
|
| ms.assetid | 90067eb8-ca3e-44e8-bb1a-bf7d1a359423 | |
| caps.latest.revision | 42 | |
| author | JennieHubbard | |
| ms.author | jhubbard | |
| manager | jhubbard | |
| ms.workload | Active |
[!INCLUDEappliesto-ss-asdb-asdw-pdw-md] Use sqlcmd to run a Transact-SQL script file. A Transact-SQL script file is a text file that can contain a combination of Transact-SQL statements, sqlcmd commands, and scripting variables.
To create a simple Transact-SQL script file by using Notepad, follow these steps:
-
Click Start, point to All Programs, point to Accessories, and then click Notepad.
-
Copy and paste the following Transact-SQL code into Notepad:
USE AdventureWorks2012; GO SELECT p.FirstName + ' ' + p.LastName AS 'Employee Name', a.AddressLine1, a.AddressLine2 , a.City, a.PostalCode FROM Person.Person AS p INNER JOIN HumanResources.Employee AS e ON p.BusinessEntityID = e.BusinessEntityID INNER JOIN Person.BusinessEntityAddress bea ON bea.BusinessEntityID = e.BusinessEntityID INNER JOIN Person.Address AS a ON a.AddressID = bea.AddressID; GO -
Save the file as myScript.sql in the C drive.
-
Open a command prompt window.
-
In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql
-
Press ENTER.
A list of [!INCLUDEssSampleDBCoShort] employee names and addresses is written to the command prompt window.
-
Open a command prompt window.
-
In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
-
Press ENTER.
No output is returned in the Command Prompt window. Instead, the output is sent to the EmpAdds.txt file. You can verify this output by opening the EmpAdds.txt file.