Powershell SQL插入,参数获取错误
我试图在PS脚本中创建一个参数化insert语句,但在转换nvarchar时不断出现错误: 调用带有0个参数的ExecuteOnQuery时出现异常:将nvarchar值“select e.endpointid from endpoints e internal join meters m on m.meterid=e.meterid,其中m.meterno='06700''转换为数据类型int时,转换失败 以下是我剧本的相关部分:Powershell SQL插入,参数获取错误,sql,.net,powershell,Sql,.net,Powershell,我试图在PS脚本中创建一个参数化insert语句,但在转换nvarchar时不断出现错误: 调用带有0个参数的ExecuteOnQuery时出现异常:将nvarchar值“select e.endpointid from endpoints e internal join meters m on m.meterid=e.meterid,其中m.meterno='06700''转换为数据类型int时,转换失败 以下是我剧本的相关部分: $linesplit = $line -split ","
$linesplit = $line -split ","
$prefix = "$($linesplit[0]),$($linesplit[1]),$($linesplit[2]),$($linesplit[3])"
$numberOfMeters = $linesplit.length - 4
for($i = 1; $i -le $numberOfMeters; $i++){
$meternumber = $linesplit[3 + $i]
$meternumber = "'"+$meternumber+"'"
$notecontent = "'"+$prefix+"'"
$endpointid = "(select e.endpointid from endpoints e inner join meters m on m.meterid=e.meterid where m.meterno ="+$meternumber+" )"
write-host $endpointid -fore Cyan
$userid = "55"
$created = "(select GETDATE())"
$notetype = "0"
$note = $notecontent
write-host $note -fore Cyan
$sqlinsert = "
insert into endpointnotes (endpointId, userId, created, noteType, note)
VALUES (@endpointid,@userid,@created,@notetype,@note)
"
# Connect to SQL database and run insert statement
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "server=$Server;database=$CSDatabase;uid=;pwd="
$SqlConnection.open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Parameters.Add("@endpointid", $endpointid)
$SqlCmd.Parameters.Add("@userid", $userid)
$SqlCmd.Parameters.Add("@created", $created)
$SqlCmd.Parameters.Add("@notetype", $notetype)
$SqlCmd.Parameters.Add("@note", $note)
$SqlCmd.CommandText = $sqlinsert
$SqlCmd.Connection = $SqlConnection
$SqlCmd.ExecuteNonQuery()
$SqlConnection.close()
}
甚至可以在我的一个参数中使用SELECT语句吗?
任何帮助都将不胜感激
谢谢不能将select语句用作参数值。您需要将插入内容重写为插入内容。。。从中交叉应用endpointId,或者首先找到endpointId并将其传递给其他用户。交叉应用?我以前从未遇到过,但我会仔细阅读。我可能只是先运行一个select查询,将endpointid首先放入PS变量中,然后将其作为参数传入。谢谢@杰西:我相信外部应用程序应该可以完成你的工作,甚至交叉应用程序从查询中的仪表中删除报价,其中m.meterno='06700'