gpt4 book ai didi

sql-server - 使用sql中的批量插入将文本文件的行号存储在表中

转载 作者:行者123 更新时间:2023-12-02 19:38:12 25 4
gpt4 key购买 nike

我的文件中有这样的文本:

In the name of God, the Mercy-giving, the Merciful! (1)
Praise be to God, Lord of the Universe, (2)
the Mercy-giving, the Merciful (3)
Ruler on the Day for Repayment! (4)
...

然后,一个包含 2 个字段的表,如下所示:

TextData Varchar(max) NoNullable
Id Int NoNullable Identity(1,1)

何时使用此查询:

bulk insert MyDb.dbo.translation 
FROM 'd:\IRVING.Arb'
WITH
(
ROWTERMINATOR ='\n',
codepage='1256'
)

(0 行受到影响)

但是,当我从表中删除 Id 列时,所有行数据都会复制它。

如何在 Id 列中存储文件的行号?

最佳答案

这个文件的格式非常具体,你必须指定一个称为“格式文件”的功能,以便 SQL Server 知道你的文件的映射(文件的哪一部分属于哪个 sql 列)

你可能会使用这样的东西:

C:\test_format.fmt :

9.0
4
1 SQLCHAR 0 100 "(" 1 Text
2 SQLINT 0 12 ")\r\n" 2 Id

将此添加到您的批量插入中:

bulk insert MyDb.dbo.translation 
FROM 'd:\IRVING.Arb'
WITH
(
codepage='1256',
FORMATFILE = 'C:\test_format.fmt'
)

在此处了解有关格式文件的更多信息:http://msdn.microsoft.com/en-us/library/ms178129.aspx

关于sql-server - 使用sql中的批量插入将文本文件的行号存储在表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com