How To Use Dynamic Query in Lookup Transformation in SSIS Package - SQL Server Integration Services (SSIS) Tutorial

Scenario:

We have a big reference table from which we need to extract records in Lookup so we can use against Source File. The table has the data from three regions AS,EU and NA. The files come with the name AS_FileName,EU_FileName and NA_FileName, Instead of extracting all the records for lookup , we want to extract the records according to the region Name. How can we perform that?

What we will learn in this video

  1. How to create SSIS Package from scratch
  2. How to Loop through files by using For-each Loop Container in SSIS Package
  3. How to Read the part of File Name and Save into Variable by using Expressions
  4. How to make use Variable to make Lookup Query dynamic in SSIS Package
  5. How to filter the rows for Lookup Reference Data Set by using Variable Value

Solution:

Let's create Lookup ( Reference Table) with some Data.

--drop table [dbo].[Customer_Dst]
CREATE TABLE [dbo].[Customer_Dst](
    [CustomerId] [int] NULL,
    [SalesAmt] int
   
) 

--drop table [dbo].[Lkp_Customer]
CREATE TABLE [dbo].[Lkp_Customer](
    [CustomerId] [int] NULL,
    [CustomerName] [varchar](100) NULL,
    [StreetAddress] [varchar](100) NULL,
    [City] [varchar](100) NULL,
    [State] [char](2) NULL,
    [Region] [varchar](2) NOT NULL
) 

insert into  [dbo].[Lkp_Customer]

SELECT *
FROM (
    SELECT N'1' AS [CustomerId]
        ,N'Aamir' AS [CustomerName]
        ,N'1234 Street' AS [StreetAddress]
        ,N'Sahiwal' AS [City]
        ,'PJ' AS [State]
        ,N'AS' AS [Region]
    
    UNION ALL
    
    SELECT N'2' AS [CustomerId]
        ,N'M Raza' AS [CustomerName]
        ,N'Test Street Address' AS [StreetAddress]
        ,N'Lahore' AS [City]
        ,N'PJ' AS [State]
        ,N'AS' AS [Region]
    
    UNION ALL
    
    SELECT N'3' AS [CustomerId]
        ,N'Robert' AS [CustomerName]
        ,N'1234 Street' AS [StreetAddress]
        ,N'FrankFurt' AS [City]
        ,'Germany' AS [State]
        ,N'EU' AS [Region]
    
    UNION ALL
    
    SELECT N'4' AS [CustomerId]
        ,N'John' AS [CustomerName]
        ,N'1234 Street' AS [StreetAddress]
        ,N'Paris' AS [City]
        ,'France' AS [State]
        ,N'EU' AS [Region]
    
    UNION ALL
    
    SELECT N'5' AS [CustomerId]
        ,N'Sam' AS [CustomerName]
        ,N'1234 Street' AS [StreetAddress]
        ,N'Charlotte' AS [City]
        ,N'NC' AS [State]
        ,N'NA' AS [Region]
    ) t;


Query for the Lookup

Select CustomerId,CustomerName,Region from [dbo].[Lkp_Customer] Where Region='AS'

Expressions for RegionCode Variable
Substring( @[User::FileName],1,2)


Expressions for Flat File Connection Manager
@[User::LocalFolder]+"\\"+ @[User::FileName]


How to use Variable in Lookup Transformation SQL Query to Make it Dynamic in SSIS Package




  Related Posts / Videos on Lookup Transformation 



6 comments:

  1. Hi,

    Not sure if you are interested in 3rd party product but here is the solution,
    Link here" SSIS Upsert

    ReplyDelete
  2. From where I can download the files used in the videos?

    ReplyDelete
  3. Great explanation! Dynamic queries in SSIS Lookup can be a bit tricky, especially when performance is a concern or when working with varying source structures. Your step-by-step breakdown made the concept much clearer.

    While I'm from a completely different industry—waterproofing services—I always appreciate clean logic and well-documented processes like this. Whether it’s sealing wet walls or debugging data flows, the value of precision and planning applies everywhere. Keep up the great content!

    ReplyDelete
  4. This article does a great job breaking down how to use dynamic queries in Lookup transformations. The step-by-step explanation and clear examples made it easy to follow and implement. It’s always great to see complex SSIS concepts simplified so effectively.

    It also highlights how customization leads to better results—something that applies beyond data tools. In areas like waterproofing of homes, tailored solutions are just as crucial. Every home has its own challenges, and the right approach makes all the difference in long-term protection.

    Thanks for the valuable insights—looking forward to more from this site!

    ReplyDelete
  5. This tutorial was incredibly helpful—clear, practical, and right on point for anyone working with SSIS packages. The explanation of dynamic queries within the Lookup transformation was exactly what I needed to resolve a long-standing issue. It’s always great to come across content that simplifies complex tasks without skipping important details. Just like in tech, having strong waterproofing solutions at home is about precision and prevention—solving the right problem before it becomes a bigger one.

    ReplyDelete