The sqlcmd utility replaces the osql utility. It allows you to enter Transact-SQL statements, system procedures, and script files at the command prompt. This versatile utility uses OLE DB to execute Transact-SQL batches. The Query Editor can be switched to SQLCMD mode, allowing you to execute sqlcmd statements from within SQL Server Management Studio.
To switch a Query Editor window to SQLCMD mode
-
In Object Explorer, right-click your server, and then click New Query, to open a new Query Editor window.
-
On the Query menu, click SQLCMD Mode.
The Query Editor executes sqlcmd statements in the context of the Query Editor.
-
On the SQL Editor toolbar, in the Available Databases list, select AdventureWorks.
-
In the Query Editor window, type the following two Transact-SQL statements and the !!DIR sqlcmd statement:
SELECT DISTINCT Type FROM Sales.SpecialOffer;
GO
!!DIR
GO
SELECT ProductCategoryID, Name
FROM Production.ProductCategory;
GO
-
Press F5 to execute the entire section of mixed Transact-SQL and MS-DOS statements.
Note the two SQL result panes from the first and third statements.
-
In the Results pane, click the Messages tab to see the messages from all three statements:
-
(6 row(s) affected)
-
<The directory information>
-
(4 row(s) affected)
Important: |
|---|
|
When executed from the command line, the sqlcmd utility permits full interaction with the operating system. When using the Query Editor in SQLCMD Mode, you must be careful not to execute interactive statements. The Query Editor does not have the ability to respond to operating system prompts. |
For more information about running SQLCMD, see sqlcmd Utility, or take the SQLCMD tutorial.
Summary: Writing Transact-SQL