Click here to Skip to main content
15,881,803 members
Articles / All Topics
Technical Blog

How to Send More than 4000 Characters from CLR Stored Proc

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
8 Jun 2013Apache1 min read 8.5K   2  
Sending more than 4000 characters from CLR stored procedure

Introduction

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 was originally posted at http://www.ikriv.com/blog?p=1296

License

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


Written By
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: www.ikriv.com.

Comments and Discussions

 
-- There are no messages in this forum --