SQL Server 2005 SSIS SQL Execut SQL task to put single value in variable

Posted on 2009-02-23
Last Modified: 2013-11-10
I am trying to get a single column/row value into a variable. No matter what I try I do not get any values into my variable. Help.

SQL to get one column value:
select top 1 cast(ADMIN_EMAIL as varchar(100)) from ADMIN_EMAIL
(ADMIN_EMAIL = nvarchar(100), not null)
Result set user::MailTo (String)
Result Name = 0, Variable Name: user::MailTo
Connection Type: OLE DB
SQLSource: Direct Input
ResultSet: Single Row

Question by:Ima Selco
    LVL 30

    Accepted Solution

    How are you verifying that your variable is not populated? Variable values are reset once run time os over. You can't run the package, stop it, then check the variable value as it will be reset.
    Try adding a script task afterwards to display your variable (attached is one I quickly took out of one of my packages)

    Imports System
    Imports System.Data
    Imports System.Math
    Imports Microsoft.SqlServer.Dts.Runtime
    Public Class ScriptMain
    Public Sub Main()
            Dim sVariable As String
            Dim vars As Variables
            Dts.VariableDispenser.LockOneForRead("DateFromSQLTask", vars)
            sVariable = vars(0).Value.ToString
            Dts.TaskResult = Dts.Results.Success
            Dts.TaskResult = Dts.Results.Success
    End Sub
    End Class

    Open in new window


    Author Closing Comment

    by:Ima Selco
    I consider it a bug in MS Dev Studio when you execute a task, in debug mode, that updates a variable and the variable is unchanged after the task has executed.
    LVL 30

    Expert Comment

    Call it a feature :)
    It doesn't really work that way though.... when you are in in design/debug mode, the variable is changed after the task is executed. Its only when you drop out of design mode that it changes.
    Thats the nature of a variable.... its only there during run time. Thats the way most other development paltforms work. (except DTS of course!)

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    Suggested Solutions

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now