Tải bản đầy đủ - 0 (trang)
7-2. Exporting Data As a Delimited Text File

7-2. Exporting Data As a Delimited Text File

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

Chapter 7 ■ Exporting Data from SQL Server



Figure 7-5.  Choosing a Flat File export format

6.



Configure the Flat File Connection Manager Editor as follows (assuming that you are

using the examples for this book—otherwise, use your own file path):

Name:



Delimited_Unicode



File Name:



C:\SQL2012DIRecipes\CH07\ClientsDelimited.txt



Unicode:



Ticked



Column Names in First Data Row



Ticked



Text Qualifier







Header rows to skip



0



Header Row delimiter







The dialog box should look like Figure 7-6.



352

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



Figure 7-6.  Setting the Flat File connection manager to export to a delimited flat file

7.



Click OK to confirm your changes.



8.



Click Mappings and ensure that all the output columns are mapped to the source

columns.



And that, fairly simply, is that. You can now export your data.



How It Works

Quite possibly the most frequently used export type (in my experience) is text. This is probably because most

database or spreadsheet programs can accept external delimited text files. At risk of stating the obvious, a text file

will have the characteristics shown in Table 7-2.



353

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



Table 7-2.  Text File Characteristics



Option



Definition



Character type



ASCII or Unicode. For ASCII export, the code page can be defined.



Column separator



Chosen by the user. The default is comma (,).



Row Delimiter



Chosen by the user. The default is Carriage Return + Line Feed (CR/LF).



Text Qualifier



Optional. Can be defined for each column.



You can choose to add data to an existing text file, or delete the contents of an existing file. To replace the

file contents, check “Overwrite data in the file” in the Flat File Destination Editor. It is worth noting that you can

create a Flat File connection manager before creating the Flat File destination and connecting it to the OLEDB

source, but this will not automatically generate the destination column structure for you. This means that you

will have to create all the columns manually, which is laborious, to say the least. A consequence of this is that if

the source data changes radically, you may find it easier to delete the existing Flat File connection manager and

replace it with a new one, rather than attempt to reconfigure the destination. This is compounded by the fact that

the only way to reorder columns, unfortunately, is to delete and re-insert them.

When tweaking (as opposed to re-creating) a Flat File destination, you can add or delete columns using the

New and Delete buttons. When adjusting column specifications in the Advanced pane, you can select multiple

columns with the Ctrl key and sets of columns using the Shift key.

There are a series of useful options for exporting text files (delimited or fixed-width). Table 7-3 provides a

brief explanation of the principal variations on this particular theme.

Table 7-3.  SSIS Text Destination Options



Option



Dialog Box Page



Comments



Locale



General page



Currency settings and so forth.



Unicode



General page



Checking this will export the file using Unicode.



Code Page



General page



If your destination file must use a specific code page (and

therefore is non-Unicode)



Header Row Delimiter



Columns page



Specifies the header row delimiter. This can be different from

the delimiter for data rows.



Header Rows to Skip



General page



Entering a figure here will cause the output to skip the first

“n” records.



Text Qualifier



General page



The character—usually doublequotes (")—that will be used

to encapsulate text fields that contain extra occurrences of the

column separator character(s).



Column Names

in the First Data Row



General page



Adds the column names as the first row in the output file.



Row Delimiter



Columns page



Specifies the default row delimiter.



Column Delimiter



Columns page



Specifies the default column delimiter. This can be specified

individually for each column in the Advanced page. Equally,

if you want to use a delimiter that is not in the pop-up list, use

the Advanced page.

(continued)



354

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



Table 7-3.  (continued)



Option



Dialog Box Page



Comments



Name



Advanced page



Allows you to rename a column. The name that will be used

if you choose to add the column names as the first row in the

output file.



Column Delimiter



Advanced page



Allows you to specify the column delimiter for this column,

and override the default. You can also enter a character (or

several characters) if the one(s) you require are not in the list.



Text Qualified



Advanced page



You can specify if this column is enclosed in the text qualifier.



Hints, Tips, and Traps





Exporting a text file as Unicode will double its size—as each character will be two bytes

instead of one. For extremely large files, the simplicity of avoiding the hassle of code

pages can come at a considerable cost in disk space and network transfer times.







You may prefer to alias columns in the T-SQL SELECT statement at source rather than

rename them in the Advanced page of the Flat File destination adapter.







Of course, you can select a table as the source for the data, but this is only good practice

if you are using all the source columns. SSIS will export the data faster if you choose only

the required columns as part of the SELECT statement, rather than choosing a table or view

source and subsequently deselecting the columns that you do not require in the data flow.







Adding a text qualifier will also encapsulate the column title in quotes for each column

where the TextQualified advanced column property is set to True. If you find this

annoying or unusable, then consider outputting the column headers as part of the SELECT

statement using a UNION clause, as shown in Recipe 7-4.







You are free to choose the file name and extension that you prefer, but it makes it easier

for those who receive your files if you respect the (fairly) standard conventions of .CSV for

comma-separated values, .PSV for pipe-separated values, and so forth.







If you are trying to adhere to the CSV specification (of course, I am using the term

somewhat loosely) and wish to escape double quotes inside field data, then the

easiest—if not the only—way to do this is to extend the SQL SELECT clause to include a

REPLACE function. To give an example, your code could be:

SELECT

FROM



ID, ClientName, Country, Town, County,

REPLACE(Address1,'"','""'), Address2,

ClientType, ClientSize

dbo.Client;



This will replace every double quote in the Address1 field with two double quotes.





The Flat File destination does not have to be simply connected to a single OLEDB source;

it can be the result of a complex SSIS data transformation process from multiple data

sources.



355

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



7-3. Exporting Data to a Fixed-Width Text File

Problem

You want to export data to a text file, but the recipient of the data wants it to be in fixed-width format, not the

usual delimited type.



Solution

Use SSIS and the Flat File destination, but specify a fixed-width file format. You export data in this format in the

following way.

1.



Perform steps 1 to 4 from Recipe 7-2, but specify Fixed Width as the Flat File format.

Configure the new Flat File connection manager that you create so that it looks like

Figure 7-7.



Figure 7-7.  Fixed-width flat file output



356

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



2.



Check the Advanced page, where you should see that the column widths have been

calculated for you, with the maximum text width for each text column, and the widest

necessary column for each numeric or date/time field type.



How It Works

Exporting a fixed-width file is largely similar to the process described in Recipe 7-2. It is, if anything, even simpler

than exporting a delimited file. One thing that makes it easier is that you do not need to worry about column

separators since columns are “delimited” according to the number of characters from the start of the row.

Another thing that makes it easier is that since there are no column delimiters, there is no requirement to escape

any character.



Hints, Tips, and Traps





A ragged-right export includes a CR/LF at the end of every row; a fixed-width export will not.



7-4. Exporting Text Files from the Command Line

Problem

You want to export text files without using SSIS, T-SQL, or the Import/Export Wizard, possibly as part of a script.



Solution

Use BCP to export data from a Command window or a Command (.cmd) file as a text file.

To export data as a text file from a table, use the following code snippet in a Command window. (Note that

all the following code extracts are in the file C:\SQL2012DIRecipes\CH07\BCPOut1.cmd):

bcp CarSales.dbo.Client OUT C:\SQL2012DIRecipes\CH07\aa1.txt -SAdam_01\Adam –c -T

Something similar, but using a SELECT, statement would be:

bcp "SELECT ID,ClientName FROM CarSales.dbo.Client" 

QUERYOUT C:\SQL2012DIRecipes\CH07\aa1.txt -SAdam_01\Adam –c -T

This command uses a stored procedure to return data (assuming that I had previously created one called

pr_OutputClients containing a query such as SELECT ID,ClientName FROM CarSales.dbo.Client):

bcp "EXECUTE CarSales.dbo.pr_OutputClients" 

QUERYOUT C:\SQL2012DIRecipes\CH07\aa1.txt -SAdam_01\Adam –c -T

The easiest way to encapsulate column data in quotes is to include it in the source T-SQL:

SELECT

FROM



ID, + '"' + ClientName + '"', Country

CarSales.dbo.Client;



357

www.it-ebooks.info



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

7-2. Exporting Data As a Delimited Text File

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

×