How do I capture the single row value from Execute SQL Task that uses an expression?

后端 未结 1 1121
盖世英雄少女心
盖世英雄少女心 2021-01-17 06:55

I have an Execute SQL Task using this expression:

@[User::SQLExportError] + (DT_WSTR,30)@[User::Id]

The expression evaluates correctly to:<

相关标签:
1条回答
  • 2021-01-17 07:33

    I am unable to pinpoint what is causing the error in your scenario. I have tried to recreate the scenario based on the question. The example might help you to figure out what is different on your end.

    Create a simple table named dbo.Error

    CREATE TABLE [dbo].[Error](
        [Id] [int] NOT NULL,
        [SPName] [varchar](20) NOT NULL
    ) ON [PRIMARY]
    GO
    

    Populate the table with some sample data using the below script.

    INSERT INTO dbo.Error (Id, SPName) VALUES 
        (1000, 'export/INSERT'),
        (1000, 'export/DELETE'),
        (1001, 'export/INSERT');
    GO
    

    On the SSIS package, create a connection manager or data source to connect to the database where the above mentioned table is hosted. In this example, I have created an OLE DB connection manager, because that is the recommended provider for SQL Server databases and named the connection manager as Practice.

    Create the following variables on the package.

    Variables

    Set the variable Id to the value 1000 so that the query we will execute in the Execute SQL Task will fetch at least two rows.

    Set the variable SQLExportError to the following query:

    SELECT COUNT(Id) AS ErrorTableCount FROM dbo.Error WHERE SPName in ('export/INSERT','export/DELETE') AND Id =

    Select the variable SQLFetch and press F4 to view the properties. Set the property EvaluateAsExpression to True. Click on the Ellipsis button against the Expression property and set the following expression.

    @[User::SQLExportError] + (DT_WSTR,30)@[User::Id]
    

    Expression

    Drag and drop an Execute SQL Task onto the control flow tab. Configure the General page of the Execute SQL task as shown below to execute the SQL query stored in the expression variable.

    Execute SQL Task - General

    Configure the Result Set page to accept the value returned by the query and store it in the variable.

    Execute SQL Task - Result Set

    Add a script task to view the value stored in the variable. Configure the script task to read the variable ErrorTableCount

    Script Task

    Add the following C# code to the script task.

    public void Main()
    {
        MessageBox.Show(String.Format("Value in variable ErrorTableCount: {0}", 
            Dts.Variables["User::ErrorTableCount"].Value), 
            "Execute SQL Task - Single Row");
        Dts.TaskResult = (int)ScriptResults.Success;
    } =
    

    Script Task Code

    Close the script task editor. Control flow would look something like this.

    Control flow

    When you execute the package, the message box should appear with the count 2 because that is the number of rows in the sample data that have the Id set to the value 1000.

    Hope that helps you to find what is wrong with your configuration.

    Package execution

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