Powershell:如何从 Invoke-Sqlcmd Select 语句中选择 1 个结果
Powershell: How to pick 1 result of many from Invoke-Sqlcmd Select statement
当我使用 Invoke-Sqlcmd 运行 一个 SELECT 语句时,我想从 X 个结果中选择 1 个以循环前进。在下面的示例中,我有 'Bob' 的多个结果,我想选择合适的一个来继续使用查询来更新他的 favFood(我知道我没有那里的代码,但只是给出一个我目前所拥有的示例)
$nameList = $(Write-Host "Enter a list of Names seperated by a comma (ex.`"Bob, Rick, Jordan\`"): " -ForegroundColor Green -NoNewline; Read-Host)
$nameList = $nameList.Split(', ')
ForEach ($element in $nameList)
{
Invoke-Sqlcmd -Server Server01 -database People -Query ` "SELECT firstName, lastName, address, favFood`
FROM People ` WHERE firstName = '$element'" }
有没有一种方法可以列出名字并让程序询问我想要更新 X 个结果中的哪一个?
老实说,我对如何从 X 个结果中选择特定的 row/person 进行更新有点困惑。
小 side-note/bonus 点:当 运行ning 程序时,它会抱怨“参数列表中缺少参数”。因为我正在写下名字,即使它会接受并且 return 结果符合预期(我用 space 和逗号分隔名字,例如“Bob, Ross, Jay”
理论上你根本不需要循环,你可以使用 SQL IN
operator and pass all the names of $nameList
to your query. Then once you have stored the $result
of your query, you can use Out-GridView -PassThru
弹出一个 DataGridView 供你选择一行,这将存储在 $choice
.
Write-Host 'Enter a list of Names seperated by a comma (ex."Bob, Rick, Jordan"): ' -ForegroundColor Green -NoNewline
$nameList = (Read-Host).Split(',').Trim()
# $namelist would become Bob','Rick','Jordan
$nameList = $nameList -join "','"
$result = Invoke-Sqlcmd -Server Server01 -Database People -Query @"
SELECT firstName, lastName, address, favFood
FROM People WHERE firstName IN ('$namelist');
"@
$choice = $result | Out-GridView -PassThru
当我使用 Invoke-Sqlcmd 运行 一个 SELECT 语句时,我想从 X 个结果中选择 1 个以循环前进。在下面的示例中,我有 'Bob' 的多个结果,我想选择合适的一个来继续使用查询来更新他的 favFood(我知道我没有那里的代码,但只是给出一个我目前所拥有的示例)
$nameList = $(Write-Host "Enter a list of Names seperated by a comma (ex.`"Bob, Rick, Jordan\`"): " -ForegroundColor Green -NoNewline; Read-Host)
$nameList = $nameList.Split(', ')
ForEach ($element in $nameList)
{
Invoke-Sqlcmd -Server Server01 -database People -Query ` "SELECT firstName, lastName, address, favFood`
FROM People ` WHERE firstName = '$element'" }
有没有一种方法可以列出名字并让程序询问我想要更新 X 个结果中的哪一个?
老实说,我对如何从 X 个结果中选择特定的 row/person 进行更新有点困惑。
小 side-note/bonus 点:当 运行ning 程序时,它会抱怨“参数列表中缺少参数”。因为我正在写下名字,即使它会接受并且 return 结果符合预期(我用 space 和逗号分隔名字,例如“Bob, Ross, Jay”
理论上你根本不需要循环,你可以使用 SQL IN
operator and pass all the names of $nameList
to your query. Then once you have stored the $result
of your query, you can use Out-GridView -PassThru
弹出一个 DataGridView 供你选择一行,这将存储在 $choice
.
Write-Host 'Enter a list of Names seperated by a comma (ex."Bob, Rick, Jordan"): ' -ForegroundColor Green -NoNewline
$nameList = (Read-Host).Split(',').Trim()
# $namelist would become Bob','Rick','Jordan
$nameList = $nameList -join "','"
$result = Invoke-Sqlcmd -Server Server01 -Database People -Query @"
SELECT firstName, lastName, address, favFood
FROM People WHERE firstName IN ('$namelist');
"@
$choice = $result | Out-GridView -PassThru