Maximum length of string which can be returned fro

2019-09-14 16:34发布

问题:

I am returning a static string from a stored procedure (in SQL Server 2008) as below:

select 'abcdefgh.........xyz'

If the static string length is exceeding more than some limit (eg:8kb) then only partial string (eg:7kb) is returned to the .net apps.

Though I tried in different ways like assigning static string to varchar(max) and selecting the variable, is still returning only partial string.

I should return complete string which could be of max of 5mb. So, main concerns:

  1. What is the max string length I can return from a stored procedure
  2. How to return 5 mb string from stored procedure to .net apps.

I request someone can help me to resolve this issue. please find the code below

 using (SqlCommand command = new SqlCommand(Source.GetExportRecordSP, Connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@CandidateRecordID ", SqlDbType.NVarChar, 32)).Value = record;
                try
                {
                    if (Connection.State != ConnectionState.Open)
                    {
                        Connection.Open();
                    }
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                    if(reader.Read())
                        {
                            xmlRecord = new XmlDocument();
                            xmlRecord.LoadXml(reader.GetString(0));
                        }
                    }
                }
                catch (Exception Ex)
                {
                    Logging.WriteError(string.Format("Error while retrieving the Record \"{0}\" details from Database. Exception: {1} ", Ex.ToString()));
                    throw;
                }               
            }

Thanks in advance geeks.

回答1:

Since you appear not to be using an OLEDB connection (which has an 8k limit), I think the problem is in your procedure code.

Or, perhaps, the compatibility version of your database is set to something other than SQL Server 2008 (SQL Server 2000 could not return more than 8k using GetString()).



回答2:

Thanks for support, I found 1 fix for this at http://www.sqlservercentral.com/Forums/Topic350590-145-1.aspx

Fix is, declare a variable, and should be initlized to empty string and concatenated with the main string.

  DECLARE @test varchar(MAX);
set @test =''
 select  @test = @test + '<Invoice>.....'

If the string length is <8000 it will work without the above approach.

Thanks all.