使用Openrowset导入错误 [英] Import error using Openrowset

查看:128
本文介绍了使用Openrowset导入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入具有数字和字母数字值的列(从.csv文件),但是当我运行openrowset过程时,它会正确导入数字行,但是对于字母数字值,它默认为null.

I am trying to import a column (from .csv file) with numeric and alphanumeric values but when I run the openrowset procedure it imports the numeric rows properly but for alphanumeric values it defaults to null.

表A

ID,A,B,C
1,12,hh,i
2,ab12,tt,b
3,2,aa,o
4,bc12,ee,l

使用的代码

SELECT 
    * 
FROM 
    OPENROWSET
        (
            'Microsoft.ACE.OLEDB.12.0','Text;Database=C:\;IMEX=1;','SELECT * FROM abc.csv'
        ) t

我使用了IMEX =1,完全没有变化.

I used IMEX =1 and no change at all.

推荐答案

问题原因是Oledb提供程序

当导入具有混合数据类型列的csv文件或excel文件时,它将用null替换非主要类型. (使用Oledb或Ace.Oledb )

When importing csv file or excel files with mixed data types column it will replace non dominant types by null. (Using Oledb or Ace.Oledb)

解决方法

您可以通过添加包含字符串值的第一行,然后在完成提示后将其删除来做一些解决方法

You can do some workaround by adding a first row that contain string values then removing it after impirting is finish

ID,A,B,C
0,a,a,a
1,12,hh,i
2,ab12,tt,b
3,2,aa,o
4,bc12,ee,l

如果使用IMEX=1

这将读取列作为字符串,将ID列读取为数字. (使用0)

This will read columns as string and ID column as number. (0 is used)

或在连接字符串中添加HDR=NO属性,以便标头是导入的第一行(所有值均为字符串)

Or add HDR=NO property to connection string so the header is the first line imported (all its values are strings)

在此文章

其他方法

或者尝试在没有aceoledb提供程序的情况下实现此目的,只需以其他方式导入csv文件,如下所示:

Or try to achieve this without aceoledb provider just import csv file in other way like the following:

使用Microsoft文本驱动程序

SELECT * FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=C:\;',
'SELECT * FROM abc.csv')

使用大容量插入

CREATE TABLE dbo.MyTable 
(ID INTEGER,
 A VARCHAR(50),
 B VARCHAR(50),
 C VARCHAR(50)
)

BULK INSERT dbo.MyTable
FROM 'C:\abc.csv'
WITH 
  (
    FIELDTERMINATOR = ',', 
    ROWTERMINATOR = '\n' 
  )

这些文章中的其他详细信息:

Other Details in these articles:

  • How to import csv file with OPENROWSET?
  • T-SQL – Read CSV files using OpenRowSet (A detailed tutorial)

这篇关于使用Openrowset导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆