XML 输出在 SQL 中被截断

发布于 2024-08-30 20:21:58 字数 588 浏览 1 评论 0原文

我需要以 XML 形式返回结果集,这工作正常,但如果记录数增加,我的 xml 输出会被截断,这是我的查询,

select t.id,t.name,t.address from test FOR XML AUTO, ROOT('Response'), ELEMENTS

但是我设置了一些选项来增加输出结果设置为..

Tools --> Options --> Query Results --> SQL Server --> Results to Text --> Maximum              
number of characters displayed in each column
Tools --> Options --> Results --> Maximum characters per column

但我仍然无法得到我想要的结果。

请建议我的解决方案

编辑:但是,当我单击 XML 时,它会在另一个查询窗口中打开,并且此处显示所有 xml,但它不会返回所有 xml 而不是截断的 xml。

谢谢....

I need to return my result set in XML and this works fine, but if the number of records are increased, my xml output is truncated here is my query

select t.id,t.name,t.address from test FOR XML AUTO, ROOT('Response'), ELEMENTS

However I have set some option to increase the output result set like..

Tools --> Options --> Query Results --> SQL Server --> Results to Text --> Maximum              
number of characters displayed in each column
Tools --> Options --> Results --> Maximum characters per column

but still I am unable to get my desired result.

please suggest my solution

EDIT: However when I click on the XML, it open in another query window and all xml shown here, but it not return all xml instead of truncated xml.

Thanks....

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

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

发布评论

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

评论(9

请别遗忘我 2024-09-06 20:21:58

尝试以下操作:

Declare @xmldata xml
set @xmldata = (select ... From test for xml...)
select @xmldata as returnXml

Try the following:

Declare @xmldata xml
set @xmldata = (select ... From test for xml...)
select @xmldata as returnXml
半葬歌 2024-09-06 20:21:58

我在 Management Studio 和 xml 结果集上也遇到了同样的问题。我通过使用 C# 和 SqlDataReader 以编程方式获取输出解决了这个问题。

I've had the same problem with management studio and xml result sets. I've solved this by getting the output programatically with c#, with SqlDataReader.

∞觅青森が 2024-09-06 20:21:58

这对我有用(SSMS 2012):
工具>选项>查询结果> SQL服务器>网格结果:重试的最大字符数:XML 数据:无限制。
然后我将网格结果保存到文件中。

This worked for me (SSMS 2012):
Tools > Options > Query Results > SQL Server > Results to Grid: Maximum Characters Retried: XML data: Unlimited.
Then I saved the results of the grid to a file.

零時差 2024-09-06 20:21:58

我有同样的问题。我将查询结果设置更改为“无限制”,但它不起作用。有一个解决方法。它不是很简洁,但如果您确实想继续使用 SQL Server Management studio 并获得结果。调整查询以转换存储的 xml:

convert(xml,'<xml><![CDATA[' + cast(MyColumnWithXml as varchar(max)) + ']]></xml>') 

返回完整文本。请注意,您最后必须进行一些格式化

I had the same issue. I changes my Query result setting to Unlimited, but it didnt work. There is a work around. Its not very neat, but if you do want to continue to use SQL Server Management studio and get the results. Tweak the query to convert the xml stored using:

convert(xml,'<xml><![CDATA[' + cast(MyColumnWithXml as varchar(max)) + ']]></xml>') 

The complete text is returned. Please note you will have to do some formatting at you end

情释 2024-09-06 20:21:58

只是想我会在上面的答案中添加一个快速补充。更改 SSMS 2017 工具 > 中的设置选项>查询结果> SQL服务器>结果到网格有效。但是,您必须完全关闭 SSMS 并重新打开它才能使设置生效。

Just thought I'd add a quick addition to the answer above. Changing the settings in SSMS 2017 Tools > Options > Query Results > SQL Server > Results to Grid works. However you must then close SSMS entirely and re-open it for the setting to take effect.

℡Ms空城旧梦 2024-09-06 20:21:58

如果更改 SSMS 选项后 XML 结果集仍然被截断,请在运行 XML 语句之前使用带有 :XML ON 选项的 SQLCMD 实用程序。使用 -o 开关将输出定向到文件。 SQLCMD 是一个使用非常简单的命令行实用程序。请参阅 http://msdn.microsoft.com/en-us/library/ms162773。 .aspx.

If your XML result set is still being truncated even after changing SSMS Options, use the SQLCMD utility with :XML ON option prior to running your XML statement. Direct your output to a file using the -o switch. SQLCMD is a command line utility that is very simple to use. See http://msdn.microsoft.com/en-us/library/ms162773.aspx.

零度℉ 2024-09-06 20:21:58

我知道这个问题是10年前提出的,但如果有人正在寻找一种不需要数据库端进行任何更改并且与已接受的答案有点相似但不具有的解决方法SqlDataReader

您可以使用DataTable 并使用SqlDataAdapter 填充它。您的 XML 数据将被分为多行。

例如

string xmloutput = "";
var cmd = new SqlCommand("<some sql query that returns data in xml format>", con);
var da = new SqlDataAdapter(cmd);
var dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
    foreach (DataRow dr in dt.Rows)
    {
        xmloutput  += dr[0].ToString();
    }

注意:

  • 上面的代码假设从 DB 返回的只有一列,其中包含 XML 数据。

然后,使用变量 xmloutput

I know this question was asked like 10 Years ago, but still if someone is looking for a workaround which doesn't require any changes in the DB side and is a bit similar to the Accepted answer but not with SqlDataReader.

You could make use of a DataTable and fill it using the SqlDataAdapter. Where your XML data will be divided into multiple rows.

For Example

string xmloutput = "";
var cmd = new SqlCommand("<some sql query that returns data in xml format>", con);
var da = new SqlDataAdapter(cmd);
var dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
    foreach (DataRow dr in dt.Rows)
    {
        xmloutput  += dr[0].ToString();
    }

Note:

  • the above code assumes that the only one column is returned from DB with XML data in it.

And later on, make use of the variable xmloutput.

笙痞 2024-09-06 20:21:58

您在查询输出窗口中获得的 XML 输出并不是真正用于保存
有效的 XML 文档。当您执行以下操作时,您可以从 SQL Server 获取有效的 XML:
使用 .NET 中的 ADO 或 SQLXML 托管类将其捕获为流

但是,您可以通过使用来限制查询输出大小
在 SQL Server Management Studio 中,转到“工具”>>选项>>查询结果>> SQL Server>>结果到文本>>每列显示的最大字符数

The XML output you get in Query output window is not really intended for saving
valid XML documents. You can get valid XML out of SQL Server when you
capture it as a stream using ADO or SQLXML managed classes in .NET

However you can limit the query output sizes by using
In SQL Server Management Studio go to Tools >> Options >> Query Results >> SQL Server >> Results to Text >> Maximum number of characters displayed in each column

多情出卖 2024-09-06 20:21:58

您可以针对 SQL Server 2005 数据库引擎使用 SQL Management Studio for 2008。如果这样做,默认值应该足够大,但在“工具”->“工具”中选项->查询结果-> SQL Server 有一个“结果到网格”和“结果到文本”节点。

结果到网格允许您显式控制 XML 数据的最大大小,默认最大大小为 2MB。

如果您没有 SSMS 2008 的许可证,您应该可以免费使用 Express 版本 此处

You can use SQL Management Studio for 2008 against a SQL Server 2005 database engine. If you do that, the defaults should be large enough, but in Tools -> Options -> Query Results -> SQL Server there is a Results to Grid and Results To Text node.

Results to Grid allows you to control the maximum size for XML data explicitly, and the default maximum size is 2MB.

If you don't have a license for SSMS 2008 you should be able to use the express edition for free here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文