简体   繁体   English

CSV从SQL View导出失败-导出后换行

[英]CSV Export failure from SQL View - line breaks after export

I have a SQL view which contains a column with concatenated text. 我有一个SQL视图,其中包含带有串联文本的列。

This text comes from a function: 这段文字来自一个函数:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[FC01_FunctionName]
    (@OrderNr VARCHAR(200)) 
RETURNS VARCHAR(2000)
BEGIN
    declare @r varchar(2000)
    declare @z varchar(2000)

    DECLARE cc CURSOR FOR
        SELECT SL.Description 
        FROM dbo.[DBName] SL
        WHERE x = @OrderNr AND y = 1

    SET @r = ''

    OPEN cc

    WHILE ( 0 = 0 ) 
    BEGIN
        fetch next from cc into @z

        if (@@fetch_status <> 0) 
            break

        if ( @r = '' ) 
        begin
            set @r = @z
        end
        else
        begin
            set @r = @r + char(13) + char(10) + @z 
        end
    end

    close cc
    deallocate cc

    return @r
end

The result looks like: 结果看起来像:

在此处输入图片说明

As you can see sometimes it's just 'one' text-element like 'Call 24 hrs. 如您所见,有时它只是“一个”文本元素,例如“ 24小时通话”。 ...' and sometimes there are multiple text elements like ' * Item No ... ...”,有时会有多个文本元素,例如“ *项目编号... Please ...* ...' and so on. 请... * ...',依此类推。

Every time I export the view to a .csv file, I got problems with rows which have more than one text element. 每次将视图导出到.csv文件时,都会遇到包含多个文本元素的行的问题。 It results in a new line for every additional text element and destroys the format (all the yellow marked text should be in one field -> the field with the text '***Item ...'): 它会为每个其他文本元素换行,并破坏格式(所有黄色标记的文本应在一个字段中->文本为'*** Item ...'的字段):

在此处输入图片说明

After the last (yellow marked) text-element the normal entries go on. 在最后一个(标记为黄色)文本元素之后,正常的输入继续进行。

The properties of the job which does the SQL view to .csv export are the following: 将SQL视图导出到.csv的作业的属性如下:

sqlcmd -W -s ";"  -E  -d master -Q "SET NOCOUNT ON; SELECT *  FROM [DB].[dbo].[ViewName];" -o "c:\Temp\XXX.csv"

I believe that the export function thinks that the text-elements are separate and thats maybe the reason why the functions decides to spend a new line for every element. 我认为导出函数认为文本元素是分开的,这也许就是函数决定为每个元素花费新行的原因。 I also believe that I have to change something in the sql function which creates the result for the field or maybe in the view itsself which gets the data. 我也相信我必须在sql函数中更改某些内容,该函数会为字段创建结果,或者在视图本身中获取数据。

This is the part of the sql query in the view which gets the data: 这是获取数据的视图中sql查询的一部分:

(SELECT [dbo].[FC01_FunctionName] (  x.[FieldName]) ) as [ColumnName],

Any ideas? 有任何想法吗? Thanks for you help! 感谢您的帮助!

Take a look at your text to columns settings in Excel. 查看您在Excel中的“文本到列”设置。 It may be that the csv export is correct (open in notepad to check) but Excel splits the lines as the file is opened. 可能是csv导出正确(在记事本中打开以进行检查),但是Excel在打开文件时会拆分行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM