Using a variable in a SQL Query in an Execute SQL Task

  • I have a SQL Table that has the email addresses stored in it and the package names also.

    My table1 looks like this:

    PackageName ToEmailID FromEmailID

    PName1 notes4we@sql.com notes4us@sql.com

    PName2 notes4us@sql.com notes4we@sql.com

    PName3 notes4u@sql.com notes4u@sql.com

    Now, I am using an Execute SQL Task and in the SQL Statement, I typed an SQL Query:

    Select ToEmailID

    FROM dbo.Table1

    WHERE PackageName = 'PName2'

    And this query gives me the desired output, which is notes4us@sql.com

    But, in the where clause I am trying to use the following System Variable in this query.

    @[System::PackageName]

    Do you know anyway I can use this in the SQL Query.

    I have a number of packages. So, if I am able to use this variable, then I will not have to write the package name seperately for all the packages.

    Please do let me know if you know someway to achieve this.

    Thank you.

  • Use question mark in sql statement as

    Select ToEmailID

    FROM dbo.Table1

    WHERE PackageName = ?

    Go to parameter mapping. Add a parameter. Rename parameter name to 0. Select system variable for package name from the list. And it is done.

  • Another approach, use the following:

    createprocedure

    (

    @packageNamevarchar (100)

    )

    as

    begin

    Select ToEmailID

    FROM dbo.Table1

    WHERE PackageName = @packageName

    end

    go

    exec @packageName= 'package1'

    go

  • Thank you Prachi. It worked fine.

Viewing 4 posts - 1 through 3 (of 3 total)

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