Tải bản đầy đủ - 0 (trang)
7-14. Shaping XML Export Data

7-14. Shaping XML Export Data

Tải bản đầy đủ - 0trang

Chapter 7 ■ Exporting Data from SQL Server



This should produce the following output from the sample data (shortened for publishing purposes):



 3

 John Smith

 1





 4

 Bauhaus Motors

 2





Defining the Highest-Level Element Name

Adding the name of the element to use—instead of  —is done like this

(C:\SQL2012DIRecipes\CH07\TopLevelElementName.sql):

SELECT

C.ID

,C.ClientName

,C.Country

FROM dbo.Client C

FOR XML PATH('Client');

And it gives (again, much shortened):



 3

 John Smith

 1





 4

 Bauhaus Motors

 2





Adding a ROOT Element

To progress to better formed XML, you can add a root element as follows

(C:\SQL2012DIRecipes\CH07\RootElementXML.sql):

SELECT

C.ID

,C.ClientName

,C.Country

FROM dbo.Client C

FOR XML PATH('Client'), ROOT('CarSales');



380

www.it-ebooks.info



Chapter 7 ■ exporting Data from SQL Server



The result will be:





3

John Smith

1



(Records ommitted to save space)



7

Slow Sid

3







Defining Attributes

If you prefer attribute-centric XML, then you can shape it using aliases in this way

(C:\SQL2012DIRecipes\CH07\AttributeCentricXML.sql):

SELECT

C.ID '@ID'

,C.ClientName '@ClientName'

,C.Country '@Country'

FROM dbo.Client C

FOR XML PATH('Client'), ROOT('CarSales');

You should get back:












ClientName = "John Smith" Country = "1" />

ClientName = "Bauhaus Motors" Country = "2" />

ClientName = "Honest Fred" Country = "3" />

ClientName = "Fast Eddie" Country = "2" />

ClientName = "Slow Sid" Country = "3" />



Outputting More Complex Nested XML

Finally, to set you on the path to more complex output, you can create nested XML by aliasing fields using code

like the following (C:\SQL2012DIRecipes\CH07\ComplexNestedXML.sql):

SELECT

C.ID

,C.ClientName

,C.Country

,C.Town



381

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



,I.TotalDiscount AS 'Invoice/@TotalDiscount'

,I.InvoiceNumber AS 'Invoice/InvoiceNumber'

FROM dbo.Client C

INNER JOIN dbo.Invoice I

ON C.ID = I.ClientID

FOR XML PATH('Client'), ROOT('CarSales');

The output from this will be





 3

 John Smith

 1

 Uttoxeter



 3A9271EA-FC76-4281-A1ED-714060ADBA30







 4

 Bauhaus Motors

 2

 Oxford



 C9018CC1-AE67-483B-B1B7-CF404C296F0B







Obviously, the output shown is only a small part of the final result.



How It Works

In some of the preceding recipes, I used FOR XML PATH to “shape” the XML that is output. Now, without any

attempt at being exhaustive, here is a crash-course on using FOR XML PATH to deliver the XML that you want

to export.

The main keyword to note is AUTO, which is used to create XML hierarchies using the source tables to define

the XML hierarchy. You need to remember that the order of the columns in the SELECT clause is important

because this will influence the nesting of XML elements.

Hopefully, this shows that





You define nested elements by aliasing them with the full path through the XML Node

tree from the top-level element.







You create attributes rather than elements by prefixing the attribute name with an at

symbol (@), and use the full path through the XML Node tree from the top-level element.







You must define attributes for a node before defining any lower-level elements.



You will need to watch out for typos when defining complex, multilevel XML schemas. It is all too easy to create

parallel levels. Also remember that the schema is casesensitive. While we are discussing this, it is worth noting that



382

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server







It is vital in the SELECT clause to choose fields from the Client table before fields from

the Invoice table to obtain the hierarchy that was produced in the XML, where invoice

elements were nested inside Client data.







Aliasing table names provide element names.







Aliasing column names provide element names.



The options shown in Table 7-8 can be used when outputting XML.

Table 7-8.  FOR XML PATH Options



Option



Description



TYPE



Outputs the result as SQL Server’s XML datatype, and not as a NVARCHAR(MAX).



ELEMENTS



Ensures that XML elements, and not attributes (the default), are used.



XSINIL



Forces empty elements to be output, rather than skipped entirely.



ROOT



Adds a root element of your choice.



There is also the somewhat feared FOR XML EXPLICIT command, which you can use when the XML you

need to output cannot be created using PATH or AUTO XML Explicit is outside the scope of these recipes. I leave it to

you to research that option from other sources, such as Books Online and the Internet.



7-15. Exporting XML Data on a Regular Basis

Problem

You want to export data as XML as part of a structured ETL package.



Solution

Use SSIS to export XML data. I’ll explain one way to do this.

1.



Create a new SSIS package and add three connection managers, configured as

follows:



Name



Connection



Comments



CarSales_OLEDB



OLEDB



Connect to the source database (CarSales here). Set the

property RetainSameConnection to True.



CarSales_ADONET



ADO.NET



Connect to the source database (CarSales here).



XMLOut



flat file



The file must be Unicode and have one destination column

(added using the Advancedpane) named XMLOut.

This column must be a Unicode text stream.



383

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



2.



3.



Add the following five SSIS package-scoped variables. Their usage will become

apparent as the package is created:



Name



DataType



Value



Description



ChunkRecordNo



Int



350000



The number of records to be

processed for each SSIS buffer.



Chunks



Int



30



The number of chunks that

will be processed.



CurrentChunk



Int



30



The current chunk to process.



LoadProcedure



String



dbo.pr_GetChunkedXML



The stored procedure that

creates the XML, chunk by

chunk.



OutputFile



String



C:\SQL2012DIRecipes\ 

CH07\Massive.xml



The destination file.



SQLProc



String



EXECUTE dbo.pr_GetChunkedXML The stored procedure to run.



Create the following SQL stored procedure in the source database. It will be used to

create the XML, chunk by chunk (C:\SQL2012DIRecipes\CH07\pr_GetChunkedXML.sql):

CREATE PROCEDURE CarSales.dbo.pr_GetChunkedXML

(

@ChunkID INT

)

AS

SELECT A.XmlOut

FROM

(

SELECT

S.ID

,InvoiceID

,StockID

,SalePrice

,DateUpdated

FROM dbo.Invoice_Lines S

INNER JOIN ##Tmp_ChunkDef Tmp -- This table is defined in step 6

ON S.ID = Tmp.ID

WHERE Tmp.ChunkID = @ChunkID

FOR XML PATH ('Invoice_Lines'), TYPE

) A (XmlOut);



4.



Add an initial Execute SQL task. Name it Get Number of Chunk Iterations.

Double-click to edit and configure as follows:



384

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-14. Shaping XML Export Data

Tải bản đầy đủ ngay(0 tr)

×