【发布时间】:2022-01-14 14:49:43
【问题描述】:
我正在尝试使用这样的方法将我的 csv 文件导入我的 sql 数据库,但我不知道为什么会这样说
Exception calling "ExecuteWithResults" with "1" argument(s): "Value cannot be null.
Parameter name: sqlCommands"
即使我的 csv 文件中没有 Null 值,并且我也确保我的表列接受 null 值。
$s = New-Object Microsoft.SqlServer.Management.Smo.Server "server name"
$db = $s.Databases.Item("LitHold")
$csvfile = import-csv -delimiter ";" -path "C:\scripts\LitHold-OneDrive\output\Return\2022-01-12-Return.csv"
$csvfile |foreach-object{
$query = "insert into DailyReport VALUES ('$($_.MIN)','$($_.MID)','$($_.UPN)','$($_.Change)','$($_.Type)','$($_.HoldValue)','$($_.OneDrive)','$($_.Mailbox)','$($_.Created)','$($_.Modified)','$($_.MultMID)','$($_.Account)','$($_.ExistOD)')"
}
$result = $db.ExecuteWithResults($query)
# Show output
$result.Tables[0]
我的 csv 文件
//The top one is my columns name and it's already inside my table
"MIN","MID","UPN","Change","Type","Hold Value","OneDrive","Mailbox","Created","Modified","Mult MID","Account","Exist OD"
"338780228","lzlcdg","lzlcdg@NAMQA.COM","Hold Created","OneDrive and Mailbox","Y","https://devf-my.sharepoint.com/personal/lzlcdg_namqa_corpqa_geuc_corp_com","lzlcdg@NAMQA.CORPQA.GEUC.CORP.COM","1/11/2022 11:38:57 AM","1/11/2022 11:38:57 AM","N","",""
"419150027","lzs8rl","lzs8rl@.corpq.com","Hold Created","OneDrive and Mailbox","Y","https://my.sharepoint.com/personal/lzs8rl_namqa_corpqa_gcom","lzs8rl@namqa.corpqa.geuc.corp.com","1/11/2022 11:39:05 AM","1/11/2022 11:39:05 AM","N","",""
【问题讨论】:
-
根据您发布的 csv,您的分隔符是逗号,而不是分号。
-
嗨@AbrahamZinala 感谢您指出这一点,我会准备好解决这个问题,但仍然遇到同样的错误。 :(
-
单独运行:
import-csv -path "C:\scripts\LitHold-OneDrive\output\Return\2022-01-12-Return.csv"是否返回任何内容? -
@AbrahamZinala 很奇怪,如果我只运行那个“import-csv:成员“LZ3J1G@CORPQA.GEUC.CORP.COM”已经存在,就会出现这个错误”
-
除此之外...当调用
ExecuteWithResults时,您希望从insert语句返回什么数据集?
标签: sql sql-server powershell csv powershell-2.0