Programming SQL Server 2005
7.2. Creating xml Data Type Instances
You can create instances of XML data by casting or converting from strings. You can even take advantage of implicit casting by simply using a string in place of an xml type value. The next two subsections go into more detail on these topics, and the third shows you how to bulk load XML data. A fourth way to create a value of xml type is to issue a SELECT statement with a FOR XML clause. You'll find that method discussed in detail in "XML Results Using the FOR XML Clause" later in this chapter. 7.2.1. Casting and Converting Strings
You can cast or convert (CONVERT function) any string data type instance[n][var]char, [n]text, varbinary, and imageto an xml data type instance. Untyped data is checked to ensure that it is well formed. Instances of typed XML data are validated against the associated schema. The XML parser discards insignificant whitespace when converting string data types to xml data types when either of the following is true:
You can override the default whitespace handling behavior by setting the optional style (third) parameter of the CONVERT function to 1. You cannot override the default whitespace handling when using the CAST function to cast a string data type instance to an xml data type instance. The following example inserts a row into the xmlTable table created in the "Creating xml Data Type Columns and Variables" section earlier in this chapter. The example specifies the style attribute (third argument) of the CONVERT function to preserve whitespace when converting a string to an xml data type instance. USE ProgrammingSqlServer2005 INSERT INTO xmlTable (ID, xmlCol) VALUES (2, CONVERT(xml, '<rootNode2> <childElement/> </rootNode2>', 1)) If the style argument is not specified or set to 0, the xml data type instance will be stripped of whitespace and stored as follows: <rootNode2><childElement/></rootNode2>
7.2.2. Constant Assignment
A string constant can be used where an xml data type instance is expected. An implicit cast to the xml data type is performed. If the xml data type is typed, the XML in the string is validated against the associated XML schema. The following example implicitly casts a string to an xml data type variable: DECLARE @xmlVar xml SET @xmlVar = '<rootNode><childElement/></rootNode>' SELECT @xmlVar
The xml data type instance is returned as shown in Figure 7-3. Figure 7-3. Results for xml constant assignment example
7.2.3. Bulk Loading Data with OPENROWSET
Enhanced OPENROWSET functionality in SQL Server 2005 lets you bulk load XML from files into xml data type columns. The syntax for the OPENROWSET statement for bulk loading data is as follows: OPENROWSET (BULK 'data_file', { FORMATFILE = 'format_file_path' [ <bulk_options> ] | SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB } } ) <bulk_options> ::= [ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' }] [ , ERRORFILE = 'file_name' ] [ , FIRSTROW = first_row ] [ , LASTROW = last_row ] [ , MAXERRORS = maximum_errors ] [ , ROWS_PER_BATCH = rows_per_batch ]
where:
The following example bulk loads data from the following tab-delimited file OpenRowSetData.txt. Make sure that the ID and ValueCol values are separated with a tab. 1 Value 1 2 Value 2 3 Value 3 4 Value 4
First create the table, OpenRowSetTable, that will be the destination for the bulk-loaded data by executing the following statement: USE ProgrammingSqlServer2005 CREATE TABLE OpenRowSetTable ( ID int, ValueCol varchar(50) )
Create a non-XML format file named OpenRowSetData.fmt that describes the data being bulk loaded: 9.0 2 1 SQLCHAR 0 12 "\t" 1 ID "" 2 SQLCHAR 0 50 "\r\n" 2 ValueCol SQL_Latin1_General_CP1_CI_AS
Executing the following OPENROWSET statement loads the data using the non-XML format file into the OpenRowSetTable table: INSERT INTO OpenRowSetTable(ID, ValueCol) SELECT rs.ID, rs.ValueCol FROM OPENROWSET( BULK N'C:\PSS2005\OpenRowSet\OpenRowSetData.txt', FORMATFILE = N'C:\PSS2005\OpenRowSet\OpenRowSetData.fmt') AS rs
Examining the OpenRowSetTable table reveals four rows, as shown in Figure 7-4. Next, you will bulk load the data from the text file to the OpenRowSetTable table using an XML format file. Create the following XML format file and name it OpenRowSetData.xml:
<?xml version="1.0"?> <BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/ format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <RECORD> <FIELD xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="12"/> <FIELD xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="50" COLLATION="SQL_Latin1_General_CP1_CI_AS"/> </RECORD> <ROW> <COLUMN SOURCE="1" NAME="ID" xsi:type="SQLINT"/> <COLUMN SOURCE="2" NAME="ValueCol" xsi:type="SQLVARYCHAR"/> </ROW> </BCPFORMAT>
Bulk load the data using the XML format file into the OpenRowSetTable table using the same statement as before but specifying the XML format file: INSERT INTO OpenRowSetTable(ID, ValueCol) SELECT rs.ID, rs.ValueCol FROM OPENROWSET( BULK N'C:\PSS2005\OpenRowSet\OpenRowSetData.txt', FORMATFILE = N'C:\PSS2005\OpenRowSet\OpenRowSetData.xml') AS rs
Figure 7-4. Results for bulk load example
The OpenRowSetTable now contains eight rows. |