SQL Server 可以 bcp 包含 Unix 行结尾的文件吗?

发布于 2024-08-06 14:51:28 字数 196 浏览 6 评论 0原文

我正在尝试使用 SQL Server bcp 实用程序从 samba 共享导入文本文件。 bcp 在 Unix 行结尾处令人窒息。我确信我可以在 Unix 或 Windows 上添加一个中间步骤,将行结尾更改为 Windows 样式。但我更愿意从 Unix 导入文件而不进行修改。

有人知道是否有办法告诉 SQL Server bcp 查找 Unix 行结尾吗?

I'm trying to use the SQL Server bcp utility to import a text file from a samba share. bcp is choking on the Unix line endings. I'm sure I could add an intermediate step, either on Unix or Windows, to change the line endings to Windows-style. But I would prefer to import the files from Unix without modification.

Anybody know if there's a way to tell SQL Server bcp to look for Unix line endings?

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

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

发布评论

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

评论(7

或十年 2024-08-13 14:51:28

简单的答案是使用十六进制,正如评论之一中提到的,作为行终止符:

-r 0x0a

The simple answer is to use hex, as was mentioned in one of the comments, as the row terminator:

-r 0x0a
拒绝两难 2024-08-13 14:51:28

您必须使用 bcp 格式文件并将终止符指定为 \n。交互式命令行将始终附加 \r,其中格式文件将完全使用您指定的内容。参考 http://www .eggheadcafe.com/software/aspnet/32239836/bcp-out-with-char10-as-row-terminator.aspx

创建格式文件在 BOL 中解释得很好,但如果您需要帮助,请评论/更新您的原始帖子。

You have to use a format file with bcp and specify the terminator as \n. The interactive command line will always append \r, where a format file will use exactly what you specify. Reference http://www.eggheadcafe.com/software/aspnet/32239836/bcp-out-with-char10-as-row-terminator.aspx.

Creating a format file is explained pretty well in BOL but comment/update your original post if you need help.

情何以堪。 2024-08-13 14:51:28

您是否尝试设置 ROWTERMINATOR = '\n'?

have you tried to set the ROWTERMINATOR = '\n'?

风情万种。 2024-08-13 14:51:28

我认为您无法从 bcp 命令行执行此操作。但是,我认为下面的 SQL 版本可以工作。

DECLARE @Command nvarchar(1000)

SET @Command = N'BULK INSERT MyTable
FROM ''<path\file>''
WITH (ROWTERMINATOR = '''+CHAR(10)+''')'

exec sp_executeSQL @Command

I don't think you can do this from the bcp command line. But, I think the following SQL version will work.

DECLARE @Command nvarchar(1000)

SET @Command = N'BULK INSERT MyTable
FROM ''<path\file>''
WITH (ROWTERMINATOR = '''+CHAR(10)+''')'

exec sp_executeSQL @Command
哽咽笑 2024-08-13 14:51:28

通过将进程输出呈现为文件的替代解决方案

还有另一种方便的方法来解决此问题。如果您想上传简单的 CSV,对于更复杂的情况,我的解决方案很好 写入格式文件

使用十六进制代码进行换行(-r 0x0a)也很方便,但对于 Ubuntu 18.04 上的 BCP 版本 17.6.0001.1 来说,它对我不起作用。

所以我使用了一个小技巧:

sed 替换 unix 行尾,并将其输出流作为文件呈现。代码的相关部分:

<(< iris.csv sed 's/\r*$/\r/')

如下所示,sed 的输出以文件形式呈现:

ls <(< iris.csv sed 's/\r*$/\r/')

输出:

/proc/self/fd/11

整个示例

我在工作中有 iris.csv目录:

>  head -n 5 iris.csv
sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa

我可以使用 bcp 加载到 MS SQL,如下所示:

query=$(cat << EOF
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='iris' and xtype='U')
    CREATE TABLE iris (
        sepal_length FLOAT NOT NULL,
        sepal_width  FLOAT NOT NULL,
        petal_length FLOAT NOT NULL,
        petal_width  FLOAT NOT NULL
    )
GO
EOF
)

sqlcmd \
    -S localhost,31433 \
    -E \
    -d "testdb" \
    -Q "$query"

bcp \
    iris in <(< iris.csv sed 's/\r*$/\r/') \
    -S localhost,31433 \
    -T \
    -d "testdb" \
    -n \
    -t ","

注意

  1. 使用此语法将进程输出流呈现为文件是一种 bashism,因此不适用于某些其他 shell,例如 dash。它将与 zsh 一起使用

  2. 我使用 Windows 集成身份验证进行 AD,是的,在 Linux 上可以使用 kerberos 来完成此操作。这就是 sqlcmd-E 标志和 bcp-T 标志的原因。

Alternative solution by presenting process output as file

There is an other convenient way to solve this. My solution is good if you want to upload a simple CSV, for more complex cases write a format file.

Using hex code for linefeed (-r 0x0a) would be also convenient, but it did not work for me for BCP version 17.6.0001.1 on Ubuntu 18.04.

So I used a little trick instead:

Replace unix line-endings with sed, and present its output stream as a file. The relevant part of the code:

<(< iris.csv sed 's/\r*$/\r/')

As you can below see the output of sed is presented as a file:

ls <(< iris.csv sed 's/\r*$/\r/')

output:

/proc/self/fd/11

A whole example

I have the iris.csv in the working directory:

>  head -n 5 iris.csv
sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa

I can load into MS SQL using bcp like this:

query=$(cat << EOF
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='iris' and xtype='U')
    CREATE TABLE iris (
        sepal_length FLOAT NOT NULL,
        sepal_width  FLOAT NOT NULL,
        petal_length FLOAT NOT NULL,
        petal_width  FLOAT NOT NULL
    )
GO
EOF
)

sqlcmd \
    -S localhost,31433 \
    -E \
    -d "testdb" \
    -Q "$query"

bcp \
    iris in <(< iris.csv sed 's/\r*$/\r/') \
    -S localhost,31433 \
    -T \
    -d "testdb" \
    -n \
    -t ","

Notice

  1. presenting process output stream as a file with this syntax is a bashism so will not work with some other shells such as dash. It will work with zsh

  2. I used windows integrated authentication for AD, yes on Linux which can do this with kerberos. Thats why the -E flag for sqlcmd and the -T flag for bcp.

司马昭之心 2024-08-13 14:51:28

如果您没有太多时间详细研究 bcp,请查看以下内容:
http://msdn.microsoft.com/en-us/library/ms190759。 aspx

它将为您提供简单的示例,解释交互式提示的含义,完成后保存格式的选项(如果您要重复执行此操作)等等。

如果您的数据很大和/或您有如果您想要几个字段,您可以先制作一个表格,然后进行一些尝试导出(bcp 将采用简单的选择作为第一个参数),并且仍然以交互方式逐列选择格式。如果您有一些额外的原因,您可以稍后深入查看保存的 fmt 文件。

If you don't have a lot of time to study bcp in great detail, check out this one:
http://msdn.microsoft.com/en-us/library/ms190759.aspx

It will give you easy example, explain what interactive prompts mean, option to save format once you are done (if you are going to do this repeatedly) etc. etc.

If your data is big and/or you have several filds you'd like, you can make a table first then do a little trial export (bcp will take a simple select as first arg) and still pick formats interactively, column by column. You can dig into saved fmt file latter if you have some extra reason for that.

贱贱哒 2024-08-13 14:51:28

是的,这太令人抓狂了。我的理解是,SQL Server bcp 总是在您希望使用的任何行终止符之前插入 \r。因此,如果您不使用 -r,您会期望它仅使用 \n。但它没有......它愚蠢地插入 \r 以便它可以使用 \r\n 。如果指定 -r \r\n 那么它仍然不起作用;我怀疑是因为它现在想要 \r\r\n 行结束符。这都是一些为 Windows 世界编写的白痴所做的工作,试图让初学者的生活变得更轻松,但最终却让其他人几乎不可能完成任务。我在将文件从 Sybase 传输到 SQL Server 时遇到了这个问题,解决方案是在 Sybase 的 bcp 输出中指定 -r \r\n (其工作原理与您要求的完全一样!)和 -r \n (或者只是不指定)请勿对 SQL Server bcp 使用 -r)。

Yes, this is maddening. My understanding is that SQL Server bcp ALWAYS inserts a \r before whatever line terminator you would expect to be used. So, if you don't use -r, you would expect it to use \n only. But it doesn't...it stupidly inserts \r so that it can use \r\n. If you specify -r \r\n then it still won't work; I suspect because it now wants \r\r\n line ends. This is all the work of some idiot coding for the Windows world trying to make life easier for beginners and ending up making things nigh on impossible for everyone else. I experienced this problem when transferring files from Sybase to SQL server and the solution was to specify -r \r\n in the bcp out from Sybase (which works exactly as you ask it to!) and -r \n (or just don't use -r) for the SQL Server bcp in.

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