-1

I'm trying to return to elseif statement after $command is equal to "n".

Clear-Host 
$Server = 'Server'
$database = 'Database_Music'
$var = Read-Host "What would you like to do? `n 1.Inspect database `n 2.Update database `n 3.Exit"
$queryprovoker = Invoke-Sqlcmd -ServerInstance $Server -Database $database -Query $query1
if($var -eq "1")
{
    $query1 = "SELECT * FROM dbo.tbl_album"
    Write-Output $queryprovoker
}
elseif($var -eq "2")
{
    Write-Output $queryprovoker | out-host
    $data = Read-Host "Type your ID"
    $query2 = "SELECT album_ID, album, ID_artiest, artwork FROM dbo.tbl_album WHERE album_ID = 
    '$data';"
    Invoke-Sqlcmd -ServerInstance $Server -Database $database -Query $query2 | out-host 

    $command = Read-Host "Is it correct?(y/n)"
        while ($command -eq "y"){}
            Write-Host $var
}`
Dale K
  • 25,246
  • 15
  • 42
  • 71
Wymik
  • 1
  • 1
  • 1
    https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements – Sean Lange May 31 '22 at 19:00
  • 1
    @SeanLange if the OP's struggling with getting the syntax of `elseif` to work you could at the very least mention that it's the `$query2=...` line which has the unrelated, but highly important sql injection issue. – Gregor y Jun 01 '22 at 23:36

1 Answers1

0

Simply wrap the whole thing in a do while loop.

    $var = Read-Host "What would you like to do? `n 1.Inspect database `n 2.Update database `n 3.Exit"
    $queryprovoker = Invoke-Sqlcmd -ServerInstance $Server -Database $database -Query $query1
    if($var -eq "1")
    {
        $query1 = "SELECT * FROM dbo.tbl_album"
        Write-Output $queryprovoker
    }
    elseif($var -eq "2")
    {
        do{

            Write-Output $queryprovoker | out-host
            $data = Read-Host "Type your ID"
            $query2 = "SELECT album_ID, album, ID_artiest, artwork FROM dbo.tbl_album WHERE album_ID = 
        '$data';"
            Invoke-Sqlcmd -ServerInstance $Server -Database $database -Query $query2 | out-host 
    
            $command = Read-Host "Is it correct?(y/n)"
                   
        }while($command -ne "y")
        Write-Host $var
    }



Write-Host "Exit. Have a nice day"
HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • @Wymik I think I misunderstood the original question, and realized that you mean "inside" the `elseif` statement instead of outside. Moving the `do ... while` loop inside the `elseif` statement should do what you are looking for. I have updated the answer. – HAL9256 Jun 02 '22 at 03:02