Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
SQL database in Microsoft Fabric
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.
Assuming you've installed the AdventureWorks2022 sample database, follow the example in this section to create a script file that you can run from the command prompt with sqlcmd.
To create a Transact-SQL script file by using Notepad, follow these steps:
Select Start, point to All Programs, point to Accessories, and then select Notepad.
Copy and paste the following Transact-SQL code into Notepad::
USE AdventureWorks2022;
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 a folder, such as C:\scripts
drive.
Open a command prompt window.
In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\scripts\myScript.sql
Press ENTER.
A list of Adventure Works 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:\scripts\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.
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today