Problem with maximum expression length in SSIS 2008

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.
Who is Participating?
LIONKINGConnect With a Mentor Commented:
Add .ToString() to each the variables.
Like Dts.Variables["User::HTMLEmailBodyPt1"].Value.ToString() + ....
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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 @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


Open in new window

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.
GrffsterAuthor Commented:
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?
GrffsterAuthor Commented:
This has worked thanks. Didn't try jimhorn's solution as this seemed a little cleaner really. Thanks to Jim too.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.