简体   繁体   English

无法弄清楚如何组合 arrays 以便能够写入 powershell 中的 SQL 列

[英]Cannot figure out how to combine arrays to be able to write to SQL columns in powershell

I have been struggling with this for hours without any result:(. I have a select string command which finds various strings in the files我已经为此苦苦挣扎了几个小时而没有任何结果:(。我有一个 select 字符串命令,它可以在文件中找到各种字符串

$sasnrs =  @(Select-String -Pattern "SASI.{11}" -path $Temp1 | Select-Object -ExpandProperty Matches)
$sassuppcars = @(Select-String -Pattern "TMB.............." -path $Temp1  | select-object -expandproperty Matches)
$sasprices = @(Select-String -Pattern '[1-2]?\.?\d{3},\d{2}' -path $Temp2 | select-object -expandproperty Matches)

This gets me output like this这让我像这样 output

PS D:\Temp> $sasnrs

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 15
Value    : SASIVB0XXXXX0

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 15
Value    : SASIIM0XXXX659

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 15
Value    : SASIIMXXXXXXX9

PS D:\Temp> $sassuppcars


Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 18
Length   : 17
Value    : TMBXXXXXXX90

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 17
Length   : 17
Value    : TMBXXXXXXXXX5625

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 17
Length   : 17
Value    : TMBXXXXXXX303

PS D:\Temp> $sasprices
Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 102
Length   : 6
Value    : 824,57

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 102
Length   : 6
Value    : 683,97

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 102
Length   : 6
Value    : 487,38

I have 4 columns created in SQL server, first one is constant, and I need to fill out rest of the three, unfortunately, when I run ForEach loop on every value of every variable, this example would create 9 rows instead of three我在 SQL 服务器中创建了 4 列,第一个是恒定的,我需要填写三列中的 rest,不幸的是,当我对每个变量的每个值运行 ForEach 循环时,此示例将创建 9 行而不是 3 行

            ForEach($Sasnr in $Sasnrs){
                $SQLSuppNRQuery = "INSERT INTO dbo.Podpory (Marken, Doklad) VALUES ('$sasmark', '$sasnr');"    
                Invoke-Sqlcmd -ServerInstance $SQLSuppSERVER -Database $SQLSuppDatabase -Query 

$SQLSuppNRQuery
                                        }
                ForEach($sassuppcar in $sassuppcars) {
                    $SQLSuppVINQuery = "INSERT INTO dbo.Podpory (VIN) VALUES ('$sassuppcar');"    
                    Invoke-Sqlcmd -ServerInstance $SQLSuppSERVER -Database $SQLSuppDatabase -Query $SQLSuppVINQuery
                                        }
                ForEach($sasprice in $sasprices){
                    $SQLSuppPRQuery = "INSERT INTO dbo.Podpory (Suma) VALUES ('$sasprice');"    
                    Invoke-Sqlcmd -ServerInstance $SQLSuppSERVER -Database $SQLSuppDatabase -Query $SQLSuppPRQuery
                                        }

Something like this像这样的东西

在此处输入图像描述

I tried creating a hashtable, but I am having troubles understanding the concept and using it in a SQL query.我尝试创建一个哈希表,但我无法理解这个概念并在 SQL 查询中使用它。

Thank you very much for your help非常感谢您的帮助

This should do, I'm just not sure of what the source type object types are for the variables holding the values.这应该可以,我只是不确定源类型 object 类型对于保存值的变量是什么。

$count = $sasnrs.count // Assuming they all have the same count here...
$i = 0
do {
                $query = "INSERT INTO dbo.Podpory (Marken,Doklad,VIN,Suma) VALUES ('$sasmark','$($sasnrs[$i].Value)','$($sassuppcars[$i].Value)','$($sasprices[$i].Value)');"    
                Invoke-Sqlcmd -ServerInstance $SQLSuppSERVER -Database $SQLSuppDatabase -Query $query
$i++
} until ($i -eq ($count - 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM