Maximum length of string which can be returned from stored proc in SQL Server 2008 to .net apps

丶灬走出姿态 提交于 2019-12-12 02:28:41

问题


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.



来源:https://stackoverflow.com/questions/9400934/maximum-length-of-string-which-can-be-returned-from-stored-proc-in-sql-server-20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!