line breaks lost in sql server

后端 未结 7 1493
心在旅途
心在旅途 2020-12-18 18:02

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\\nDe         


        
7条回答
  •  一整个雨季
    2020-12-18 18:38

    I echo David C's answer, except you should use the "TYPE" keyword so that you can click to open the data in a new window.

    Note that any unsafe XML characters will not work well with either of our solutions.

    Here is a proof of concept:

    DECLARE @ErrorLog TABLE (ErrorText varchar(500), ErrorDate datetime);
    INSERT INTO @ErrorLog (ErrorText, ErrorDate) VALUES
        ('This is a long string with a' + CHAR(13) + CHAR(10) + 'line break.', getdate()-1),
        ('Another long string with' + CHAR(13) + CHAR(10) + ' line break.', getdate()-2);
    SELECT
        (
            SELECT  ErrorText AS '*'
            FOR XML PATH(''), TYPE
        ) AS 'ErrorText',
        ErrorDate
    FROM        @ErrorLog
    ORDER BY    ErrorDate;
    

    I can confirm that the line breaks are preserved when copying out of a grid in SSMS 2012.

提交回复
热议问题