Tải bản đầy đủ - 0 (trang)
15-9. Handling Errors in SSIS

15-9. Handling Errors in SSIS

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

Chapter 15 ■ Logging and Auditing



Figure 15-3.  Redirecting error rows in a Data Flow task

6.



Confirm with OK. For the moment, disregard the warning.



7.



Add an OLEDB destination to which you connect the OLEDB source component.

Double-click and select the Car_Sales_Staging_OLEDB connection manager. Create

a new table named Clients and map the columns. Set the Error Output for the task to

Redirect Row.



8.



Add two Row Count tasks to the Data Flow pane. Name them RowCountSource

and RowCountDestination. Connect the former to the OLEDB source task and the

latter to the OLEDB destination task. As you connect the Row Counter (using the red

precedence constraint), the Configure Error Output dialog box will appear.

Just click OK.



9.



Add the ErrorRowsSource variable to the Row Count task named RowCountSource,

and the ErrorRowsDestination variable to the Row Count task named

RowCountDestination.



10.



Add a Script component—choose the Transform option. Connect the Row Count task

named RowCountSource to it. Double-click to edit.



875

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



11.



Click Inputs and Outputs and add a new output column to Output 0. Name the

column ErrorColumn. Click Script on the left, and then Edit Script. Add the following

(assuming that the script language is set to Microsoft Visual Basic 2010):

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

Row.ErrorDescription = ComponentMetaData.GetErrorDescription(Row.ErrorCode)

End Sub



12.



Add a Flat File destination task. Connect the Script task to it and configure it with

a delimited file destination. I suggest calling it C:\SQL2012DIRecipes\CH15\

SSISErrors.Txt.Map all the source columns to it (including the three columns added

by SSIS—ErrorCode, ErrorColumn, and ErrorDescription).



13.



Add an OLEDB destination. Connect the RowCountDestination task to it and doubleclick to edit. Use the Car_Sales_Staging_OLEDB connection manager and create a

new table to contain error records. This, too, will contain the fields ErrorCode and

ErrorColumn. The data flow should look like Figure 15-4.



Figure 15-4.  Data flow with error redirection

You can now run the import process. Any errors in the OLEDB source will be sent to the flat file

SSISErrors.Txt. Any errors in the destination task will be captured to the destination database.



How It Works

SSIS also has its own error handling, of course. The essential place where graceful error handling is required is as

part of a data flow, when data cannot be passed through the SSIS pipeline correctly.

SSIS offers three options when a data row contains an error (which tend to be data type errors or data

length errors):





Continue (which might not always succeed).







Discard the row.







Redirect the row to a file or data table.



As merely avoiding a row with an error will result in an incomplete load and no tracking of the missing

records, I only look at the redirect option here. This way you get to isolate any erroneous records. In the real

world, you can send error records to a flat file, the normal destination database—or indeed any database. I find

it much easier to output the source errors to a flat file, as it can handle nearly everything that you throw at it. If

you want to use a destination table here, you must ensure that it has columns that are wide enough to handle

overflow due to truncation—assuming that you are handling truncation errors. In practice, this nearly always

means setting all columns to a very wide NVARCHAR type.



876

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



Hints, Tips, and Traps





A Flat File destination cannot handle certain source column types, such as image

columns. You will need to remove these from the column mapping.







The Script component to add the error description is not absolutely necessary—but it

helps to have fuller error descriptions when debugging!







You can also redirect error information from the data conversion task and the derived

column task.



15-10. Creating a Centralized Logging Framework

Problem

You have developed a fairly complex SSIS package with many calls to T-SQL stored procedures, and you want to

log events to a single repository.



Solution

Develop a custom logging framework to handle logging events from both T-SQL and SSIS. Here is one such

framework:

1.



Create the table (using the following DDL) to hold logged information

(C:\SQL2012DIRecipes\CH15\tblEventDetail.Sql):

CREATE TABLE CarSales_Logging.log.EventDetail

(

EventDetailID INT IDENTITY(1,1) NOT NULL,

Process VARCHAR(255) NULL,

Step VARCHAR(255) NULL,

Comments VARCHAR(MAX) NULL,

ErrorNo INT NULL,

ErrorDescription VARCHAR(MAX) NULL,

ErrorLineNo INT NULL,

ErrorSeverity INT NULL,

ErrorState INT NULL,

StartTime DATETIME NULL,

Logtime DATETIME NULL

);

GO



2.



Using the following DDL, create the stored procedure that logs any outcome

(C:\SQL2012DIRecipes\CH15\pr_LogEvents.Sql):

CREATE PROCEDURE CarSales_Logging.log.pr_LogEvents

(

@Process VARCHAR(150)

,@Step VARCHAR(150)

,@StartTime DATETIME

,@Comments VARCHAR(MAX) = NULL

,@ErrorNo INT = NULL



877

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



,@ErrorDescription VARCHAR(MAX) = NULL

,@ErrorLineNo INT = NULL

,@ErrorSeverity INT = NULL

,@ErrorState INT = NULL

)



AS



INSERT INTO CarSales_Logging.log.EventDetail

(

Process

,Step

,StartTime

,Comments

,ErrorNo

,ErrorDescription

,ErrorLineNo

,ErrorSeverity

,ErrorState

)



VALUES

(

@Process

,@Step

,@StartTime

,@Comments

,@ErrorNo

,@ErrorDescription

,@ErrorLineNo

,@ErrorSeverity

,@ErrorState

) ;

GO

3.



Add logging for all your T-SQL-based processes, like this:

-- Start Header ------------------------------------------------------------DECLARE @StartTime DATETIME

DECLARE @ ProcName VARCHAR(150) --- The calling procedure

DECLARE @ProcStep VARCHAR(150) -- The step in the procedure

SET @ProcName = OBJECT_NAME(@@PROCID)



BEGIN TRY

---- End Header ----------------------------------------------------------- 

SET @ProcStep = 'My test'

   

--Your code here - 



878

www.it-ebooks.info



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

15-9. Handling Errors in SSIS

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

×