Dump SSIS USER variable name and value in SQL Server table

后端 未结 1 1890
南方客
南方客 2020-12-21 07:02

Purpose: Fetch all the user variables that are in the SSIS package and write the variable name and their values in a SQL Server 2008 table.

相关标签:
1条回答
  • 2020-12-21 07:16

    I can think of 2 ways of solving this issue right now .

    1. Use the same script task to insert the values into the Database
    2. using Foreach loop to enumerate the ssis package variables ( as you have asked )

    Table Script for inserting the variable name and value is

    CREATE TABLE [dbo].[SSISVariables]
    (
    [Name] [varchar](50) NULL,
    [Value] [varchar](50) NULL
    )
    

    1.Use Script task and write the below code

    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        private static string m_connectionString = @"Data Source=Server Name;
        Initial Catalog=Practice;Integrated Security=True";
       public void Main()
        {
           List<SSISVariable> _coll = new List<SSISVariable>();
            Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
            Package pkg = app.LoadPackage(PackageLocation,Null);
                       Variables pkgVars = pkg.Variables;
    
            foreach (Variable pkgVar in pkgVars)
            {
                if (pkgVar.Namespace.ToString() == "User")
                {
                    _coll.Add(new SSISVariable ()
                    {
                     Name =pkgVar.Name.ToString(),
                     Val =pkgVar .Value.ToString () 
                    });
               }
            }
            InsertIntoTable(_coll);
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    
        public void InsertIntoTable(List<SSISVariable> _collDetails)
        {  
           using (SqlConnection conn = new SqlConnection(m_connectionString))
            {
                conn.Open();
                foreach (var item in _collDetails )
                {
                 SqlCommand command = new SqlCommand("Insert into SSISVariables values (@name,@value)", conn);
                 command.Parameters.Add("@name", SqlDbType.VarChar).Value = item.Name ;
    
                 command.Parameters.Add("@value", SqlDbType.VarChar).Value = item.Val ;
                 command.ExecuteNonQuery();    
                }
            }
         }
      }
    
       public class SSISVariable
       {
        public string Name { get; set; }
        public string Val { get; set; }
       }
    

    Explanation:In this ,i'm creating a class which has properties Name and Val . Retrieve the package variables and their value using your code and store them in a collection List<SSISVariable> .Then pass this collection to a method (InsertIntoTable) which simply inserts the value into the database by enumerating through the collection .

    Note :

    There is a performance issue with the above code ,as for every variable 
    im hitting the database and inserting the value.You can use
    [TVP][1]( SQL Server 2008)   or stored procedure which takes
     xml( sql server 2005) as input. 
    

    2.Using ForEach Loop

    Design

    enter image description here

    Step 1: create a Variable VariableCollection which is of type System.Object and another variable Item which is of type String to store the result from Foreach loop

    Step 2: For the 1st script task .

    VariableCollection is used to store the Variable name and its value enter image description here

    Step 3: Inside the script task Main Method write the following code

      public void Main()
       {
         ArrayList _coll = new ArrayList(); 
    
            Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
            Package pkg = app.LoadPackage(Your Package Location,null);
    
            Variables pkgVars = pkg.Variables;
            foreach (Variable pkgVar in pkgVars)
            {
                if (pkgVar.Namespace.ToString() == "User")
                {
                    _coll.Add(string.Concat ( pkgVar.Name,',',pkgVar.Value ));
                }
            }
            Dts.Variables["User::VariableCollection"].Value = _coll;
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    

    Step 4: Drag a Foreach loop and in the expression use Foreach from Variable Enumerator

    enter image description here

    Step 5:Foreach loop enumerates the value and stores it in a variable User::Item

    enter image description here

    Step 6:Inside the foreach loop drag a script task and select the variable

    readonly variables   User::Item 
    

    step 7: Write the below code in the main method

       public void Main()
        {
          string name = string.Empty;
          string[] variableCollection;
          variableCollection = Dts.Variables["User::Item"].Value.ToString().Split(',');
          using (SqlConnection conn = new SqlConnection(m_connectionString))
            {
                conn.Open();
                SqlCommand command = new SqlCommand("Insert into SSISVariables values (@name,@value)", conn);
                command.Parameters.Add("@name", SqlDbType.VarChar).Value = variableCollection[0];
    
                command.Parameters.Add("@value", SqlDbType.VarChar).Value = variableCollection[1];
    
                command.ExecuteNonQuery();
            }
            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    

    Explanation : In the Foreach loop ,i can only enumerate one variable .So the logic is, i need to somehow pass the variable name and its value into one variable .And for this ,i have concatenated name and value

     string.Concat ( pkgVar.Name,',',pkgVar.Value )
    

    Script task in the foreach loop simply splits the variable and stores it into a array of string and then u can access the name and value using array index and store it in the database

    0 讨论(0)
提交回复
热议问题