Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 52177b5

Browse files
author
MightyPen
committed
Issue 1501 fixed. FOR XML, CAST ... AS XMLDATA.
1 parent e99077e commit 52177b5

1 file changed

Lines changed: 49 additions & 31 deletions

File tree

docs/t-sql/queries/select-for-clause-transact-sql.md

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "FOR Clause (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "08/09/2017"
4+
ms.date: "01/08/2019"
55
ms.prod: sql
66
ms.prod_service: "database-engine, sql-database"
77
ms.reviewer: ""
@@ -24,22 +24,22 @@ ms.author: "douglasl"
2424
manager: craigg
2525
---
2626
# SELECT - FOR Clause (Transact-SQL)
27+
2728
[!INCLUDE[tsql-appliesto-ss2008-asdb-xxxx-xxx-md](../../includes/tsql-appliesto-ss2008-asdb-xxxx-xxx-md.md)]
2829

29-
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.
3031

3132
- Allow updates while viewing query results in a browse mode cursor by specifying **FOR BROWSE**.
3233

3334
- Format query results as XML by specifying **FOR XML**.
3435

3536
- Format query results as JSON by specifying **FOR JSON**.
36-
37-
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
37+
38+
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
3839

3940
## Syntax
4041

41-
```
42-
42+
```
4343
[ FOR { BROWSE | <XML> | <JSON>} ]
4444
4545
<XML> ::=
@@ -78,20 +78,21 @@ JSON
7878
[ , WITHOUT_ARRAY_WRAPPER ]
7979
]
8080
81-
}
82-
```
81+
}
82+
```
8383

84-
## FOR BROWSE
84+
## FOR BROWSE
85+
8586
BROWSE
8687
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)].
8788

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.
9091
9192
FOR BROWSE cannot appear in SELECT statements that are joined by the UNION operator.
9293

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.
9596
9697
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:
9798

@@ -149,8 +150,7 @@ JSON
149150
FROM tleft
150151
RIGHT JOIN tright
151152
ON tleft.c1 = tright.c1
152-
WHERE tright.c1 <> 2 ;
153-
153+
WHERE tright.c1 <> 2 ;
154154
```
155155

156156
Notice the following output in the Results pane:
@@ -165,14 +165,15 @@ JSON
165165

166166
After you run the SELECT query to access the tables in the browse mode, the result set of the SELECT query contains two null values 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 null values that came from the table and the null values that the right outer join statement introduced. You might receive incorrect results if you must ignore the null values from the result set.
167167

168-
> [!NOTE]
169-
> If the columns that are included in the unique index do not accept null values, all the null values in 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 null values, all the null values in the result set were introduced by the right outer join statement.
170170

171-
## FOR XML
171+
## FOR XML
172+
172173
XML
173174
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 &#40;SQL Server&#41;](../../relational-databases/xml/for-xml-sql-server.md).
174175

175-
RAW [ **('***ElementName***')** ]
176+
RAW [ **('**_ElementName_**')** ]
176177
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).
177178

178179
AUTO
@@ -184,10 +185,26 @@ JSON
184185
XMLDATA
185186
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.
186187

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_**')** ]
191208
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).
192209

193210
ELEMENTS
@@ -199,7 +216,7 @@ JSON
199216
ABSENT
200217
Indicates that for null column values, corresponding XML elements will not be added in the XML result. Specify this option only with ELEMENTS.
201218

202-
PATH [ **('***ElementName***')** ]
219+
PATH [ **('**_ElementName_**')** ]
203220
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).
204221

205222
BINARY BASE64
@@ -208,7 +225,7 @@ JSON
208225
TYPE
209226
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).
210227

211-
ROOT [ **('***RootName***')** ]
228+
ROOT [ **('**_RootName_**')** ]
212229
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.
213230

214231
For more info, see [FOR XML &#40;SQL Server&#41;](../../relational-databases/xml/for-xml-sql-server.md).
@@ -228,7 +245,8 @@ ORDER BY LastName, FirstName
228245
FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS XSINIL;
229246
```
230247

231-
## FOR JSON
248+
## FOR JSON
249+
232250
JSON
233251
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 or PATH. For more information about the **FOR JSON** clause, see [Format Query Results as JSON with FOR JSON &#40;SQL Server&#41;](../../relational-databases/json/format-query-results-as-json-with-for-json-sql-server.md).
234252

@@ -243,15 +261,15 @@ FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS XSINIL;
243261
INCLUDE_NULL_VALUES
244262
Include null values in 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 &#40;SQL Server&#41;](../../relational-databases/json/include-null-values-in-json-include-null-values-option.md).
245263
246-
ROOT [ **('***RootName***')** ]
264+
ROOT [ **('**_RootName_**')** ]
247265
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 &#40;SQL Server&#41;](../../relational-databases/json/add-a-root-node-to-json-output-with-the-root-option-sql-server.md).
248266
249267
WITHOUT_ARRAY_WRAPPER
250268
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 &#40;SQL Server&#41;](../../relational-databases/json/remove-square-brackets-from-json-without-array-wrapper-option.md).
251269

252270
For more info, see [Format Query Results as JSON with FOR JSON &#40;SQL Server&#41;](../../relational-databases/json/format-query-results-as-json-with-for-json-sql-server.md).
253271

254-
## See Also
255-
[SELECT &#40;Transact-SQL&#41;](../../t-sql/queries/select-transact-sql.md)
256-
257-
272+
## See Also
273+
274+
[SELECT &#40;Transact-SQL&#41;](../../t-sql/queries/select-transact-sql.md)
275+

0 commit comments

Comments
 (0)