Logo
banner

Blogs

Copying environment variables from one environment to another using SQL query

, October 7, 2022 1962 Views

As any developer, one always follows the cycle of deployment using different environments, i.e., DEV (for development phase or the underground lab) -> UAT (for testing phase or Racoon city) -> PROD (for real-time application or the world).

In SSIS also, we are able to deploy the same SSIS package in 3 different environments, but how do we deploy environment variables for assigning values of parameters?

This is the most asked question whenever we deploy SSIS package in SQL – Can we duplicate environment variables?

Initially, we used to add variables in each of the environments manually. This can be done if there are few parameters and also while creating variables, we have to keep in mind that the name of parameters and variables should be the same to avoid any conflicts.

This can be a cumbersome process if the number of environments is more, for e.g., let’s say we have 20 parameters in our package, then this can be a cumbersome process and there are high chances of any mistakes to happen. Furthermore, if there are more environments, then this becomes a very risky part.

Please note that SSISDB does provide a feature to move the environment variable, but it moves the variables and does not copy the variables to the new environment.

So, to do this without much manual intervention below is the approach that uses a simple T-SQL script using which we can copy-paste variables.

Following are the steps that we need to follow: 

  • Create a new environment.
    To do this, rightclick on the Environmentfolder and select ‘Create Environment. Here, we have created an environment named “UATTest”.
  • After creating the environment, right-click on the new environment that you have recently created and then select properties. Take note of the value of the identifier of the new environment.
  • Right-click on the old environment and select ‘Properties’. Take note of the value of the identifier of the old environment. For example, we have DevTest as our old environment with 3 variables where we have marked one of the variables as sensitive. Please see below:
  • Execute the below query to find the rows for the old environment for each variable in that environment. Look for the old environment_id you noted in the above step.  
    SELECT 
       * 
    FROM 
       [SSISDB].[internal].[environment_variables]


    Please know that we have marked Var3 as a sensitive value so it will be displaying NULL in value and will store the information by encrypting it into sensitive_value column.

  • Now, we have to duplicate the same rows with the new environment ID.  
     
    Below is the SQL Query to “INSERT INTO” statement followed by a select statement.  
    We will need to add a hardcoded value of the new environment ID (which we noted in Step 2) in the “SELECT” statement in the columns and a where clause looking for the previous environment id
    (which we noted in Step 3).

    INSERT INTO [SSISDB].[internal].[environment_variables] 
    (
        [environment_id] 
       ,[name] 
       ,[description] 
       ,[type] 
       ,[sensitive] 
       ,[value] 
       ,[sensitive_value] 
       ,[base_data_type]
    )
    SELECT 
        12 AS environment_id --(New Environment ID) 
       ,[name] 
       ,[description] 
       ,[type] 
       ,[sensitive] 
       ,[value] 
       ,[sensitive_value] 
       ,[base_data_type] 
    FROM 
        [SSISDB].[internal].[environment_variables] 
    WHERE 
        environment_id = 11 --(Previous Environment ID)

    Please make sure that you create the new environment first and get the id’s correct in this script and you should be all set.

  • Now, to test the variables, we can check them by going through the properties of the new environment and then select ‘Variables’.
    Or else we can check it by running the below script:

    SELECT 
       * 
    FROM 
       [SSISDB].[internal].[environment_variables]  
    WHERE 
       environment_id = 12 --new environmentid

As we can see here, using this approach would reduce our efforts to have to create it manually every time. We can easily copy the environment variables to different environments.

Please let us know in the comments if you have any queries.

Suggestions are always welcome! 🙂

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?