You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 80b93ac
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/tools/sqlcmd/edit-sqlcmd-scripts-with-query-editor.md
+20-21Lines changed: 20 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: "Edit SQLCMD Scripts with Query Editor"
2
+
title: Sqlcmd utility - Edit SQLCMD scripts with Query Editor
3
3
description: You use SQLCMD scripts when you have to process Windows System commands and Transact-SQL statements in the same script. Learn how to write and edit SQLCMD scripts using the Database Engine Query Editor.
4
4
author: grrlgeek
5
5
ms.author: jeschult
@@ -17,16 +17,15 @@ helpviewer_keywords:
17
17
- "scripts [SQL Server], SQL Server Management Studio"
[!INCLUDE[SQL Server Azure SQL Database Synapse Analytics PDW](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
24
23
25
-
By using the Database Engine Query Editor in SQL Server Management Studio you can write and edit queries as SQLCMD scripts. You use SQLCMD scripts when you have to process Windows System commands and Transact\-SQL statements in the same script.
24
+
Using the Database Engine Query Editor in SQL Server Management Studio, you can write and edit queries as SQLCMD scripts. You use SQLCMD scripts when processing Windows System commands and Transact\-SQL statements in the same script.
26
25
27
26
## SQLCMD Mode
28
27
29
-
To use the Database Engine Query Editor to write or edit SQLCMD scripts, you must enable the SQLCMD scripting mode. By default, SQLCMD mode is not enabled in the Query Editor. You can enable scripting mode by selecting the **SQLCMD Mode** icon in the toolbar or by selecting **SQLCMD Mode** from the **Query** menu.
28
+
To use the Database Engine Query Editor to write or edit SQLCMD scripts, you must enable the SQLCMD scripting mode. By default, SQLCMD mode isn't allowed in the Query Editor. You can enable scripting mode by selecting the **SQLCMD Mode** icon in the toolbar or by selecting **SQLCMD Mode** from the **Query** menu.
30
29
31
30
> [!NOTE]
32
31
> Enabling SQLCMD mode turns off IntelliSense and the Transact\-SQL debugger in the Database Engine Query Editor.
@@ -49,7 +48,7 @@ SQLCMD scripts in the Query Editor can use the same features that all Transact\-
49
48
50
49
#### Switch a database engine query editor window to sqlcmd mode
51
50
52
-
1. In Object Explorer, right-click the server, and then select **New Query**, to open a new Database Engine Query Editor window.
51
+
1. In Object Explorer, right-click the server and then select **New Query**, to open a new Database Engine Query Editor window.
53
52
54
53
1. On the **Query** menu, select **SQLCMD Mode**.
55
54
@@ -68,7 +67,7 @@ SQLCMD scripts in the Query Editor can use the same features that all Transact\-
68
67
GO
69
68
```
70
69
71
-
1. Press F5 to execute the whole section of mixed Transact\-SQL and MS-DOS statements.
70
+
1. Press F5 to execute the mixed Transact\-SQL and MS-DOS statements section.
72
71
73
72
Notice the two SQL result panes from the first and third statements.
74
73
@@ -83,36 +82,36 @@ SQLCMD scripts in the Query Editor can use the same features that all Transact\-
83
82
> [!IMPORTANT]
84
83
> When executed from the command line, the **sqlcmd** utility permits full interaction with the operating system. When you use the Query Editor in**SQLCMD Mode**, you must be careful not to execute interactive statements. The Query Editor cannot respond to operating system prompts.
85
84
86
-
For more information about how to run SQLCMD, see [sqlcmd Utility](../../tools/sqlcmd-utility.md), or take the SQLCMD tutorial.
85
+
For more information about how to run SQLCMD, see [Sqlcmd utility](../../tools/sqlcmd-utility.md), or take the SQLCMD tutorial.
87
86
88
87
## Enable SQLCMD Scripting by Default
89
88
90
-
To turn SQLCMD scripting onby default, on the **Tools** menu select**Options**, expand **Query Execution**, and**SQL Server**, select the **General** page,and then check the **By default open new queries in SQLCMD Mode**box.
89
+
To turn onSQLCMD scripting by default, on the **Tools** menu,select**Options**, expand **Query Execution**, and**SQL Server**, select the **General** page and then check the **By default open new queries in SQLCMD Mode**box.
91
90
92
91
## Write and Editing SQLCMD Scripts
93
92
94
-
After enabling scripting mode you may write SQLCMD commands and Transact\-SQL statements. The following rules apply:
93
+
After enabling scripting mode, you may write SQLCMD commands and Transact\-SQL statements. The following rules apply:
95
94
96
95
- SQLCMD commands must be the first statement on a line.
97
96
98
97
- Only one SQLCMD command is permitted on each line.
99
98
100
99
- SQLCMD commands can be preceded by comments or white space.
101
100
102
-
- SQLCMD commands within comment characters are not executed.
101
+
- SQLCMD commands within comment characters aren't executed.
103
102
104
-
- Singleline comment characters are two hyphens (`--)`and must appear at the beginning of a line.
103
+
- Single-line comment characters are two hyphens (`--)` and must appear at the beginning of a line.
105
104
106
105
- Operating system commands must be preceded by two exclamation points (`!!`). The double-exclamation points command causes the statement that follows the exclamation points to be executed using the `cmd.exe` command processor. The text after `!!` is passed in as a parameter to `cmd.exe`, so the final command line will execute as: `"%SystemRoot%\system32\cmd.exe /c <text after !!>"`.
107
106
108
-
- To make a clear distinction between SQLCMD commands and Transact\-SQL, all SQLCMD commands, need to be prefixed with a colon (`:`).
107
+
- To make a clear distinction between SQLCMD commands and Transact\-SQL, all SQLCMD commands need to be prefixed with a colon (`:`).
109
108
110
-
- The `GO` command may be used without preface,or preceded by `!!:`
109
+
- The `GO` command may be used without preface or preceded by `!!:`
111
110
112
-
- The Database Engine Query Editor supports environment variables and variables that are defined as part of a SQLCMD script, but does not support built-in SQLCMD or**osql** variables. SQLCMD processing by SQL Server Management Studio is case sensitive for variables. For example, PRINT '$(COMPUTERNAME)' produces the correct result, but PRINT '$(ComputerName)' returns an error.
111
+
- The Database Engine Query Editor supports environment variables and variables that are defined as part of a SQLCMD script, but doesn't support built-in SQLCMD or**osql** variables. SQLCMD processing by SQL Server Management Studio is case sensitive for variables. For example, PRINT '$(COMPUTERNAME)' produces the correct result, but PRINT '$(ComputerName)' returns an error.
113
112
114
113
> [!CAUTION]
115
-
> SQL Server Management Studio uses [!INCLUDE[msCoName](../../includes/msconame-md.md)] [!INCLUDE[dnprdnshort](../../includes/dnprdnshort-md.md)]SqlClient for execution in regular and SQLCMD mode. When run from the command line, SQLCMD uses the OLE DB provider. Because different default options may apply, it is possible to get different behavior while executing the same query in SQL Server Management Studio SQLCMD Mode,andin the SQLCMD utility.
114
+
> SQL Server Management Studio uses [!INCLUDE[msCoName](../../includes/msconame-md.md)] [!INCLUDE[dnprdnshort](../../includes/dnprdnshort-md.md)]SqlClient for execution in regular and SQLCMD mode. When run from the command line, SQLCMD uses the OLE DB provider. Because different default options may apply, it is possible to get different behavior while executing the same query in SQL Server Management Studio SQLCMD Mode andin the SQLCMD utility.
116
115
117
116
## Supported SQLCMD Syntax
118
117
@@ -139,20 +138,20 @@ SQLCMD scripts in the Query Editor can use the same features that all Transact\-
139
138
`:out <filename>|stderr|stdout`
140
139
141
140
> [!NOTE]
142
-
>For both `:error`and`:out`, `stderr`and`stdout` send output to the messages tab.
141
+
>Send output to the messages tab for both `:error`and`:out`, `stderr`and`stdout`.
143
142
144
-
SQLCMD commands not listed above are not supported in Query Editor. When a script containing SQLCMD keywords that are not supported is executed, the Query Editor will send an "Ignoring command *\<ignored command*>" message to the destination for each unsupported keyword. The script will execute successfully, but the unsupported commands will be ignored.
143
+
SQLCMD commands not listed above aren't supported in Query Editor. When a script containing SQLCMD keywords aren't supported is executed, the Query Editor will send an "Ignoring command *\<ignored command*>" message to the destination for each unsupported keyword. The script will execute successfully, but the unsupported commands will be ignored.
145
144
146
145
> [!CAUTION]
147
-
> Because you are not starting SQLCMD from the command line, there are some limitations when running Query Editor in SQLCMD Mode. You cannot pass in command-line parameters such as variables, and, because the Query Editor does not have the ability to respond to operating system prompts, you must be careful not to execute interactive statements.
146
+
> Because you are not starting SQLCMD from the command line, there are some limitations when running Query Editor in SQLCMD Mode. You cannot pass in command-line parameters such as variables, and, because the Query Editor cannot respond to operating system prompts, you must be careful not to execute interactive statements.
148
147
149
148
## Color Coding in SQLCMD Scripts
150
149
151
150
With SQLCMD scripting enabled, scripts will be color coded. The color coding for Transact\-SQL keywords will remain the same. SQLCMD commands are presented with a shaded background.
152
151
153
152
## Example
154
153
155
-
The following example uses a **sqlcmd** statement to create an output file called testoutput.txt, executes two Transact\-SQL SELECT statements along with one operating system command (to print out the current directory). The resultant file contains the message output from the `DIR` statement, followed by the results output from the Transact\-SQL statements.
154
+
The following example uses a **sqlcmd** statement to create an output file called testoutput.txt, executes two Transact\-SQL SELECT statements along with one operating system command (to print the current directory). The resultant file contains the message output from the `DIR` statementand the results output from the Transact\-SQL statements.
156
155
157
156
```bash
158
157
:out C:\testoutput.txt
@@ -163,6 +162,6 @@ SELECT @@SERVERNAME AS 'Server Name'
0 commit comments