Click here to Skip to main content
12,299,086 members (53,705 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

How to Send More than 4000 Characters from CLR Stored Proc

, 8 Jun 2013 Apache
Rate this:
Please Sign up or sign in to vote.
Sending more than 4000 characters from CLR stored procedure


I was sending some debug output from my CLR stored procedure via SqlPipe.Send() method, and suddenly I received this exception:

System.ArgumentException: Message length 4474 exceeds maximum length supported of 4000.

I don’t think it says so in the documentation, but sending over 4000 characters is verboten. Similar limit exists for the size of input string parameters, but it can be overcome with some attribute magic as described here.

So, I needed to break my output message into chunks smaller than 4000 chars. Of course, I could just divide the message into 4000-char blocks, but it would break some words in the middle. E.g.

...very long text something something...

could suddenly become:

...very long text som
ething something...

My debug output was sensitive to this kind of thing, so I needed a more intelligent cutting mechanism. I wrote a MessageCutter class that intelligently breaks long messages into smaller parts. It tries to put a part boundary on a new line (best choice), or a space (second best choice). Only if neither new line nor space is available, it will break the message mid-word.

Note to .NET authors: Making the startIndex parameter of String.LastIndexOf to mean the end of the search range was purely evil. Rule of least astonishment violated big time. First, I got a weird exception and then I had exactly the same “am I retarded?” moment as this guy. The sad part is that this API will probably remain this way until the end of time.


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 8 Jun 2013
Article Copyright 2013 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid