Today, I am going to give basic example of Lookup Transformation Task in SSIS. Use of Look up Transformation: In my source system (table), I have all the product with their details. Somehow I have products which belongs to the country which doesn’t exist in my reference (master) table. I assigned a job to rectify those products. I need to design ETL which gives me those records whenever we import products to our target database (table). So here, I am going to use “Lookup no match output” to capture those records by following steps: Let’s take an example to easily understand how to use Lookup Transformation in SSIS. 1. Create Source Connection: Select and drag “Data Flow Task”, from “Control Flow Items” to designer surface. Then double click it and Create a New OLEDB connection. 2. Create sample tables Now we will create tables named ‘LKP_Countries_Source’ and ‘LKP_Countries’ into Test Database from the given script. 1: CREATE TABLE [LKP_Countries_Source]2: (3: [CountryCode] [int] NULL,4: [CountryName] [varchar](100) NULL5: )6: GO7:8: INSERT INTO [LKP_Countries_Source]9: ( [CountryCode]10: ,[CountryName])11: VALUES12: (91, N'India')13: ,(92, N'Pakistan')14: ,(93, N'Afghanistan')15: ,(94, N'Sri Lanka')16: ,(95, N'Myanmar')17: ,(960, N'Maldives')18: ,(961, N'Lebanon')19: ,(962, N'Jordan')20: ,(963, N'Syrian Arab Republic')21: ,(964, N'Iraq')22: ,(965, N'Kuwait')23: ,(966, N'Saudi Arabia')24: ,(967, N'Yemen')25: ,(968, N'Oman')26: ,(971, N'United Arab Emirates')27: ,(972, N'Israel')28: ,(1, N'USA')29: ,(65, N'Singapore')30: GO 3. Create Lookup connection:1: CREATE TABLE [LKP_Countries]2: (3: [Country] [varchar](100) NULL4: ,[Code] [int] NULL5: )6: GO7:8: INSERT [LKP_Countries]9: (10: [Country]11: ,[Code]12: )13: VALUES14: (N'INDIA', 91)15: ,(N'SINGAPORE', 65)16: ,(N'USA', 1)17: ,(N'PAKISTAN', 92)18: GO Now you Need to select the proper OLEDB connection in “Connection Manager” tab and the source table for lookup task. 4. Columns Selection from Source Table: Select the columns to use as output columns. 5. Lookup Transformation Editor: Here, I have added “Lookup Data Transformation” Task to designer tab and click on edit to configure the Lookup transformation. 6. Handle No Match output: Now we need to configure various sections in the “Lookup Transformation Editor”. In General section, select “Redirect rows to no match output” to handle the unmatched data from the lookup task. Here we will need to select Cache mode as “Full Cache”. This option is used to improve the performance while handling large scale of data.
7. Set Connection Manager for Lookup table: In Connection section select the reference table with proper connection. This list will get compared with source dataset for matching the data. In Columns section, select the available input columns and map it with the available lookup columns. This will create a join between 2 source datasets.We have used “Full Cache Mode”, so Advanced section will be disabled and in “Error Output” keep fields as it is. 9. Input Output Selection Setup:Select new “OLEDB Destination” transformation and drag it to the designer surface. Drag green arrow from “Lookup Transformation” Task to “OLEDB Destination” and provide “Lookup Match Output” as Output and click OK. 10. Create Output table for Match and Not Matched Data:Now we will need to create output tables to store the matched as well as not matched result. 11. Data Mapping for Result table:1: CREATE TABLE [LKP_Output_Match]2: (3: [CountryCode] INT,4: [CountryName] VARCHAR(100),5: [Country_Calling_Code] INT6: )7: GO8:9: CREATE TABLE [LKP_Output_NO_Match]10: (11: [CountryCode] INT NULL,12: [CountryName] VARCHAR(100) NULL,13: )14: GO Now I need to provide mapping for the Output Table to store the Matched Data. 12. Complete Data Flow for Lookup Transformation: In order to handle the “Not Matched data”, provide the link of Not Matched data to OLEDB Destination table “LKP_Output_No_Match”. It will store the not matched results. 13. Package Execution and Result: Now let’s execute the Package and check with the output inside the tables we have created to store the result as in “LKP_Output_Match” table for Matched Data and “LKP_Output_NO_Match” table for Not Matched Data. Result :1: SELECT * FROM LKP_Output_Match2:3: SELECT * FROM LKP_Output_NO_Match4: GO Lookup Transformation can be used in various ways according to the requirements and can be implemented accordingly. This was just an understanding document for Lookup Transformation. Reference: Tejas Shah (www.SQLYoga.com) |
Learn SQL and database management at SQLYoga for articles, tutorials, and tips to improve your skills and streamline data operations. Join our community!
March 31, 2014
SQL SERVER: SSIS - Look Up Transformation Task
Labels:
Data Flow Task,
Lookup,
Lookup Transformation Task,
Match Data output,
SQL,
SQL Server,
SSIS,
Tejas Shah
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
March 28, 2014
T-SQL to find out Space by File Group
T-SQL script to find out SQL SERVER FileGroup space details:
SELECT fg.groupname AS 'File Group', Name, CONVERT (Decimal(15,2),ROUND(f.Size/128.000,2)) [Currently Allocated Space (MB)], CONVERT (Decimal(15,2), ROUND(FILEPROPERTY(f.Name,'SpaceUsed')/128.000,2)) AS [Space Used (MB)], CONVERT (Decimal(15,2), ROUND((f.Size-FILEPROPERTY(f.Name,'SpaceUsed'))/128.000,2)) AS [Available Space (MB)] FROM dbo.sysfiles f (NOLOCK) JOIN sysfilegroups fg (NOLOCK) ON f.groupid = fg.groupid ORDER BY fg.groupname
Labels:
FileGroup,
SQL Server,
SQLYoga,
T-SQL,
Tejas Shah
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
Subscribe to:
Posts (Atom)