Tải bản đầy đủ - 0 (trang)
7-9. Exporting and Compressing Multiple Tables

7-9. Exporting and Compressing Multiple Tables

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

Chapter 7 ■ Exporting Data from SQL Server



Figure 7-10.  Setting an object variable for a Recordset destination

9.



Click OK, and then click the Control Flow tab.



10.



Add a Foreach Loop container onto the Control Flow area under the Data Flow task.

Connect the latter to the Foreach Loop container.



11.



Add an Execute Process task into the Foreach Loop container. Double-click to edit it.



368

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



12.



Click Process (on the left) and enter the following:



Process



Parameter



Comments



Executable



C:\Program Files\Microsoft

SQL Server\110\Tools\Binn\

bcp.exe



The SQL Server BCP Executable. This

will be C:\Program Files\Microsoft SQL

Server\110\Tools\Binn\bcp.exe for SQL

Server 2012. This will be 90 for SQL Server

2005 and 100 for SQL Server 2008.



Arguments



CarSales.dbo. OUT .bcp -N -T



The core BCP command

(minus the table and file names).



WorkingDirectory \\Server\Path



13.



The directory that the BCP files will be

exported to.



You should see something like what’s shown in Figure 7-11.



Figure 7-11.  Exporting BCP Files from an SSIS Execute Process task

14.



Click Expressions, and then click to display the property Expressions Editor. Select

Arguments, as shown in Figure 7-12.



369

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



Figure 7-12.  Defining an expression in SSIS

15.



Click to display the Expression Builder. Enter the following expression:

"Book_TestData.dbo." + @[User::sTableName] + " OUT " + @[User::sTableName]

+ ".bcp -N -T"



16.



You should see a dialog box like the one shown in Figure 7-13.



370

www.it-ebooks.info



Chapter 7 ■ exporting Data from SQL Server



Figure 7-13. Building an expression to run BCP

17.



Test the expression (by clicking Evaluate Expression). Then click OK three times to

confirm the Execute Process task and return to the Data Flow pane.



18.



Add a Script task to the Data Flow pane—inside the Foreach Loop container and

under the Execute Process task. Link the latter to the former.



19.



Double-click the Script task to edit it, and click Script on the left. In the Script pane on

the right, enter sTableName as a read-only variable.



20.



Click Design Script. Replace the Main method with the following code

(C:\SQL2012DIRecipes\CH07\CompressFiles.vb):

Public Sub Main()

Dim

Dim

Dim

Dim



fileName As String

filePath As String

streamWriter As StreamWriter

fileData As String



371

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



Dim WriteBuffer As Byte()

Dim GZStream As GZipStream

fileName = Dts.Variables("sTableName").Value.ToString

filePath = Dts.Variables("FilePath").Value.ToString

Dim fileStream As New FileStream(

filePath & fileName, FileMode.Open, FileAccess.Read)

WriteBuffer = New Byte(CInt(fileStream.Length)) {}

fileStream.Read(WriteBuffer, 0, WriteBuffer.Length)

Dim strDestinationFileName As String

strDestinationFileName = filePath & fileName & ".gz"

Dim fileStreamDestination As New FileStream(

strDestinationFileName, FileMode.OpenOrCreate, FileAccess.Write)

GZStream = New GZipStream(fileStreamDestination, CompressionMode.Compress, True)

GZStream.Write(WriteBuffer, 0, WriteBuffer.Length)

fileStream.Close()

GZStream.Close()

fileStreamDestination.Close()

Dts.TaskResult = Dts.Results.Success

End Sub

21. Save and close the Script editor. Click OK to close the Script task.

You can now run the process, which will export all tables defined in the TableList table to both BCP and

compressed (.gz) files.



How It Works

BCP export is a valid way of transferring multiple tables—assuming that your database’s design and constraints

allow you to re-import data in this way. This recipe shows how SSIS can really shine by not only selecting the

tables to export from a list, but also compressing the exported files.

The set of tables to be exported is held in an SQL Server table named TableList. Of course, this could be an

Excel spreadsheet or a text file. This table is read using an Execute SQL task, which loads the table data into an

SSIS object variable. This variable is then iterated over by an SSIS Foreach Loop container, and each individual

table is exported using a BCP command. The point to note here is that the BCP parameters are defined as part of

an SSIS expression.

Once each table is exported as a BCP file, an SSIS Script task is used to call the GZipStream class to compress

the file.



372

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



■■Note The interesting thing about this recipe is that it requires no third-party software to compress the files.

Instead, the .NET GZIPStream class is used to compress the files. They can later be decompressed by the most

currently available compression utilities, or by an SSIS Script task that uses the GZIPStream class to decompress the files.



Hints, Tips, and Traps





The path to the BCP executable is normally added to the PATH system variable during

installation. If this has not been done, you might find it worth your while to add it

manually.







A simple script like this can be enhanced by the use of variables to hold the table name

and so forth. I will leave you to extend it if you so wish.







This approach is no replacement for a valid backup strategy.



7-10. Exporting a Tiny Dataset As XML

Problem

You want to export a very small dataset from an SQL Server table as an XML file, probably in response to a

completely “ad hoc” request from a user.



Solution

Manually craft the XML as part of a T-SQL SELECT clause, and then copy the output to a text editor and save it.

1.



Use the following code snippet to add beautifully-formed XML containing the data

from the Client table in the sample CarSales database to an XML file

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

CREATE VIEW CarSales.dbo.vw_XMLTest

AS

SELECT XMLCol, SortKey FROM

(

SELECT ' < Root > ' AS XMLCol, 0 AS SortKey

UNION

SELECT

' < Client > '

+ ' < ID > ' + ISNULL(CAST(ID AS VARCHAR(12)),'') + ' '

+ ' < ClientName > ' + ISNULL(ClientName,'') + ' '

+ ' ' AS XMLCol

,1 AS SortKey

FROM dbo.Client



373

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



UNION

SELECT ' ' AS XMLCol, 2 AS SortKey

) MainXML ;

GO

2.



In an SSMS query window run the following code:

SELECT * FROM CarSales.dbo.vw_XMLTest;



3.



Copy and paste the output into a text file



4.



Save the text file.



How It Works

It is possible to export simple XML in the simplest way possible—by hand-crafting the XML, as shown in this

recipe. This will only be of any use if the XML that you are exporting is really uncomplicated—by that I mean,

essentially, that you are exporting a table or view as “flattened” XML, with no hierarchies. However, if this is the

case—then it is your lucky day, as breaking down the output into chunks is not required, and you can output your

XML quickly and easily, as each database record becomes an output record. In a production environment you

are probably going to use FOR XML, so I have to admit, this is a largely theoretical example. Here I have wrapped

the XML in a view that may not be necessary in all circumstances. The reason to wrap this in a view is to include

the  element to form valid XML. The correct order of the output is forced by using a hard-coded SortKey

column. If you need to sort the XML records, use a secondary sort key. You may prefer to wrap the XML SELECT

clauses in a stored procedure—this can also be run from BCP.

This recipe, begs the question “what is a tiny dataset?” Unfortunately, the answer can only be “it depends.”

For me the technique used here is only suitable when exporting a handful of columns (given the fact that handcrafting XML is laborious) in a simple XML format where the rowset is small enough to be copied and pasted.

However, you can decide when—or when not—to use it according to your requirements. Since this approach is

really only designed for more ad hoc XML exports, I am presuming that a simple cut-and-paste will suffice to save

the output as a file. For a more fully automated approach to exporting XML data, please see Recipe 7-11.



7-11. Exporting a Larger Dataset As XML

Problem

You want to export a medium-sized dataset from an SQL Server table as an XML file in a single process. You want

to use T-SQL if possible.



Solution

Create a BCP format file that you then use with BCP to export the XML file. The following code snippet will add

beautifully-formed XML containing the data from the Client table to an XML file.

1.



Create the following BCP format file (C:\SQL2012DIRecipes\CH07\XmlExport.Fmt):

11.0

1

1



SQLBINARY



0



0



""



1



374

www.it-ebooks.info



XmlData



""



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

7-9. Exporting and Compressing Multiple Tables

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

×