本文介绍了换行符在 sql server 中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将错误信息输入到我数据库的 ErrorLog 表中.我有一个实用程序类来执行此操作:

I am entering error information into an ErrorLog table in my database. I have a utility class to do this:

ErrorHandler.Error("Something has broken!!\n\nDescription");

这很好用.但是,当我尝试访问此表时,换行符似乎不再存在.

This works fine. However, when I try to access this table, the line breaks no longer seem to be present.

如果我SELECT表格:

SELECT * from ErrorLog ORDER BY ErrorDate

日志中没有换行符.这是一种预期,因为一行中的换行符会破坏格式.但是,如果我将数据复制出来,则换行符丢失了,数据都在一行上.

there are no line breaks present in the log. This is kind of expected, as line breaks in one-line rows would break the formatting. However, If I copy the data out, the line break characters have been lost, and the data is all on one line.

当我输入换行符时,如何在查询结束时在数据中获得换行符?不知道是字符串进入表格时去掉了换行符,还是 SQL Server Management Studio 中的查看器去掉了换行符.

How do I get line breaks in data at the end of my query when I put line breaks in? I don't know if the string has been stripped of line breaks when it enters the table, or if the viewer in SQL Server Management Studio has stripped out the line breaks.

放入错误消息的列的数据类型是 nvarchar(Max),如果这有区别的话.

The data type of the column into which error messages are put is nvarchar(Max), if that makes a difference.

出乎意料的是,Pendri 的解决方案不起作用.

Unexpectedly, Pendri's solution didn't work.

这是字符串在传入 SQL 服务器之前的摘录:

Here is an excerpt of the string just before it passes into the SQL server:

POST /ipn/paymentResponse.ashx?installation=272&msgType=result HTTP/1.0\n\rContent-Length: 833\n\rContent-Type: 

当我从 SQL Server Management Studio 的网格查看器中提取它时,这是相同的字符串:

And here is the same string when I extract it from the grid viewer in SQL Server Management Studio:

POST /ipn/paymentResponse.ashx?installation=272&msgType=result HTTP/1.0  Content-Length: 833  Content-Type:

应该换行的地方已经加倍了.

The place where the line break should be has been double spaced.

有什么想法吗?

推荐答案

SSMS 在网格输出中用空格替换换行符.如果您使用 Print 打印值(将转到您的消息选项卡),则如果它们与数据一起存储,则回车将显示在那里.

SSMS replaces linebreaks with spaces in the grid output. If you use Print to print the values (will go to your messages tab) then the carriage returns will be displayed there if they were stored with the data.

示例:

SELECT 'ABC' + CHAR(13) + CHAR(10) + 'DEF'
PRINT 'ABC' + CHAR(13) + CHAR(10) + 'DEF'

第一个将显示在网格中的单个单元格中,没有中断,第二个将在消息窗格中中断打印.

The first will display in a single cell in the grid without breaks, the second will print with a break to the messages pane.

打印值的一种快速简便的方法是选择一个变量:

A quick and easy way to print the values would be to select into a variable:

DECLARE @x varchar(100);
SELECT @x = 'ABC' + CHAR(13) + CHAR(10) + 'DEF';
PRINT @x;

这篇关于换行符在 sql server 中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:30