使用PowerShell在SQL Server DB插入字符串值。
技术问答
244 人阅读
|
0 人回复
|
2023-09-14
|
我有一长串需要单列。SQL Server插入表中的值。我使用以下代码。但这需要很长时间。有没有更简单的方法来实现这个目标?$ k. j" ]- A$ ?7 b8 m; k7 Z
$list = 'aaa','bbb','cccc','ddddd','eeeee','ffff'.... foreach($i in $list) $sql ="if not exists (select 1 from [table_nm] where column_nm = '$i begin insert table_nm select '$i end " Invoke-Sqlcmd -ServerInstance $server -Database db_nm -query $sql
9 d+ J9 n1 M- m/ R9 y- v) R 解决方案: ' B- I- p2 q3 b4 @3 a
尝试此操作,它将在单个连接上运行,因此您可以避免遵循@vonPryz昂贵的费用:/ w5 \8 e) S! k! r" _5 ^
$list = 'aaa','bbb','cccc','ddddd','eeeee','ffff'....$server = "server1"$Database = "DB1"$Connection = New-Object System.Data.SQLClient.SQLConnection$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"$Connection.Open()$Command = New-Object System.Data.SQLClient.SQLCommand$Command.Connection = $Connectionforeach($i in $list) $sql ="if not exists (select 1 from [table_nm] where column_nm = '$i begin insert table_nm select '$i end " $Command.CommandText = $sql $Command.ExecuteReader()}$Connection.Close() |
|
|
|
|
|