如何在 SQL Server 2008 Management Studio 中查看 text 或 varchar(MAX) 列的完整内容?

发布于 2024-08-31 07:27:10 字数 957 浏览 4 评论 0原文

在此实时 SQL Server 2008(内部版本 10.0.1600)数据库中,有一个 Events 表,其中包含一个名为 Detailstext 列。 (是的,我意识到这实际上应该是一个 varchar(MAX) 列,但是设置此数据库的人并没有这样做。)

该列包含非常大的异常日志和关联的 JSON 数据,我尝试通过 SQL Server Management Studio 进行访问,但每当我将结果从网格复制到文本编辑器时,它都会将其截断为 43679 个字符。

我在 Internet 上的多个位置读到,您可以在 Tools > 中设置 XML 数据检索的最大字符数。选项>查询结果> SQL服务器> Results To Grid 为 Unlimited,然后执行如下查询:(

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

请注意,数据列根本不是 XML。CONVERT 将列转换为 XML 只是一种解决方法,我通过谷歌搜索发现其他人已经用来绕过 SSMS 从 textvarchar(MAX) 列检索数据的限制。)

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

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

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

那么,知道如何访问这些数据吗?将列转换为 varchar(MAX) 可以解决我的问题吗?

In this live SQL Server 2008 (build 10.0.1600) database, there's an Events table, which contains a text column named Details. (Yes, I realize this should actually be a varchar(MAX) column, but whoever set this database up did not do it that way.)

This column contains very large logs of exceptions and associated JSON data that I'm trying to access through SQL Server Management Studio, but whenever I copy the results from the grid to a text editor, it truncates it at 43679 characters.

I've read on various locations on the Internet that you can set your Maximum Characters Retrieved for XML Data in Tools > Options > Query Results > SQL Server > Results To Grid to Unlimited, and then perform a query such as this:

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

(Note that the data is column is not XML at all. CONVERTing the column to XML is merely a workaround I found from Googling that someone else has used to get around the limit SSMS has from retrieving data from a text or varchar(MAX) column.)

However, after setting the option above, running the query, and clicking on the link in the result, I still get the following error:

Unable to show XML. The following error happened:
Unexpected end of file has occurred. Line 5, position 220160.

One solution is to increase the number of characters retrieved from the server for XML data. To change this setting, on the Tools menu, click Options.

So, any idea on how to access this data? Would converting the column to varchar(MAX) fix my woes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

霓裳挽歌倾城醉 2024-09-07 07:27:10

SSMS 只允许无限量的 XML 数据。这不是默认值,需要在选项中设置。

输入图像描述这里

在相当有限的情况下可能起作用的一个技巧是简单地以如下特殊方式命名列,以便将其视为 XML 数据。

DECLARE @S varchar(max) = 'A'

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

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

在 SSMS 中(至少版本 2012 到当前版本 18.3),结果显示如下

在此处输入图像描述

单击它会在 XML 查看器中打开完整结果。向右滚动显示 B 的最后一个字符被保留,

但是这确实存在一些重大问题。向查询添加额外的列会破坏效果,并且额外的行都会与第一行连接起来。最后,如果字符串包含诸如 < 之类的字符,则打开 XML 查看器会失败并出现解析错误。

下面是一种更可靠的方法,可以避免 SQL Server 将 < 转换为 < 等问题或由于这些字符而失败(在此注明 Adam Machanic)。

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('')

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 < 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('')
眸中客 2024-09-07 07:27:10

我能够让它发挥作用......

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

I was able to get this to work...

SELECT CAST('<![CDATA[' + LargeTextColumn + ']]>' AS XML) FROM TableName;
蓝梦月影 2024-09-07 07:27:10

一种解决方法是右键单击结果集并选择“将结果另存为...”。这会将其导出到包含该列的全部内容的 CSV 文件。虽然不完美,但对我来说已经足够好了。

解决方法

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

旧街凉风 2024-09-07 07:27:10

您尝试过这个简单的解决方案吗?只需点击 2 次即可!

在查询窗口中,

  1. 将查询选项设置为“结果到网格”,运行查询
  2. 右键单击网格角的结果选项卡,将结果另存为任何文件

您将获得您想要在文件中看到的所有文本!我可以看到 varchar(MAX) 字段的结果有 130,556 个字符

文件中的结果

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

游魂 2024-09-07 07:27:10

我发现的最简单的解决方法是备份表并查看脚本。为此,

  1. 右键单击您的数据库并选择Tasks > 生成脚本...
  2. “简介”页面单击下一步
  3. “选择对象”页面
    1. 选择选择特定数据库对象并选择您的表。
    2. 点击下一步
  4. “设置脚本选项”页面
    1. 将输出类型设置为将脚本保存到特定位置
    2. 选择保存到文件并填写相关选项
    3. 点击高级按钮
    4. 设置常规 > 要编写脚本的数据类型仅数据架构和数据,然后单击“确定”
    5. 点击下一步
  5. “摘要页面”单击next
  6. 您的sql脚本应该根据您在4.2中设置的选项生成。打开此文件并查看您的数据。

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.
迷爱 2024-09-07 07:27:10

听起来 Xml 的格式可能不太好。如果是这种情况,那么您将无法将其转换为 Xml,并且鉴于此,您可以在 Management Studio 中返回的文本量受到限制。但是,您可以将文本分成更小的块,如下所示:

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

然后您需要再次手动组合它们。

编辑

听起来 Xml 解析器不喜欢 text 数据中的某些字符。您可以尝试将这些值转换为实体,然后尝试 Convert(xml, data) 技巧。所以像这样:(

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

我需要转换为 varchar(max) 因为替换函数不适用于 text 列。您应该没有任何理由无法转换这些 text 列转换为 varchar(max)。)

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)),'<','<')

(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).)

旧时浪漫 2024-09-07 07:27:10

数据类型 TEXT 很旧,不应再使用,从 TEXT 列中选择数据很痛苦。

ntext、文本和图像 (Transact-SQL)

ntext、文本和图像数据类型
将在未来版本中删除
微软SQL服务器。避免使用
这些数据类型在新开发中
工作,并计划修改应用程序
目前正在使用它们。使用
nvarchar(最大值)、varchar(最大值)和
varbinary(max) 代替。

您需要使用 TEXTPTR (Transact-SQL) 检索文本数据。

另请参阅这篇关于处理文本数据类型的文章。

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.

乄_柒ぐ汐 2024-09-07 07:27:10

我想你运气不好。这个问题不是 SQL 级别的问题,就像所有其他答案似乎都关注的那样,而只是用户界面之一。 Management Studio 并不是一个通用/通用数据访问接口。它不是你的界面,而是你的管理区域,并且它在处理二进制数据和大型测试数据方面有严重的限制 - 因为在指定的使用配置文件中使用它的人不会遇到这个问题。

呈现大型文本数据根本不是计划的用途。

您唯一的选择是一个表值函数,它接受文本输入并为每一行剪切行,以便 Management Studio 获得行列表,而不是单行。

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.

满栀 2024-09-07 07:27:10

我更喜欢这个简单的 XML hack,它使 SSMS 中的列可以逐个单元格地单击。使用此方法,您可以在 SSMS 的表格视图中快速查看数据,并在感兴趣时单击特定单元格以查看完整值。这与 OP 的技术相同,只是它避免了 XML 错误。

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

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, '&', '&'), '<', '<') AS XML) Details
FROM Events e
WHERE 1=1
AND e.EventID BETWEEN 13920 AND 13930
;
千鲤 2024-09-07 07:27:10

从 SSMS 18.2 开始,您现在可以在网格结果中查看最多 200 万个字符。 来源

允许显示更多数据(结果到文本)并存储在单元格中
(结果到网格)。 SSMS 现在允许两者最多 200 万个字符。

我用下面的代码验证了这一点。

DECLARE @S varchar(max) = 'A'

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

SELECT @S as a

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
一个人的夜不怕黑 2024-09-07 07:27:10
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
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
漫雪独思 2024-09-07 07:27:10

来源: SqlAuthority .com

  1. 转至数据库
  2. 右键单击​​“任务”
  3. 单击“导出数据”
  4. 在向导中,选择以下选项:
    1. 来源:当前数据库
    2. 输出文件:平面文件
    3. 查询选项:编写查询
    4. 按照向导步骤操作:后续步骤

Source: SqlAuthority.com

  1. Go to Database
  2. Right click on Tasks
  3. Click on Export Data
  4. In the wizard, select the following:
    1. Source: Current database
    2. Output file: Flat file
    3. Query option: Write a query
    4. Follow wizard steps: Next steps
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文