如何在 SQLServer2008ManagementStudio 中查看文本或 varchar (MAX)列的完整内容?

在这个即时 SQL Server 2008(build 10.0.1600)数据库中,有一个 Events表,其中包含一个名为 Detailstext列。(是的,我知道这实际上应该是一个 varchar(MAX)列,但是设置这个数据库的人没有这样做。)

此列包含非常大的异常日志和相关的 JSON 数据,我试图通过 SQLServerManagementStudio 访问这些日志和数据,但每当我将结果从网格复制到文本编辑器时,它都会截断为43679个字符。

我在互联网上读到过,你可以将 Tools > Options > Query Results > SQL Server > Results To Grid中 XML 数据检索的最大字符数设置为 Unlimited,然后执行如下查询:

select Convert(xml, Details) from Events
where EventID = 13920

(注意,数据是列根本不是 XML。将列转换为 XML 仅仅是我从 Google 中发现的一种变通方法,其他人已经使用这种方法绕过了 SSMS 从 textvarchar(MAX)列检索数据的限制。)

但是,在设置上面的选项、运行查询并单击结果中的链接之后,我仍然会得到以下错误:

无法显示 XML。发生了以下错误: 文件发生意外结束。第5行,位置220160。

一种解决方案是增加从服务器检索的 XML 数据字符数。若要更改此设置,请在“工具”菜单上单击“选项”。

那么,知道怎么获取这些数据吗?将列转换为 varchar(MAX)会解决我的问题吗?

139278 次浏览

It sounds like the Xml may not be well formed. If that is the case, then you will not be able to cast it as Xml and given that, you are limited in how much text you can return in Management Studio. However, you could break up the text into smaller chunks like so:

With Tally As
(
Select ROW_NUMBER() OVER ( ORDER BY s1.object_id ) - 1 As Num
From sys.sysobjects As s1
Cross Join sys.sysobjects As s2
)
Select Substring(T1.textCol, T2.Num * 8000 + 1, 8000)
From Table As T1
Cross Join Tally As T2
Where T2.Num <= Ceiling(Len(T1.textCol) / 8000)
Order By T2.Num

You would then need to manually combine them again.

EDIT

It sounds like there are some characters in the text data that the Xml parser does not like. You could try converting those values to entities and then try the Convert(xml, data) trick. So something like:

Update Table
Set Data = Replace(Cast(Data As varchar(max)),'<','&lt;')

(I needed to cast to varchar(max) because the replace function will not work on text columns. There should not be any reason you couldn't convert those text columns to varchar(max).)

The data type TEXT is old and should not be used anymore, it is a pain to select data out of a TEXT column.

ntext, text, and image (Transact-SQL)

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

you need to use TEXTPTR (Transact-SQL) to retrieve the text data.

Also see this article on Handling The Text Data Type.

You are out of luck, I think. THe problem is not a SQL level problem as all other answers seem to focus on, but simply one of the user interface. Management Studio is not meant to be a general purpose / generic data access interface. It is not there to be your interface, but your administrative area, and it has serious limitations handling binary data and large test data - because people using it within the specified usage profile will not run into this problem.

Presenting large text data is simply not the planned usage.

Your only choice would be a table valued function that takes the text input and cuts it rows for every line, so that Management Studio gets a list of rows, not a single row.

SSMS only allows unlimited data for XML data. This is not the default and needs to be set in the options.

enter image description here

One trick which might work in quite limited circumstances is simply naming the column in a special manner as below so it gets treated as XML data.

DECLARE @S varchar(max) = 'A'


SET @S =  REPLICATE(@S,100000) + 'B'


SELECT @S as [XML_F52E2B61-18A1-11d1-B105-00805F49916B]

In SSMS (at least versions 2012 to current of 18.3) this displays the results as below

enter image description here

Clicking on it opens the full results in the XML viewer. Scrolling to the right shows the last character of B is preserved,

However this does have some significant problems. Adding extra columns to the query breaks the effect and extra rows all become concatenated with the first one. Finally if the string contains characters such as < opening the XML viewer fails with a parsing error.

A more robust way of doing this that avoids issues of SQL Server converting < to &lt; etc or failing due to these characters is below (credit Adam Machanic here).

DECLARE @S varchar(max)


SELECT @S = ''


SELECT @S = @S + '
' + OBJECT_DEFINITION(OBJECT_ID) FROM SYS.PROCEDURES


SELECT @S AS [processing-instruction(x)] FOR XML PATH('')

The simplest workaround I found is to backup the table and view the script. To do this

  1. Right click your database and choose Tasks > Generate Scripts...
  2. "Introduction" page click Next
  3. "Choose Objects" page
    1. Choose the Select specific database objects and select your table.
    2. Click Next
  4. "Set Scripting Options" page
    1. Set the output type to Save scripts to a specific location
    2. Select Save to file and fill in the related options
    3. Click the Advanced button
    4. Set General > Types of data to script to Data only or Schema and Data and click ok
    5. Click Next
  5. "Summary Page" click next
  6. Your sql script should be generated based on the options you set in 4.2. Open this file up and view your data.

I was able to get this to work...

SELECT CAST('<![CDATA[' + LargeTextColumn + ']]>' AS XML) FROM TableName;

I prefer this simple XML hack which makes columns clickable in SSMS on a cell-by-cell basis. With this method, you can view your data quickly in SSMS’s tabular view and click on particular cells to see the full value when they are interesting. This is identical to the OP’s technique except that it avoids the XML errors.

SELECT
e.EventID
,CAST(REPLACE(REPLACE(e.Details, '&', '&amp;'), '<', '&lt;') AS XML) Details
FROM Events e
WHERE 1=1
AND e.EventID BETWEEN 13920 AND 13930
;

One work-around is to right-click on the result set and select "Save Results As...". This exports it to a CSV file with the entire contents of the column. Not perfect but worked well enough for me.

Workaround

Did you try this simple solution? Only 2 clicks away!

At the query window,

  1. set query options to "Results to Grid", run your query
  2. Right click on the results tab at the grid corner, save results as any files

You will get all the text you want to see in the file!!! I can see 130,556 characters for my result of a varchar(MAX) field

Results in a file

Starting from SSMS 18.2, you can now view up to 2 million characters in the grid results. Source

Allow more data to be displayed (Result to Text) and stored in cells (Result to Grid). SSMS now allows up to 2M characters for both.

I verified this with the code below.

DECLARE @S varchar(max) = 'A'


SET @S =  REPLICATE(@S,2000000) + 'B'


SELECT @S as a
declare @takeOver table(details nvarchar(max))
declare @json_auto nvarchar(max)


select @json_auto = (select distinct
From table_1 cg
inner join table_2 c
on cg.column_1= c.column_1and cg.isDeleted =0 and c.isdeleted = 0
inner join table_3 d
on c.column_2= d.column_2 and d.isdeleted = 0
where cg.Id= 1017
for Json Auto)


insert into @takeOver
values(@json_auto)


select * from @takeOver