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. 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. 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 ![]() Reference: Tejas Shah (www.SQLYoga.com) |
March 31, 2014
March 28, 2014
T-SQL to find out Space by File Group
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
September 4, 2013
SQL SERVER: T-SQL to read all attributes of the XML
XML:
Expected Result:

Query to achieve the same:
DECLARE @XML AS XML SELECT @XML = '' SELECT CAST(x.v.query('local-name(.)') AS VARCHAR(100)) AS AttributeName ,x.v.value('.', 'VARCHAR(100)') AttributeValue FROM @XML.nodes('//@*') x(v) ORDER BY AttributeName
July 8, 2013
SQL Yoga: Parse XML with namespace with SQL SERVER
Recently, we were working on XML having namespace and we need to read that XML to fetch the information from the XML. Here, is the Sample XML which we are trying to parse: DECLARE @x XML If we need to fetch the “info” element with T-SQL we can write query as follows: ;WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com/search/local/ws/rest/v1') Here, we have to use “WITH XMLNAMESPACES”, so we can specify the namespace of the XML that we are reading, else it wont give us any result. |
May 6, 2013
SQL Yoga – Read XML with T-SQL statement in SQL SERVER
In my previous post, “XML Result sets with SQL Server”, we review to generate result sets in XML from SQL server. Then I got a comment from the team, to also have post to read XML in SQL Server. To read XML in SQL server, is also simple. Lets read the XML which is created by XML PATH in previous post. Read XML Elements with T-SQL: DECLARE @SQLYoga TABLE( DECLARE @xml XML Now, please find query to read the query to read XML generated above: SELECT x.v.value('ID[1]', 'INT') AS ID, This query generates the output as follows: That’s it. It is much simple and you can get rid of the complex coding in application. Let me know your comments or issues you are facing while working on this. Reference: Tejas Shah (www.SQLYoga.com) |
May 3, 2013
SQLYoga – XML Result sets with SQL Server
Recently, found that people making much complex code to have an XML in application. I have found that they return result set in Table to the application and have applied code to convert that data table to XML by Asp. Net application. When I review the process, I have suggested that why don’t you use the XML feature of the application. They surprised the simple use of it and make their life easy. Let me have sample query to generate XML result sets in SQL Server database. There are two ways to have an XML from query: 1. FOR XML RAW 2. FOR XML PATH Please find following sample queries where both of the way are being used: Lets create sample data DECLARE @SQLYoga TABLE( Generate XML with XML ROW SELECT * XML RAW, returns the XML by each record with row element and all columns as attributes in the XML. Generate XML with XML PATH SELECT * XML PATH, returns the XML by each record with separate element and also columns as element within respected element of the record. That’s it. It is much simple and you can get rid of the complex coding in application. Let me know your comments or issues you are facing while working on this. Reference: Tejas Shah (www.SQLYoga.com) |
April 30, 2013
SQL SERVER: Backup the database and upload it on FTP and on Network
Recently one of my friend called me and asked me that his MS SQL SERVER Database server is crashed and the problem is, he has the database backups on the same server. So now as the server is crashed, and he cannot have any backup to restore the database. I suggested him, first get data recovery tool to get the data from crashed server and then restore it to make it work, so application will be up. Then, to overcome this kind of problem in future, I suggested him to always keep the database backup on another server and it is best to have database back up on another location too. He agreed with me that, and asked me a good question that: "Do you know if there is any kind of software by which I can schedule this and get out of this problem?" As he needs the solution quickly, I checked it out some Tools and I come with the very cool tool "SQL SERVER Backup And FTP". This is very simple to use. This tool works as:
We need to install this software on the server and we can take SQL SERVER Database backup on Network and FTP together. First please install this software from SQL SERVER Backup And FTP and Let me explain it how it works:
Here I have done following:
That's it and now we are out of stress to move database backup to another server or on FTP Let's click on "Run now" to check it out. After process is completed, Two zip files, pubs and SQLYoga is on Network path as well as on FTP. Another cool feature is that, we can schedule Full Backup, Differential Backup, and Transaction Backup, so we can have very minimal amount of data loss.
Here, I have configured this software as:
Here, we assume that it is ok that we have 15 minutes data loss. It is depends on your application to decide this time window. Let's take one case study to get more understanding about to restore these backups: e.g. Database server is crashed at 8:20 AM, How can I restore my database from the backups?
Another, option is also available, to remove old Differential backup and Transaction backup. Once new differential backup is generated, there is no need to keep old differential backup. We only need to restore recent differential backup, so we can remove old differential backups. We can also remove all transaction log backup which are generated before Differential Log. SO to remove old backups, this software is also providing an option for the same, we just need to checked this option and that's it. This software is also allowed following settings (advanced):
I like this tool, I recommend this tool to take back up and move it on FTP. Try to use it and let me know your comments |
December 24, 2012
SQL SERVER: Cross tab Query with Pivot (Dynamic Pivot)
CREATE TABLE #TestTable(ctrl_no INT, id int, Amount NUMERIC(18,2), OrderDate DATE) INSERT INTO #TestTable(ctrl_no , id , Amount , OrderDate ) SELECT 1000029,100016,990.0000,'10/25/2012' UNION ALL SELECT 1000029,100018,990.0000,'10/25/2012' UNION ALL SELECT 1000029,100016,660.0000,'10/26/2012' UNION ALL SELECT 1000029,100018,660.0000,'10/26/2012' UNION ALL SELECT 1000029,206007,660.0000,'10/26/2012' UNION ALL SELECT 1000029,206007,660.0000,'10/27/2012' DECLARE @MinDate DATE ,@MaxDate DATE DECLARE @Dates VARCHAR(MAX) SELECT @Dates = '' SELECT @MinDate = MIN(OrderDate) ,@MaxDate = MAX(OrderDate) FROM #TestTable WHILE @MinDate <= @MaxDate BEGIN SELECT @Dates = @Dates + '[' + CONVERT(VARCHAR(10), @MinDate, 101) + '],' SELECT @MinDate = DATEADD(d, 1, @MinDate) END SELECT @Dates = LEFT(@Dates, LEN(@Dates) - 1) DECLARE @qry NVARCHAR(MAX) SELECT @qry = N' SELECT id, ' + @Dates + ' FROM ( SELECT id, ISNULL(Amount,0) AS Amount, OrderDate FROM #TestTable t ) x PIVOT( SUM(Amount) FOR OrderDate IN(' + @Dates + ') ) p ' EXEC (@qry) DROP TABLE #TestTable
November 20, 2012
Configure SQL Reporting Service, to Access Reports using External IP With IIS 7
Hi All, Recently I come to situation where I need to Configure Reporting services on server having configuration: Window server 2008 and IIS 7. I need to configure reports in manner, so User can access this reports thru External IP Address. I found configuration for IIS 7.0, so user can access reports externally. To able to access the reports externally, do the steps as mentioned below: 1. Create Virtual Directory in IIS 7.0 : 2. Go to Handler Mappings : 3. Double Click on "Handler Mapping". You will get screen like the given below: 4. Now see Top Right Corner: Click On "Add Managed Handler". Here you need to fill up the info like: Fill this info as: Request path: Reserved.ReportViewerWebControl.axd Thats it. Now try with External IP. You can access the reports with External IP too. |
November 5, 2012
SQL SERVER: Cross tab query with PIVOT
ctrl_no | id | Amount | OrderDate |
1000029 | 100016 | 990 | 10/25/2012 |
1000029 | 100018 | 990 | 10/25/2012 |
1000029 | 100016 | 660 | 10/26/2012 |
1000029 | 100018 | 660 | 10/26/2012 |
1000029 | 206007 | 660 | 10/26/2012 |
1000029 | 206007 | 660 | 10/27/2012 |
id | 10/25/2012 | 10/26/2012 | 10/27/2012 |
100016 | 990.00 | 660.00 | NULL |
100018 | 990.00 | 660.00 | NULL |
206007 | NULL | 660.00 | 660.00 |
CREATE TABLE #TestTable ( ctrl_no INT ,id INT ,Amount NUMERIC(18, 2) ,OrderDate DATE ) INSERT INTO #TestTable(ctrl_no , id , Amount , OrderDate ) SELECT 1000029,100016,990.0000,'10/25/2012' UNION ALL SELECT 1000029,100018,990.0000,'10/25/2012' UNION ALL SELECT 1000029,100016,660.0000,'10/26/2012' UNION ALL SELECT 1000029,100018,660.0000,'10/26/2012' UNION ALL SELECT 1000029,206007,660.0000,'10/26/2012' UNION ALL SELECT 1000029,206007,660.0000,'10/27/2012' SELECT id ,[10/25/2012] ,[10/26/2012] ,[10/27/2012] FROM ( SELECT id ,Amount ,OrderDate FROM #TestTable t ) x PIVOT(SUM(Amount) FOR OrderDate IN ( [10/25/2012] ,[10/26/2012] ,[10/27/2012] )) p DROP TABLE #TestTable
About Me

- Ahmedabad, Gujarat, India
- 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
Popular Posts
-
Today I have the following situation, where I need to display all related data in comma separated list. Till today we are using s...
-
With SSIS, most of the time we need to process all the files from the folder. Once file has been processed, we need to move the file to ...
-
Today, I have one requirement to check dynamically if a node exists in my xml or NOT. I have a stored procedure that receives XML and ...
-
Today, I am going to give basic example of Lookup Transformation Task in SSIS. Lookup transformation performs lookup operation by joini...