SQL Server 截断和 8192 限制

发布于 2024-07-24 04:04:51 字数 371 浏览 9 评论 0原文

在 SQL Server 2005 中,我尝试查询 varchar(MAX) 列,该列的某些行的文本数据超过 8192。然而,在 Management Studio 中,我在 工具 --> 下找到了 选项 --> 查询结果--> 结果到文本 --> 每列显示的最大字符数 = 8192,这是最大值。 因此,这些行的截断似乎只是由于文本输出施加的限制而发生。

我认为解决这个问题的唯一方法是使用 SUBSTRING 函数来获取前 8000 个字符,然后是接下来的 8000 个字符,等等。但这很丑陋且容易出错。

我应该提到 SSIS 和 BCP 不是我的选择。

有人有更好的建议吗? 谢谢!

In SQL Server 2005 I am trying to query a varchar(MAX) column which has some rows with text data that exceed the 8192. Yet, In Management Studio I have under Tools --> Options --> Query Results --> Results to Text --> Max numbers of characters displayed in each column = 8192, which is a maximum. Accordingly, it seems the truncation on these rows occurs only due to the limitation imposed by text output.

The only thing I see to get around this is to use a SUBSTRING function to grab say the first 8000 chars, then the next 8000 chars etc. etc. But this is ugly and error prone.

I should mention that SSIS and BCP are not options for me.

Does anyone have a better suggestion? Thanks!

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

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

发布评论

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

评论(10

怪我太投入 2024-07-31 04:04:51

您可以将数据导出到不会被截断的平面文件。 为此:

  1. 右键单击数据库,
  2. 单击任务 -> 导出数据
  3. 选择您的数据源(默认即可)
  4. 选择“平面文件目标”作为目标类型。
  5. 为输出选择一个文件名。
  6. 在“指定表复制或查询”上,选择“编写查询以指定要传输的数据”
  7. 粘贴到您的查询中

其余步骤应该是不言自明的。 这会将文件输出为文本,您可以在您喜欢的文本编辑器中打开它。

You can export the data to a flat file which will not be truncated. To do this:

  1. Right click the Database
  2. Click Tasks -> Export Data
  3. Select your Data Source (defaults should be fine)
  4. Choose "Flat File Destination" for the Destination type.
  5. Pick a file name for the output.
  6. On the "Specify Table Copy or Query", choose "Write a query to specify the data to transfer"
  7. Paste in your query

Remaining steps should be self explanatory. This will output the file to text and you can open it in your favorite text editor.

我恋#小黄人 2024-07-31 04:04:51

我也使用 XML,但 一种稍微不同的方法,它解决了 XML 实体化的大多数问题。

declare @VeryLongText nvarchar(max) = '';

SELECT top 100 @VeryLongText = @VeryLongText + '

' + OBJECT_DEFINITION(object_id) 
FROM sys.all_objects 
WHERE type='P' and is_ms_shipped=1

SELECT LEN(@VeryLongText)

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

PRINT @VeryLongText /*WILL be truncated*/

确保 SSMS 中的“XML 数据”限制设置得足够高!

屏幕截图

I also use XML but a slightly different method that gets around most of the issues with XML entitisation.

declare @VeryLongText nvarchar(max) = '';

SELECT top 100 @VeryLongText = @VeryLongText + '

' + OBJECT_DEFINITION(object_id) 
FROM sys.all_objects 
WHERE type='P' and is_ms_shipped=1

SELECT LEN(@VeryLongText)

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

PRINT @VeryLongText /*WILL be truncated*/

Make sure that the "XML data" limit in SSMS is set sufficiently high!

Screenshot

錯遇了你 2024-07-31 04:04:51

您尝试过这个简单的解决方案吗? 只需点击 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

Just Two Clicks away!

缱倦旧时光 2024-07-31 04:04:51

我的解决方案有点迂回,但让我到达了(只要输出少于65535个字符)

  1. 在SQL Management Studio中,将网格结果的限制设置为65535(工具> 选项 > 查询结果 > SQL Server > 结果到网格 >
  2. 运行查询,输出到网格
  3. 右键单击​​结果,选择“将结果另存为...”将结果保存到文件
  4. 中 在记事本或类似工具中打开文件以获取输出

UPDATE:为了证明此操作有效,这里有一些选择单个 100,000 个字符列的 SQL。 如果我将网格输出保存到 csv 文件,则所有 100,000 个字符都在那里,没有截断。

DECLARE @test nvarchar(MAX), @i int, @line nvarchar(100)
SET @test = ''; SET @i = 100
WHILE @i < 100000
BEGIN
    SET @test = @test + STUFF(REPLICATE('_', 98) + CHAR(13) + CHAR(10), 1, LEN(CAST(@i AS nvarchar)), CAST(@i AS nvarchar))
    SET @i = @i + 100
END
SELECT @test

注意:

  1. 正如我最初所想的那样,字符长度设置似乎没有任何区别。
  2. 我正在使用 SQL 2008 R2(服务器和 Management Studio)
  3. 如果长列存储在本地变量中(如本例所示),或者从实际表中选择,似乎没有什么区别

My solution was a bit round-about but got me there (as long as the output is less than 65535 characters):

  1. In SQL Management Studio, set the limit for grid results to 65535 (Tools > Options > Query Results > SQL Server > Results to Grid > Non XML data)
  2. Run the query, output to grid
  3. Right-click the results, choose "Save Results As..." and save the results to a file
  4. Open the file in notepad or similar to get the output

UPDATE: To demonstrate that this works, here's some SQL that selects a single 100,000 character column. If I save the grid output to a csv file, all 100,000 characters are there with no truncation.

DECLARE @test nvarchar(MAX), @i int, @line nvarchar(100)
SET @test = ''; SET @i = 100
WHILE @i < 100000
BEGIN
    SET @test = @test + STUFF(REPLICATE('_', 98) + CHAR(13) + CHAR(10), 1, LEN(CAST(@i AS nvarchar)), CAST(@i AS nvarchar))
    SET @i = @i + 100
END
SELECT @test

Notes:

  1. It doesn't seem to make any difference what the character length setting is, as I orignally thought.
  2. I'm using SQL 2008 R2 (both the server and Management Studio)
  3. It doesn't seem to make a difference if the long column is stored in a local variable (as in this example), or selected from an actual table
风吹雨成花 2024-07-31 04:04:51

我在尝试导出 XML 时遇到了这个问题。 这是我使用的解决方案:

选择“结果到网格”选项,右键单击“结果”窗格中显示的链接,然后选择“将结果另存为”,选择“所有文件”文件类型,为文件命名并单击“保存”。 所有 xml 数据都正确保存到文件中。

我正在使用 SSMS 10,但我无法让 Torre 的解决方案发挥作用。 导出向导一直认为输入列是图像:

“输入列“XML_F52E2B61-18A1-11d1-B105-00805F49916B”(26)”的数据类型是 DT_IMAGE

I ran in to this trying to export XML. This is the solution I used:

Select the Result to Grid option, right click the link that shows up in the Results pane, then select Save Results As, choose the All Files file type, give the file a name and click Save. All the xml data is saved correctly to a file.

I'm using SSMS 10, and I could't get Torre's solution to work. The export wizard kept thinking the input column was an image:

The data type for "input column "XML_F52E2B61-18A1-11d1-B105-00805F49916B" (26)" is DT_IMAGE

脱离于你 2024-07-31 04:04:51

在 SSMS 中,如果您从一行中选择数据,则它仅限于少量字符,但如果您从一行中编辑数据,则完整值将在那里。 它可能并不总是在那里,但如果你按 ctrl-a、ctrl-c 然后在编辑器中将其过去,它就会全部在那里。

In SSMS if you select data from a row it is limited to a small number of characters, but if you Edit data from a row, the full value will be there. It might not always be there but if you ctrl-a, ctrl-c then past it in an editor it will all be there.

满意归宿 2024-07-31 04:04:51

如果给定一个选择,我会让查询将数据返回为“For XML Auto”或“For XML Raw”或“For XML Explicit”,这样限制会更高,并且您可以对输出的结果执行更多操作。

If given a choice I would have the query return the data as "For XML Auto" or "For XML Raw" or "For XML explicit" that way the limitations are much higher and you can do much more with the outputed results.

十年不长 2024-07-31 04:04:51

我通常使用 XML 来获取巨大的调试字符串作为输出(使用 Luke 的测试工具):

    declare @test nvarchar(max), @i int, @line nvarchar(100)
    set @test = ''; set @i = 100
    while @i < 100000
    begin
        set @test = @test + stuff(replicate('_', 98) + char(13) + char(10), 1, len(cast(@i as nvarchar)), cast(@i as nvarchar))
        set @i = @i + 100
    end
    -- ctrl+d for "results to grid" then click the xml output
    --select cast('<root>' + @test + '</root>' as xml)

-- revised
select @test for xml path(''), type;

I usually use XML to get huge debug string as output (using test harness from Luke):

    declare @test nvarchar(max), @i int, @line nvarchar(100)
    set @test = ''; set @i = 100
    while @i < 100000
    begin
        set @test = @test + stuff(replicate('_', 98) + char(13) + char(10), 1, len(cast(@i as nvarchar)), cast(@i as nvarchar))
        set @i = @i + 100
    end
    -- ctrl+d for "results to grid" then click the xml output
    --select cast('<root>' + @test + '</root>' as xml)

-- revised
select @test for xml path(''), type;
感悟人生的甜 2024-07-31 04:04:51

另一种解决方法是使用 HeidiSql 来处理这个棘手的查询。 它没有字段长度的限制。

Another workaround , use HeidiSql for this tricky queries. It does not have the limits in the field lenght.

鸠书 2024-07-31 04:04:51

您所说的截断仅发生在 Management Studio 中。 如果您将该列拉入另一个应用程序,它不会被截断。

您无法使用查询分析器与 SQL Server 2005 对话。您是说 Management Studio 吗?

The truncation you are talking about only happens in Management Studio. If you pull the column into another app, it will not be truncated.

There's no way you're using Query Analyzer to talk to SQL Server 2005. Do you mean Management Studio?

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