菜鸟在这里。这对我来说是一个涉及 Powershell 和 SQL 的学习练习。
我正在编写一个脚本,该脚本从 Excel 电子表格中提取数据并将其插入到 Oracle 数据库中。
我从一本书中借用了这个功能。我已经使用不同的数据库对其进行了测试,并且可以正常工作:
function Add-Oracle
{
Param(
[Parameter(Mandatory=$true)][Oracle.DataAccess.Client.OracleConnection]$conn,
[Parameter(Mandatory=$true)][string]$sql,
[Parameter(Mandatory=$false)][System.Collections.Hashtable]$paramValues,
[Parameter(Mandatory=$false)][string]$idColumn )
$cmd = New-Object Oracle.DataAccess.Client.OracleCommand($sql,$conn)
$cmd.BindByName = $true
$idParam = $null
$colon = ":"
if ($idColumn)
{
$cmd.CommandText = "{0} RETURNING {1} INTO :{2} " -f $cmd.
CommandText, $idColumn, $idColumn
$idParam = New-Object Oracle.DataAccess.Client.OracleParameter
$idParam.Direction = [System.Data.ParameterDirection]::Output
$idParam.DbType = [System.Data.DbType]::Int32
$idParam.Value = [DBNull]::Value
$idParam.SourceColumn = $idColumn
$idParam.ParameterName = $idColumn
$cmd.Parameters.Add($idParam) | Out-Null
}
if ($paramValues)
{
foreach ($p in $paramValues.GetEnumerator())
{
$oraParam = New-Object Oracle.DataAccess.Client.OracleParameter
$oraParam.ParameterName = $p.Key
$oraParam.Value = $p.Value
$cmd.Parameters.Add($oraParam) | Out-Null
}
}
$result = $cmd.ExecuteNonQuery()
if ($idParam)
{
if ($idParam.Value -ne [DBNull]::Value) { $idParam.Value }
else { $null }
$idParam.Dispose()
}
$cmd.Dispose()
}
这是其余的代码:
[void][reflection.assembly]::LoadFile("D:\app2\product\11.2.0\dbhome_1\ODP.NET\bin\2.x\Oracle.DataAccess.dll")
$cs = "Data Source=orcl;User Id=Id;password=pw"
$cnn = new-object oracle.dataaccess.client.oracleconnection($cs)
$cnn.open()
$val = @()
$xl = new-object -comobject "Excel.Application"
$fls = "Poland_201306.xlsx"
$xf = "C:\test\Poland_201306.xlsx"
$wb = $xl.Workbooks.Open($xf,0,$true,5,"reporting")
$ws = $wb.Activesheet.cells.item(1,1).currentregion
[void]$ws.columns.autofit()
$nr = $ws.rows.count
$nc = $ws.columns.count
$hdr = $sel = $u = $valplaceholder = ""
$null = [System.DBNull]::value
$placeholders = ""
$questionmark = "?"
$at = "@"
for( $row=1; $row -le $nr; $row++ ) {
$sep = ""
$qsep = $null
$qsep = ""
$msg = "$row " + $ws.cells.item($row,3).text
$msg
$tempintval =0
for( $col=1; $col -le $nc; $col++ ) {
$cl = $ws.cells.item($row,$col).text.replace("""","")
if( $row -eq 1 ) {
if( $cl.contains(" ") ) {
$cl = """$cl"""
}
$hdr += "$qsep$cl"
$valplaceholder += "$sep$colon$cl"
$placeholders += "$qsep$questionmark"
$sep = ","
$qsep = ','
} else {
if($col -lt 4){ $val += "'$cl'"}
else{ $val += "$cl" }
}
}
if($row -ne 1){
try {
$map = $null
$map = @{}
if($val.count -eq $par_names.count){
for($i = 0; $i -lt 100; $i++){
$m1 = $par_names[$i]
$m2 = $val[$i]
$map.add($m1,$m2)
}
}
Add-Oracle $cnn "Insert into data($hdr) values($valplaceholder)" $map
$val = @()
$map = @{}
} Catch [System.Exception] {
Write-Host $_.Exception.ToString() -ForeGroundColor "Red"
break
}
}
}
$xl.quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
所以我的问题是,当我调用 Add-Oracle 时,出现以下错误:
System.Management.Automation.MethodInvocationException:使用“0”参数调用“ExecuteNonQuery”的异常:“ORA-01036:非法变量名称/编号”---> Oracle.DataAccess.Cli ent.OracleException:ORA-01036:非法变量名称/编号
我不确定如何跟进或找到更详细的错误。在过去的几天里,我进行了研究和搜索,并尝试了将参数发送到 Add-Oracle 的不同组合,我得到的错误是“缺少表达式”或“无效字符”
任何见解将不胜感激。