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
Use the FOR clause to specify one of the following options for query results.
30
+
Use the FOR clause to specify one of the following options for query results.
30
31
31
32
- Allow updates while viewing query results in a browse mode cursor by specifying **FOR BROWSE**.
32
33
33
34
- Format query results as XML by specifying **FOR XML**.
34
35
35
36
- Format query results as JSON by specifying **FOR JSON**.
36
-
37
-
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
37
+
38
+
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
38
39
39
40
## Syntax
40
41
41
-
```
42
-
42
+
```
43
43
[ FOR { BROWSE | <XML> | <JSON>} ]
44
44
45
45
<XML> ::=
@@ -78,20 +78,21 @@ JSON
78
78
[ , WITHOUT_ARRAY_WRAPPER ]
79
79
]
80
80
81
-
}
82
-
```
81
+
}
82
+
```
83
83
84
-
## FOR BROWSE
84
+
## FOR BROWSE
85
+
85
86
BROWSE
86
87
Specifies that updates be allowed while viewing the data in a DB-Library browse mode cursor. A table can be browsed in an application if the table includes a **timestamp** column, the table has a unique index, and the FOR BROWSE option is at the end of the SELECT statements sent to an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
87
88
88
-
> [!NOTE]
89
-
> You cannot use the \<lock_hint> HOLDLOCK in a SELECT statement that includes the FOR BROWSE option.
89
+
> [!NOTE]
90
+
> You cannot use the \<lock_hint> HOLDLOCK in a SELECT statement that includes the FOR BROWSE option.
90
91
91
92
FOR BROWSE cannot appear in SELECT statements that are joined by the UNION operator.
92
93
93
-
> [!NOTE]
94
-
> When the unique index key columns of a table are nullable, and the table is on the inner side of an outer join, the index is not supported by browse mode.
94
+
> [!NOTE]
95
+
> When the unique index key columns of a table are nullable, and the table is on the inner side of an outer join, the index is not supported by browse mode.
95
96
96
97
The browse mode lets you scan the rows in your [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] table and update the data in your table one row at a time. To access a [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] table in your application in the browse mode, you must use one of the following two options:
97
98
@@ -149,8 +150,7 @@ JSON
149
150
FROM tleft
150
151
RIGHT JOIN tright
151
152
ON tleft.c1 = tright.c1
152
-
WHERE tright.c1 <> 2 ;
153
-
153
+
WHERE tright.c1 <> 2 ;
154
154
```
155
155
156
156
Notice the following output in the Results pane:
@@ -165,14 +165,15 @@ JSON
165
165
166
166
After you run the SELECT query to access the tables in the browse mode, the result set of the SELECT query contains two nullvalues for the c1 column in the tleft table because of the definition of the right outer join statement. Therefore, in the result set, you cannot distinguish between the nullvalues that came from the table and the nullvalues that the right outer join statement introduced. You might receive incorrect results if you must ignore the nullvaluesfrom the result set.
167
167
168
-
> [!NOTE]
169
-
>If the columns that are included in the unique index do not accept nullvalues, all the nullvaluesin the result set were introduced by the right outer join statement.
168
+
> [!NOTE]
169
+
> If the columns that are included in the unique index do not accept nullvalues, all the nullvaluesin the result set were introduced by the right outer join statement.
170
170
171
-
## FOR XML
171
+
## FOR XML
172
+
172
173
XML
173
174
Specifies that the results of a query are to be returned as an XML document. One of the following XML modes must be specified: RAW, AUTO, EXPLICIT. For more information about XML data and [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)], see [FOR XML (SQL Server)](../../relational-databases/xml/for-xml-sql-server.md).
174
175
175
-
RAW [ **('***ElementName***')** ]
176
+
RAW [ **('**_ElementName_**')** ]
176
177
Takes the query result and transforms each row in the result set into an XML element with a generic identifier \<row />as the element tag. You can optionally specify a name for the row element. The resulting XML output uses the specified *ElementName*as the row element generated for each row. For more information, see [Use RAW Mode with FOR XML](../../relational-databases/xml/use-raw-mode-with-for-xml.md).
177
178
178
179
AUTO
@@ -184,10 +185,26 @@ JSON
184
185
XMLDATA
185
186
Returns inline XDR schema, but does not add the root element to the result. If XMLDATA is specified, XDR schema is appended to the document.
186
187
187
-
> [!IMPORTANT]
188
-
> The XMLDATA directive is deprecated. Use XSD generation in the case of RAW and AUTO modes. There is no replacement for the XMLDATA directive in EXPLICIT mode. [!INCLUDE[ssNoteDepFutureAvoid](../../includes/ssnotedepfutureavoid-md.md)]
189
-
190
-
XMLSCHEMA [ **('***TargetNameSpaceURI***')** ]
188
+
> [!IMPORTANT]
189
+
> The XMLDATA directive is **deprecated**. Use XSD generation in the case of RAW and AUTO modes. There is no replacement for the XMLDATA directive in EXPLICIT mode. [!INCLUDE[ssNoteDepFutureAvoid](../../includes/ssnotedepfutureavoid-md.md)]
190
+
191
+
You might use SQL Server Management Studio (SSMS) to issue a query that uses the FOR XML clause. Sometimes a large amount of XML is returned and displayed in one grid cell. The XML string could be longer than one SSMS grid cell can hold on a single line. In these cases, SSMS might insert line break characters between long segments of the whole XML string. Such line breaks might occur in the middle of a substring that should not be split across lines. You can prevent the line breaks by using a cast AS XMLDATA. This solution can also apply when you use FOR JSON PATH. The technique is discussed on Stack Overflow, and is shown in the following Transact-SQL sample SELECT statement:
192
+
193
+
- [Using SQL Server FOR XML: Convert Result Datatype to Text/varchar/string whatever?](https://stackoverflow.com/questions/5655332/using-sql-server-for-xml-convert-result-datatype-to-text-varchar-string-whate/5658758#5658758)
194
+
195
+
```sql
196
+
SELECT CAST(
197
+
(SELECT column1, column2
198
+
FROM my_table
199
+
FOR XML PATH('')
200
+
)
201
+
AS VARCHAR(MAX)
202
+
) AS XMLDATA ;
203
+
```
204
+
205
+
<!-- The preceding Stack Overflow example is per MicrosoftDocs/sql-docs Issue 1501. 2019-01-06 -->
206
+
207
+
XMLSCHEMA [ **('**_TargetNameSpaceURI_**')** ]
191
208
Returns inline XSD schema. You can optionally specify a target namespace URI when you specify this directive, which returns the specified namespace in the schema. For more information, see [Generate an Inline XSD Schema](../../relational-databases/xml/generate-an-inline-xsd-schema.md).
192
209
193
210
ELEMENTS
@@ -199,7 +216,7 @@ JSON
199
216
ABSENT
200
217
Indicates that for null column values, corresponding XML elements will not be added in the XML result. Specify this option only with ELEMENTS.
201
218
202
-
PATH [ **('***ElementName***')** ]
219
+
PATH [ **('**_ElementName_**')** ]
203
220
Generates a \<row> element wrapper for each row in the result set. You can optionally specify an element name for the \<row> element wrapper. If an empty string is provided, such as FOR XML PATH (**''**) ), a wrapper element is not generated. Using PATH may provide a simpler alternative to queries written using the EXPLICIT directive. For more information, see [Use PATH Mode with FOR XML](../../relational-databases/xml/use-path-mode-with-for-xml.md).
204
221
205
222
BINARY BASE64
@@ -208,7 +225,7 @@ JSON
208
225
TYPE
209
226
Specifies that the query returns results as**xml** type. For more information, see [TYPE Directive in FOR XML Queries](../../relational-databases/xml/type-directive-in-for-xml-queries.md).
210
227
211
-
ROOT [ **('***RootName***')** ]
228
+
ROOT [ **('**_RootName_**')** ]
212
229
Specifies that a single top-level element be added to the resulting XML. You can optionally specify the root element name to generate. If the optional root name is not specified, the default \<root> element is added.
213
230
214
231
For more info, see [FOR XML (SQL Server)](../../relational-databases/xml/for-xml-sql-server.md).
@@ -228,7 +245,8 @@ ORDER BY LastName, FirstName
228
245
FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS XSINIL;
229
246
```
230
247
231
-
## FOR JSON
248
+
## FOR JSON
249
+
232
250
JSON
233
251
Specify FOR JSON to return the results of a query formatted as JSON text. You also have to specify one of the following JSON modes : AUTO orPATH. For more information about the **FOR JSON** clause, see [Format Query Results as JSON with FOR JSON (SQL Server)](../../relational-databases/json/format-query-results-as-json-with-for-json-sql-server.md).
234
252
@@ -243,15 +261,15 @@ FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS XSINIL;
243
261
INCLUDE_NULL_VALUES
244
262
Include nullvaluesin the JSON output by specifying the **INCLUDE_NULL_VALUES** option with the **FOR JSON** clause. If you don't specify this option, the output does not include JSON properties for null values in the query results. For more info and examples, see [Include Null Values in JSON Output with the INCLUDE_NULL_VALUES Option (SQL Server)](../../relational-databases/json/include-null-values-in-json-include-null-values-option.md).
245
263
246
-
ROOT [ **('***RootName***')** ]
264
+
ROOT [ **('**_RootName_**')** ]
247
265
Add a single, top-level element to the JSON output by specifying the **ROOT** option with the **FOR JSON** clause. If you don't specify the **ROOT** option, the JSON output doesn't have a root element. For more info and examples, see [Add a Root Node to JSON Output with the ROOT Option (SQL Server)](../../relational-databases/json/add-a-root-node-to-json-output-with-the-root-option-sql-server.md).
248
266
249
267
WITHOUT_ARRAY_WRAPPER
250
268
Remove the square brackets that surround the JSON output by default by specifying the **WITHOUT_ARRAY_WRAPPER** option with the **FOR JSON** clause. If you don't specify this option, the JSON output is enclosed within square brackets. Use the **WITHOUT_ARRAY_WRAPPER** option to generate a single JSON object as output. For more info, see [Remove Square Brackets from JSON Output with the WITHOUT_ARRAY_WRAPPER Option (SQL Server)](../../relational-databases/json/remove-square-brackets-from-json-without-array-wrapper-option.md).
251
269
252
270
For more info, see [Format Query Results as JSON with FOR JSON (SQL Server)](../../relational-databases/json/format-query-results-as-json-with-for-json-sql-server.md).
0 commit comments