Variables and expressions are key to making SQL Server Integration Services (SSIS) packages dynamic, reusable, and adaptable to runtime conditions. Below is a detailed explanation with practical examples.
1. Variables in SSIS
What Are Variables?
Variables store values that can be used across the SSIS package, such as file paths, SQL queries, flags, or counters.
Types of Variables
| Type | Scope | Example |
|---|---|---|
| System Variables | Built-in (read-only) | System::PackageName, System::StartTime |
| User Variables | Custom (read/write) | User::SourceFilePath, User::RowCount |
Variable Properties
Name: Unique identifier (e.g.,
User::FileName).Data Type:
String,Int32,DateTime,Boolean, etc.Value: Default value (can be changed at runtime).
Scope: Defines where the variable is accessible (Package, Container, or Task level).
How to Create a Variable?
Open Variables Window (
SSIS > VariablesorCtrl + \).Click Add Variable and set:
Name:
User::SourceFolderData Type:
StringValue:
C:\Data\
2. Expressions in SSIS
What Are Expressions?
Expressions are formulas that dynamically compute values at runtime using:
Variables (
@[User::VarName])Operators (
+,-,==,&&)Functions (
GETDATE(),SUBSTRING(),REPLACE())
Where Can Expressions Be Used?
Variable Expressions (Update a variable dynamically).
Property Expressions (Modify task properties at runtime).
Precedence Constraints (Conditionally execute tasks).
SSIS Expression Language Syntax
| Component | Example |
|---|---|
| Variable Reference | @[User::SourcePath] + "file.txt" |
| String Concatenation | "Server=" + @[User::ServerName] |
| Date Functions | (DT_WSTR,4)YEAR(GETDATE()) → "2024" |
| Conditional Logic | @[User::IsFullLoad] ? "TRUNCATE TABLE X" : "SELECT * FROM X" |