为什么bcp在列包含空字符串时输出null,在列为null时输出空字符串?
这让我觉得非常奇怪的行为,我花了一段时间检查代码中的错误,然后才发现
“从数据库表或视图到文件的副本。如果指定现有文件,该文件将被覆盖。提取数据时,请注意 bcp 实用程序将空字符串表示为 null,将 null 字符串表示为空字符串。” (来自 http://msdn.microsoft.com/en-us/library/ms162802 .aspx)
显然这让我解决了我的问题,但是有人能想到或者有人知道为什么会出现这种情况吗?
This struck me as really weird behaviour and I spent a while checking for bugs in my code before I found this
"out copies from the database table or view to a file. If you specify an existing file, the file is overwritten. When extracting data, note that the bcp utility represents an empty string as a null and a null string as an empty string." (from http://msdn.microsoft.com/en-us/library/ms162802.aspx)
Obviously this allowed me to fix my problem but can anybody think of or does anybody know a reason why this is the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已经有一段时间了,但我确信这是 SQL Server 6.5 的向后兼容性/遗留问题
SQL Server 6.5 无法存储空字符串:总是有一个空格。 SQL 7 改变了这一点,
所以
'' -> NULL
和' ' -> ''
从古代历史的角度来看是正确的。It's been some time, but I'm sure it's a backwards compatibility/legacy back to SQL Server 6.5
SQL Server 6.5 could not store empty string: there was always one space. This changed with SQL 7
So
'' -> NULL
and' ' -> ''
is correct from an ancient history perspective.发送
null
代替空。我在这里找到了最好的解决方案:
https ://bytes.com/topic/sql-server/answers/143738-bcp-inserting-blank-space-empty-string
Send
null
in place of empty.I find the best solution here:
https://bytes.com/topic/sql-server/answers/143738-bcp-inserting-blank-space-empty-string
这与 BCP 的“默认值”部分相关:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or -use-default-values-during-bulk-import-sql-server
您必须回想一下从其他奇怪的系统导入纯文本文件的日子。 BCP 将
''
翻译为“未定义”(=缺少数据)并在数据库中设置 NULL(=缺少数据)。对于其他系统,数据库中 NULL 的另一种方式必须是''
。要从数据库中获取“真实”数据,请使用
-k
开关:https://learn.microsoft.com/en-us/sql/关系数据库/导入导出/keep-nulls-or-use-default-values-during-bulk-import-sql-server#keep_nulls
然后你的文件/数据库中就有 ASCII 0x0。
This is related to the "default values" section for BCP:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server
You have to think back to days where importing plain text files from other weird systems. BCP translates
''
as "not defined"(=missing data) and sets a NULL in the database (=missing data). The other way around a NULL from database must be a''
for the other systems.To get the 'real' data out of the database use the
-k
switch:https://learn.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server#keep_nulls
Then you have your ASCII 0x0 in your file/database.