Cari Blog Ini

22 Maret 2012

Running Transact-SQL Script Files by Using sqlcmd With Output To File

To create a simple Transact-SQL script file by using Notepad,
follow these steps:

1. Click Start, point to All Programs, point to Accessories, and then click Notepad.
2. Copy and paste the following Transact-SQL code into Notepad:

USE AdventureWorks;
GO
SELECT c.FirstName + ' ' + c.LastName AS 'Employee Name',
a.AddressLine1, a.AddressLine2 , a.City, a.PostalCode
FROM Person.Contact AS c
   INNER JOIN HumanResources.Employee AS e
        ON c.ContactID = e.ContactID
    INNER JOIN HumanResources.EmployeeAddress ea
        ON ea.EmployeeID = e.EmployeeID
    INNER JOIN Person.Address AS a
        ON a.AddressID = ea.AddressID;
GO

3. Save the file as myScript.sql in the C drive.







To run the script file
1. Open a command prompt window.
2. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql
3. Press ENTER.


To save this output to a text file
1. Open a command prompt window.
2. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
3. Press ENTER.
Note: 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.


Source:
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_5techref/html/90067eb8-ca3e-44e8-bb1a-bf7d1a359423.htm