Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:32:18+00:00 2026-05-22T16:32:18+00:00

I have a result set from an Execute SQL Task query saved on a

  • 0

I have a result set from an Execute SQL Task query saved on a System.Object variable and i would like to send this results using a Send Mail Task using the following expression on the MessageSource.

“Please find attached data summary\n\n” + SUBSTRING( @[User::myVariable] ,1,3990)
+ “\n\n”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:32:19+00:00Added an answer on May 22, 2026 at 4:32 pm

    Probably, you have found an answer to your question by now. This answer is for others who might stumble upon this question. I don’t think you can use object variable in expression. You need to loop through the query result object and format it to a string so that you can send the query output in an e-mail message. You can also export the data to a file and send the file as an attachment. That is another possible option. This example shows how to loop through the query result set to form the message body that will then be emailed using Send Email task.

    Step-by-step process:

    1. Create a table named dbo.EmailData using the script provided under SQL Scripts section.

    2. Screenshot #1 shows sample data that Execute SQL task will query and send it in an e-mail in this example.

    3. On the SSIS package, create 5 variables as shown in screenshot #2.

    4. On the SSIS package, place the following tasks: Execute SQL task, Foreach loop container, Script task within the Foreach loop container and Send Email task.

    5. Configure the Execute SQL task as shown in screenshots #3 and #4.

    6. Configure the Foreach loop container as shown in screenshots #5 and #6. Variable mappings section shows the order in which the query result columns appear and how they are assigned to SSIS variables. These variables will be used to form the email message inside the Script task.

    7. In the Script task, replace the code with the one shown under the Script task code section. The script task has very simple plain text email message formatting.

    8. Configure the Send Email task as shown in screenshot #7. You need to configure it with valid email address in From and To fields.

    9. After configuring the Control flow tasks, your package should look like as shown in screenshot #8.

    10. Sample package execution is shown in screenshot #9.

    11. E-mail sent by the package is shown in screenshot #10. Some information have been removed from the screenshot. You can compare the table data shown in screenshot #1 with this email output and they should same.

    Hope that helps.

    SQL Scripts:
    .

    CREATE TABLE [dbo].[EmailData](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [ItemId] [varchar](255) NOT NULL,
        [ItemName] [varchar](255) NOT NULL,
        [ItemType] [varchar](255) NOT NULL,
        [IsProcessed] [bit] NULL,
     CONSTRAINT [PK_EmailData] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
    GO
    

    Script task code:

    C# code that can be used only in SSIS 2008 and above.
    .

    /*Microsoft SQL Server Integration Services Script Task
       Write scripts using Microsoft Visual C# 2008.
       The ScriptMain is the entry point class of the script.
    */
    
    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    
    namespace ST_7f59d09774914001b60a99a90809d5c5.csproj
    {
        [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
        public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        {
    
            #region VSTA generated code
            enum ScriptResults
            {
                Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
                Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
            };
            #endregion
    
            public void Main()
            {
                Variables varCollection = null;
                string header = string.Empty;
                string message = string.Empty;
    
                Dts.VariableDispenser.LockForWrite("User::EmailMessage");
                Dts.VariableDispenser.LockForWrite("User::ItemId");
                Dts.VariableDispenser.LockForWrite("User::ItemName");
                Dts.VariableDispenser.LockForWrite("User::ItemType");
                Dts.VariableDispenser.GetVariables(ref varCollection);
    
                //Set the header message for the query result
                if (varCollection["User::EmailMessage"].Value == string.Empty)
                {
                    header = "Execute SQL task output sent using Send Email Task in SSIS:\n\n";
                    header += string.Format("{0}\t{1}\t\t\t{2}\n", "Item number", "Item name", "Item type");
                    varCollection["User::EmailMessage"].Value = header;
                }
    
                //Format the query result with tab delimiters
                message = string.Format("{0}\t{1}\t{2}",
                                            varCollection["User::ItemId"].Value,
                                            varCollection["User::ItemName"].Value,
                                            varCollection["User::ItemType"].Value);
    
                varCollection["User::EmailMessage"].Value = varCollection["User::EmailMessage"].Value + message;
    
                Dts.TaskResult = (int)ScriptResults.Success;
            }
        }
    }
    

    Screenshot #1:

    1

    Screenshot #2:

    2

    Screenshot #3:

    3

    Screenshot #4:

    4

    Screenshot #5:

    5

    Screenshot #6:

    6

    Screenshot #7:

    7

    Screenshot #8:

    8

    Screenshot #9:

    9

    Screenshot #10:

    10

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ResultSet object containing all the rows returned from an sql query.
I have a java.sql.ResultSet object containg data from a query that was run. How
Using T-SQL, I would like to execute an UPDATE statement that will SET columns
I have a very complex Linq to SQL query that returns a result set
I have a result set in MS-SQL within a stored procedure, and lets say
I have a query that returns a result set similar to the one below:
I have a dynamic query which I call and I put the Result set
I am having a problem getting a result set from my Sql Server 2008
I have two results from a data set. I want to add both results
I have a large result set assembled in a parent/child relationship. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.