Foreach and Variables...

  • Please forgive me if this is stupid but I've search everywhere and can't seem to find anything on this.

    Object of SSIS: I need to go through a collection (from one select statement) and query off of that in order to set a value. For example...given results from query named "A". I then take "A" and for each record, set a column on the table to yes where certain conditions are met.

    I have a fairly simple SSIS package. There is an "Execute SQL Task" that feeds a "Foreach Loop Container" that has another "Execute SQL Task" inside of it.

    Feeder SQL Task

    Query

    SELECT isnull(MonitorAlert,'No'), WEBfeWBPhaseID

    FROM tbl_WRPhase

    WHERE (DATEDIFF(day, GETDATE(), ManualAlertDate) > 7) OR

    (ManualAlertDate IS NULL)

    ResultSet: Full result set

    On the Result Set tab:

    Result Name: 0

    Variable Name: User:: PhasesToCheck of type Object

    Foreach Loop Container

    Collection Tab

    Enumerator: Foreach ADO Enumerator

    ADO object source variable: User:: PhasesToCheck

    Rows in the first table checked

    Variable Mappings Tab

    Variable: User::MonitorAlert

    Index: 0

    Variable: User::WEBfeWBPhaseID

    Index: 1

    Inner SQL Task

    In a query within this, how do I reference the User::WEBfeWBPhaseID variable? I've tried [User::WEBfeWBPhaseID] and @WEBfeWBPhaseID but both don't seem to work. Do I need to set the parameter mappings? I'm so confused if I've even done it right.

    Thank you for any and all help. I hope I've explained it well enough. (Also, if there's a better way of doing it, please dont hesitate to say so)

  • Yes you need to set the parameters for the Execute SQL Task and use select the variables as Input types. Not sure what type of query you are using but if you are trying to execute a procedure use something like

    Exec my_sp ?,? (for OLDBD type connection)

    And depending on your connection type you need to set the parameter marker, parameter name etc. The following link might be useful for this

    http://www.sqljunkies.com/WebLog/knight_reign/archive/2005/10/05/17016.aspx

  • Thanks. I went ahead and in the Parameter mapping section of the interior SQL Task and set this...

    Variable Name: User::WEBfeWBPhaseID

    Direction: Input

    Data Type: LARGE_INTEGER

    Parameter Name: 0

    Then in the task, I use ? where I need the variable. Here's my question. Can I use it more than once?

    When I run the SSIS, I get this error...

    failed with the following error: "Syntax error, permission violation, or other nonspecific error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

  • Sorry, forgot to add that I tried adding this...

    DECLARE @WEBfeWBPhaseID as int

    SET @WEBfeWBPhaseID = ?

    and then just using @WEBfeWBPhaseID when I need it but I get the same error.

  • What is the datatype of your SSIS variable? If its Int32 try to use LONG as datatype in Parameter mappings.

  • You are correct. User::WEBfeWBPhaseID is Int32. I changed the parameter mapping to be LONG but the same error comes up.

    The query I've been trying (extremely simple for now) is this:

    DECLARE @WEBfeWBPhaseID as int

    SET @WEBfeWBPhaseID = ?

    UPDATE tbl_WRPhase

    SET MonitorAlert = 'Yes'

    WHERE WEBfeWBPhaseID = @WEBfeWBPhaseID

    Once I get it working, the query will be more complicated.

  • I did a simple test and the same scenario works absolutely fine for me. Not sure anymore what the problem could be.

    The only other thing that comes to my mind is Null values, is it possible that the this parameter could be Null by any chance? NULL values can cause such errors too.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply