?
Solved

Problem with maximum expression length in SSIS 2008

Posted on 2012-09-21
5
Medium Priority
?
1,683 Views
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.
0
Comment
Question by:Grffster
  • 2
  • 2
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38421504
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)
AS

begin


/*
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 @ret = COUNT(IMPORT_XXX_MEMBER_ID) FROM IMPORT_XXX_MEMBER_FINAL
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 @ret = COUNT(IMPORT_834_MEMBER_ID) FROM IMPORT_834_MEMBER_FINAL WHERE USE_CASE = 2
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
END

Open in new window

0
 
LVL 13

Expert Comment

by:LIONKING
ID: 38421565
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";
Dts.Variables["User::myPackageVariable"].Value=myEmail;
Dts.TaskResult=Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success;


Let us know if this works for you.
0
 

Author Comment

by:Grffster
ID: 38421754
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?
0
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 total points
ID: 38421773
Add .ToString() to each the variables.
Like Dts.Variables["User::HTMLEmailBodyPt1"].Value.ToString() + ....
0
 

Author Closing Comment

by:Grffster
ID: 38421929
This has worked thanks. Didn't try jimhorn's solution as this seemed a little cleaner really. Thanks to Jim too.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

839 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