Problem with maximum expression length in SSIS 2008

Posted on 2012-09-21
Last Modified: 2012-09-21
I'm trying to build the content of an email that is sent via a Script Task rather than the standard Send Mail Task control. The string that goes in to the email content is quite long. I understand the issue with the maximum string length in an expression being 4000 characters. I thought I got round it by creating three string variables, populating them in expressions shorter than 4000 characters, and then having another variable which concatenates all three variables. On second thoughts, this was a little stupid in that the result of the expression of adding the three variables together could possibly still exceed 4000 characters. I've noticed from:

4000 limit blog

that it's not necessarily the limitation of the string variable that is 4000 characters, but the outcome of an expression.

Is there therefore, another means of populating a variable in this way please? Even if it's just a mechanism to populate the variable from concatenating the three variables mentioned above via a script task may be? Not too good on those really.
Question by:Grffster
    LVL 65

    Expert Comment

    by:Jim Horn
    Not sure if this helps, but I've built email messages with SQL Stored procedures before, then just call the SP within SSIS

    Two tasks:
    (1) Execute SQL Task to run a function and stick it into a variable
    (2) Send Mail Task to send an email, using that variable as the Expressions:MessageSource

    Here's the SP code, watered down a little:
    CREATE FUNCTION ufn_ssis_import_XXX_success_email_message
    RETURNS varchar(4000)
    We may want to, when time permits, add some additional information:
    1.	Count of records by History Code
    2.	Count of new records added to USER
    3.	Count of new policies (rows) added to MEDICA_USER
    Declare @msg varchar(4000), @CRLF varchar, @ret int
    SELECT @CRLF = CHAR(13) + CHAR(10)
    SELECT @msg = @CRLF + @CRLF 
    SELECT @msg = @msg + '========================================' + @CRLF 
    SELECT @msg = @msg + 'COUNTS' + @CRLF
    SELECT @msg = @msg + '========================================' + @CRLF 
    SELECT @msg = @msg + CAST(@ret as varchar) + ' Imported Members From File' + @CRLF
    SELECT @msg = @msg + 'XXX' + ' Total Active Member' + @CRLF
    SELECT @msg = @msg + 'XXX' + ' Total Unique  Members (From All Imports)' + @CRLF
    SELECT @msg = @msg + 'XXX' + ' Failed Records In Current File' + @CRLF
    SELECT @msg = @msg + CAST(@ret as varchar) + ' New Terminated Members' + @CRLF
    SELECT @msg = @msg + 'XXX' + ' Unprocessed Members (if non-zero, something happened; needs to be restarted)' + @CRLF
    RETURN @msg

    Open in new window

    LVL 13

    Expert Comment

    What you can do is create a script task, in the code (C# or VB) you declare a String variable, you put all your text (greater than 4000) in that string variable and assign the string variable to the variable in your package. Have you tried this?

    When you add the script task add your variable (from your package) to the ReadWriteVariables.

    In the main function you can try something along the lines of:

    String myEmail="";

    myEmail="This is my over 4000 char long email";

    Let us know if this works for you.

    Author Comment

    I've tried a hybrid of your solution, Lionking by doing the following in a VB script:

    Dts.Variables["User::HTMLEmailBody2"].Value = Dts.Variables["User::HTMLEmailBodyPt1"].Value + Dts.Variables["User::HTMLEmailBodyPt2"].Value + Dts.Variables["User::HTMLEmailBodyPt3"].Value

    Open in new window

    where HTMLEmailBody2 is a read/write variable and the others are read only. The others are built dynamically in expressions. None of which should go over 4000 characters. Problem is I get this error:

    operator '+' cannot be applied to operands of type 'object' and 'object'

    Open in new window

    Have I done something wrong here?
    LVL 13

    Accepted Solution

    Add .ToString() to each the variables.
    Like Dts.Variables["User::HTMLEmailBodyPt1"].Value.ToString() + ....

    Author Closing Comment

    This has worked thanks. Didn't try jimhorn's solution as this seemed a little cleaner really. Thanks to Jim too.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now