有没有办法强制将截断的列插入到 SQL Server 表中?
数据以电子表格的形式提供给我,我将其转换为管道分隔的文件。我尝试插入超过 46000 行,但其中四 (4) 行失败并显示错误消息
SQLState = 22001,NativeError = 0 错误 = [Microsoft][SQL Native Client]字符串数据,右截断
如果能知道哪些行失败就好了。有没有办法可以强制将这些行与截断的字符串一起插入,或者确定哪些行在 BCP 中失败?
- 我尝试过构建 在 XLSX 文件中插入语句 与连接。那失败了,因为 我的参数太多了 连接。
- 我已经尝试过逗号 - 和 制表符分隔的文件,失败 因为有些数据有那些 人物。
- 我终于构建了管道分隔 更改后的文件 本地化设置,这样我就可以做 使用 Excel(如何保存 Excel 电子表格为 管道分隔),然后使用 MyCreatedCSVFile.csv -T -c -t^| 中的
bcp TableName 将它们导入到表中
<- 我不得不逃离|命令行中带有 ^ 的字符。
有更好的方法吗?
Data are given to me in a spread sheet which I'm converting to pipe-delimited files. I tried inserting over 46000 rows but four (4) of them failed with the error message
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Native Client]String data, right truncation
It would be nice to know which rows fail. Is there a way I can either force these rows to be inserted with the truncated strings, or to determine which ones fail with BCP?
- I have tried constructing the the
insert statements in the XLSX file
with CONCATENATE. That fails because
I have too many parameters for
CONCANTENATE. - I have tried comma- and
tab-delimited files, which fail
because some of the data have those
characters. - I finally constructed pipe-delimited
files after changing the
localization settings so I can do
this with Excel (How to save an
Excel spreadsheet as
pipe-delimited), and then importing them into the table withbcp TableName in MyCreatedCSVFile.csv -T -c -t^|
<- I had to escape the | character on the command-line with ^.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用临时表吗?
您通常会使用具有足够长的色谱柱等的色谱柱,然后执行验证和质量检查。即使是一个简单的
存储过程也可以用来执行批量插入,然后进行一些检查,然后插入到最终表中。
Are you using a staging table?
You'd normally use one with generous column lengths etc then perform validation and quality checks. Even a simple
A stored proc can be used too to do a BULK INSERT then some checks then an INSERT into the final table.
您可以将错误数量设置得足够高以覆盖异常,然后指定错误文件。然后,您一次处理一个异常。这是最干净的方法。如果您愿意使用 SSIS,您可以在工作流程中更加精细并处理截断和日志记录,但您会失去批量复制的好处。
You can set the number of errors high enough to cover the exception and then specify an error file. You then handle the exceptions one at a time. This is the cleanest method. If you are willing to use SSIS, you can get more elaborate in your workflow and handle the truncation and perhaps logging, but you lose the benefit of bulk copying.
我设法通过创建“而不是”触发器来做到这一点。
我用 varchar(max) 创建了一个表,以确保我没有得到截断。
然后,我在插入上创建了一个替代触发器(因此这个“代理”表永远不会获取行)。
在触发器中,我执行转换(varchar(50),name255)。
即:明确地进行截断。
但是,这意味着您需要使用 BCP 命令指定“FIRE_TRIGGER”。
这有什么帮助。
I managed to do this by creating an "Instead of" trigger.
I created a table with varchar(max) to ensure I didn't get the truncates.
I then created an instead-of trigger on the insert (so this 'proxy' table never get rows).
In the trigger I do a convert(varchar(50),name255).
Ie: doing the truncate explicitly.
However, this means that you need to specify "FIRE_TRIGGER" with your BCP command.
How this helps.