我有一个巨大的CSV文件,其中包含很多空白值,例如:
VALUE1,VALUE2,VALUE3,VALUE4,VALUE5,VALUE6,,VALUE8,VALUE9,VALUE10 ,,
我正在尝试通过PS脚本导入。为此,我正在使用以下powershell脚本
$Server = 'server_value'
$Database = 'db_value'
$Username = 'usr_value'
$Password = 'pass_value'
$table = 'table_value'
$CSVFilePath = "csv_file_path.csv"
$DbQuery = "BULK INSERT $table FROM '$CSVFilePath' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR='\n')"
# Create SQL connection
$DbConnection = New-Object System.Data.SqlClient.SqlConnection
$DbConnectionString = "Server = $Server; Database = $Database; User ID=$Username; Password=$password; pooling=false;"
$DbConnection.ConnectionString = $DbConnectionString
$DbConnection.Open()
# Create SQL command
$DbCommand = New-Object System.Data.SQLClient.SQLCommand
$DbCommand.Connection = $DbConnection
$DbCommand.CommandText = $DbQuery
# Execute
$DbCommand.ExecuteNonQuery()
# Close connection
$DbConnection.Close()
我得到的结果是:
COLUMN01 | COLUMN02 | COLUMN03 | COLUMN04 | COLUMN05 | COLUMN06 | COLUMN07 | COLUMN08 | COLUMN09 | COLUMN10 | COLUMN11 | COLUMN12 | COLUMN13 | COLUMN14
VALUE001 | VALUE002 | VALUE003 | VALUE004 | VALUE005 | VALUE006 | NULL | VALUE008 | VALUE009 | VALUE010 | VALUE011 | NULL | NULL | NULL
我真正想要的是:
COLUMN01 | COLUMN02 | COLUMN03 | COLUMN04 | COLUMN05 | COLUMN06 | COLUMN07 | COLUMN08 | COLUMN09 | COLUMN10 | COLUMN11 | COLUMN12 | COLUMN13 | COLUMN14
VALUE001 | VALUE002 | VALUE003 | VALUE004 | VALUE005 | VALUE006 | | VALUE008 | VALUE009 | VALUE010 | VALUE011 | | |
我已经在空值上尝试使用“单”和“双”引号:
VALUE1,VALUE2,VALUE3,VALUE4,VALUE5,VALUE6,'',VALUE8,VALUE9,VALUE10,'','','',''
或:
VALUE1,VALUE2,VALUE3,VALUE4,VALUE5,VALUE6,“”,VALUE8,VALUE9,VALUE10,“”,“”,“”,“”
我得到的是:
COLUMN01 | COLUMN02 | COLUMN03 | COLUMN04 | COLUMN05 | COLUMN06 | COLUMN07 | COLUMN08 | COLUMN09 | COLUMN10 | COLUMN11 | COLUMN12 | COLUMN13 | COLUMN14
VALUE001 | VALUE002 | VALUE003 | VALUE004 | VALUE005 | VALUE006 | '' | VALUE008 | VALUE009 | VALUE010 | VALUE011 | '' | '' | ''
或:
COLUMN01 | COLUMN02 | COLUMN03 | COLUMN04 | COLUMN05 | COLUMN06 | COLUMN07 | COLUMN08 | COLUMN09 | COLUMN10 | COLUMN11 | COLUMN12 | COLUMN13 | COLUMN14
VALUE001 | VALUE002 | VALUE003 | VALUE004 | VALUE005 | VALUE006 | "" | VALUE008 | VALUE009 | VALUE010 | VALUE011 | "" | "" | ""
还尝试在批量插入上对 KEEPNULLS 进行如下操作:
BULK INSERT $table
FROM '$CSVFilePath'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR='\n',
KEEPNULLS
)
但是没有用。
答案 0 :(得分:0)
批量插入时,选择空值的方式是两件事之一。
或
选项2的问题是...如果未指定任何文字,则该列的默认值为NULL。意味着在表上没有定义默认值的情况下,您的选项为NULL或NULL或KEEPNULL(也为NULL)。
所以...,您唯一真正的选择是更改表,并在要执行此操作的所有列上将一个空字符串定义为默认值。
在此处查看示例: