Share via

Dynamically build variable and assign value

Vaishu 41 Reputation points
2021-01-02T15:50:50.603+00:00

Is this possible in script task or better way of doing

Trying to dynamically build variable and assign value to dynamic variables

Example

String varValue = "TEST" , this value passed from parameter, it varies

public void Main()
{
// Lock variables

String varValue = "TEST"
String varName =  "User::"+ varValue 



Dts.VariableDispenser.LockForRead("User::ReadVariable"); 

Dts.VariableDispenser.LockForWrite(varName );

Variables vars = null;

Dts.VariableDispenser.GetVariables(ref vars);

vars[varName] .Value = vars["User::ReadVariable"].Value;

vars.Unlock();

Dts.TaskResult = (int)ScriptResults.Success;

}

SQL Server Integration Services
0 comments No comments

1 answer

Sort by: Most helpful
  1. Monalv-MSFT 5,926 Reputation points
    2021-01-04T08:15:50.597+00:00

    Hi @Vaishu ,

    Please try the following code :

    Sub Main()    
        
        Dim pkg As Package = New Package    
        Dim v100 As Variable = pkg.Variables.Add("myVar", False, "", 1)    
        v100.EvaluateAsExpression = True    
        v100.Expression = "100 * 2"    
        Console.WriteLine("Expression for myVar: {0}", _    
          v100.Properties("Expression").GetValue(v100))    
        Console.WriteLine("Value of myVar: {0}", v100.Value.ToString)    
        Console.Read()    
        
    End Sub    
    

    We can refer to the following links:

    1. Working with Variables Programmatically
    2. Logging in the Script Task

    Best Regards,
    Mona

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.