0

I'm using C# to build a SQL Server CLR procedure. In that I'm trying to convert a SqlString parameter to string, but the result is being truncated.

Here's simplified version of the code. The parameter "MyParam" contains a string of over 200 characters, but for some reason the ToString() truncates it to 100 characters. Any ideas? Or better way to convert the SqlString parameter.

[Microsoft.SqlServer.Server.SqlProcedure]
public static void MySqlClrProcedure(SqlString MyParam)
{
    SqlContext.Pipe.Send("Test: " + MyParam.ToString());
}
2
  • It would be awesome if you could share a minimal reproducible example. Commented Sep 12, 2019 at 2:49
  • Please show your SQL Server SP definition. Commented Sep 12, 2019 at 3:12

1 Answer 1

1
  1. Try to always use the Value property of the Sql* types. In your case it would be: MyParam.Value. The Value property is available in all of the Sql* types and returns the expected .NET type.

  2. There is no inherent truncation in SqlString as it can support NVARCHAR(MAX). And SqlContext.Pipe.Send() should be limited to 4000 UTF-16 characters (8000 bytes) as it is a PRINT statement.

    Most likely your T-SQL wrapper object has its input parameter defined as NVARCHAR(100). Change it to be NVARCHAR(4000) or even NVARCHAR(MAX). If you are using Visual Studio / SSDT to build this, you can added a SqlFacet decorator to your input parameter to have it use that max length: MySqlClrProcedure([SqlFacet(MaxLength=4000)] SqlString MyParam)

To learn more about working with SQLCLR in general, please check out the series I am writing on this topic:

Stairway to SQLCLR

Also: SQLCLR Info

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.