따라하기 – 스크립트 구성 요소를 데이터 대상으로 이용하기
따라하기 2의 작업에서 레코드 집합 대상 대신 스크립트 구성 요소를 이용한 대상을 만드는 예제를 구현하겠습니다. 스크립트를 이용한 대상에서는 입력되는 데이터의 ProductName 데이터 중 길이가 가장 긴 데이터와 Quantity 열의 합계를 SSIS의 변수에 저장한 후, 메시지 박스로 출력하는 예제입니다.
따라하기 2의 8단계 까지는 동일합니다.
9. 왼쪽의 도구 상자에서 스크립트 구성 요소를 하나 더 추가한 후, 유형을 대상으로 선택하고 이름을 스크립트 대상으로 변경합니다. 이 후, 변환 작업을 수행하는 스크립트 구성 요소의 녹색 선과 연결합니다.
10. 패키지 수준의 변수를 추가해야 합니다. 작업 영역에서 제어 흐름을 선택한 후, 빈 공간을 눌러 나오는 메뉴에서 변수(S)를 선택하여 변수 창을 띄웁니다.
11. 변수 창에서 maxProductName이라는 String형 변수와 sumQty라는 Int32형 변수를 추가합니다.
12. 데이터 흐름 영역에서 스크립트 대상을 더블 클릭한 후, 입력 열 탭에서 ProductName 열과 Quantity 열을 선택합니다.
13. 스크립트 탭에서 ReadWriteVariables 속성에 maxProductName, sumQty 변수명을 입력한 후, 스크립트 디자인(S)을 클릭하여 VSA를 띄웁니다.
14. 스크립트 탭에서 ReadWriteVariables 속성에 maxProductName, sumQty 변수명을 입력한 후, 스크립트 디자인(S)을 클릭하여 VSA를 띄웁니다.
' Microsoft SQL Server Integration Services user script component ' This is your new script component in Microsoft Visual Basic .NET ' ScriptMain is the entrypoint class for script components Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper Imports Microsoft.SqlServer.Dts.Runtime.Wrapper Public Class ScriptMain Inherits UserComponent Dim tmpStr As String Dim sumQty As Integer Public Overrides Sub PreExecute() tmpStr = "" sumQty = 0 End Sub Public Overrides Sub 입력_ProcessInputRow(ByVal Row As 입력Buffer) If Row.ProductName.Length > tmpStr.Length Then tmpStr = Row.ProductName.ToString End If sumQty = sumQty + Row.Quantity End Sub Public Overrides Sub PostExecute() Variables.maxProductName = tmpStr Variables.sumQty = sumQty End Sub End Class |
15. 이제 최종 결과가 maxProductName, sumQty 변수에 저장됩니다. 이 저장된 값을 확인하기 위해, 제어 흐름 영역에서 스크립트 작업을 추가한 후, 데이터 흐름 작업의 녹색 선과 연결합니다.
(참고 : 본 예제에서는 데이터 흐름 작업의 이름을 “데이터 변환”으로 미리 변경 해 놓았습니다.)
16. 스크립트 작업을 더블 클릭한 후, 스크립트 탭의 ReadOnlyVariables 속성에 maxProductName, sumQty로 변수명을 기입합니다.
17. 스크립트 디자인(S)를 클릭하여 VSA를 띄운 후, 다음 스크립트를 입력합니다.
' Microsoft SQL Server Integration Services Script Task ' Write scripts using Microsoft Visual Basic ' The ScriptMain class is the entry point of the Script Task. Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Runtime Public Class ScriptMain ' The execution engine calls this method when the task executes. ' To access the object model, use the Dts object. Connections, variables, events, ' and logging features are available as static members of the Dts class. ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. ' ' To open Code and Text Editor Help, press F1. ' To open Object Browser, press Ctrl+Alt+J. Public Sub ' ' Add your code here ' MsgBox("Max Length ProductName : " & Dts.Variables("maxProductName").Value.ToString, MsgBoxStyle.Information) MsgBox("Sum Quantity : " & Dts.Variables("sumQty").Value.ToString, MsgBoxStyle.Information) Dts.TaskResult = Dts.Results.Success End Sub End Class |
18. 패키지를 실행하여 수행되는 결과를 확인합니다.
'연구개발 > DTS & SSIS' 카테고리의 다른 글
기본 강좌 40 - 식 (0) | 2009.06.20 |
---|---|
기본 강좌 39 - 변수 (0) | 2009.06.20 |
따라하기 - 스크립트 구성 요소 - 변환 (0) | 2009.06.20 |
따라하기 - 스크립트 구성 요소 - 원본 (0) | 2009.06.20 |
기본 강좌 38 - 스크립트 구성 요소 (0) | 2009.06.20 |