Cannot execute SQL query (“USE statement is not supported”) on Azure SQL DB RRS feed

  • Question

  • I have really big issue which I am not able to solve and did not find proper solution on web. I need to execute INSERT statement from PowerShell script into SQL database.

    On regular SQL Server Database this is executed just fine and the row is inserted properly. This is my code:
    Install-Module -Name SqlServer -Force
    Import-Module SqlServer
    Write-Host(“Adding source to database $AMB_DB_NAME”)
    $query = @”
    INSERT INTO system VALUES (‘$item’, NULL, NULL);
    Invoke-Sqlcmd -Query $query -ServerInstance $SQL_SERVER_NAME -Username $DB_ADMIN_USER -Password $DB_ADMIN_PASS

    If I am targeting the database which is being installed on Azure then I am getting following ERROR:

    “USE statement is not supported to switch between databases. Use a new connection to connect to a different database.”

    Thursday, July 23, 2020 2:26 PM