Using SSIS Expressions for Dynamic Connection Strings

By Tom Nonmacher

Welcome to another post on SQLSupport.org. Today, we will be discussing a fascinating topic: Using SSIS Expressions for Dynamic Connection Strings. SSIS (SQL Server Integration Services) is a powerful tool offered in SQL Server 2012 and SQL Server 2014 for data integration and transformations. It becomes even more powerful when combined with dynamic expressions, which allow for greater flexibility and adaptability in your ETL processes. One of the most common uses of these expressions is to dynamically set connection strings for your data sources and destinations.

The connection string is a series of name-value pairs that specify how a connection is made to a data source. When working with multiple environments like development, testing, and production, it is often necessary to change the connection string. Instead of manually changing it each time, you can use SSIS expressions to make the process automatic and error-proof.

To achieve this, we will use the "Expression" property of the ConnectionManager object in SSIS. This property allows us to set the connection string dynamically. The expression can be a combination of variables and constants, and it can use any of the functions and operators that SSIS supports.


-- T-SQL code for setting a connection string dynamically
"Data Source=" + @[User::ServerName] + ";Initial Catalog=" + @[User::DatabaseName] + ";Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;"

In the above T-SQL code, we are constructing a connection string using the "ServerName" and "DatabaseName" variables. These variables can be set dynamically at runtime, allowing the same SSIS package to connect to different servers or databases depending on the requirements of the task at hand.

This feature is not limited to SQL Server databases. It can be used with any data source that SSIS supports, including MySQL 5.6 and DB2 10.5. The only requirement is that the connection string must be correctly formatted for the specific data source.


-- MySQL connection string example
"Server=" + @[User::ServerName] + ";Database=" + @[User::DatabaseName] + ";Uid=" + @[User::UserName] + ";Pwd=" + @[User::Password] + ";"

The above MySQL connection string example demonstrates the dynamic creation of a connection string using variables for the server name, database name, user name and password. This way, you can adapt your SSIS packages to different environments or requirements without having to modify the package itself.

Lastly, it's worth mentioning that this feature works perfectly with Azure SQL as well, making it an excellent tool for hybrid or cloud-based scenarios. The connection string format for Azure SQL is very similar to the one for SQL Server, but it includes the Azure SQL server name and optionally the Azure SQL database name.


-- Azure SQL connection string example
"Server=tcp:" + @[User::ServerName] + ".database.windows.net,1433;Initial Catalog=" + @[User::DatabaseName] + ";Persist Security Info=False;User ID=" + @[User::UserName] + ";Password=" + @[User::Password] + ";MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"

In conclusion, the use of SSIS expressions for dynamic connection strings is a powerful technique that can greatly simplify the development and maintenance of your SSIS packages, especially in environments where the data sources are subject to change. Happy coding!




181F93
Please enter the code from the image above in the box below.