Use CLI to move an Azure SQL database in a SQL elastic pool

This Azure CLI script example creates two elastic pools and moves an Azure SQL database from one SQL elastic pool into another SQL elastic pool, and then moves the database out of elastic pool to a single database compute size.

If you don't have an Azure subscription, create a free account before you begin.

Open Azure Cloud Shell

Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Select Copy to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:

Select Try It in the upper-right corner of a code block. Example of Try It for Azure Cloud Shell
Open Cloud Shell in your browser. Launch Azure Cloud Shell button
Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Cloud Shell button in the Azure portal

If you choose to install and use the CLI locally, this topic requires that you are running the Azure CLI version 2.0 or later. Run az --version to find the version. If you need to install or upgrade, see Install the Azure CLI.

Sample script


# Set an admin login and password for your database
export adminlogin=ServerAdmin
export password=ChangeYourAdminPassword1
# The logical server name has to be unique in the system
export servername=server-$RANDOM

# Create a resource group
az group create \
    --name myResourceGroup \
    --location westeurope 

# Create a logical server in the resource group
az sql server create \
    --name $servername \
    --resource-group myResourceGroup \
    --location westeurope \
    --admin-user $adminlogin \
    --admin-password $password

# Create two pools in the logical server
az sql elastic-pools create \
    --resource-group myResourceGroup \
    --location westeurope  \
    --server $servername \
    --name myFirstPool \
    --dtu 50 \
    --database-dtu-max 20
az sql elastic-pools create \
    --resource-group myResourceGroup \
    --location westeurope  \
    --server $servername \
    --name MySecondPool \
    --dtu 50 \
    --database-dtu-max 50

# Create a database in the first pool
az sql db create \
    --resource-group myResourceGroup \
    --server $servername \
    --name mySampleDatabase \
    --elastic-pool-name myFirstPool

# Move the database to the second pool - create command updates the db if it exists
az sql db create \
    --resource-group myResourceGroup \
    --server-name $servername \
    --name mySampleDatabase \
    --elastic-pool-name mySecondPool

# Move the database to standalone S1 performance level
az sql db create \
    --resource-group myResourceGroup \
    --server $servername \
    --name mySampleDatabase \
    --service-objective S1

Clean up deployment

Use the following command to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

Script explanation

This script uses the following commands. Each command in the table links to command specific documentation.

Command Notes
az group create Creates a resource group in which all resources are stored.
az sql server create Creates a SQL Database server that hosts single databases and elastic pools.
az sql elastic-pools create Creates an elastic pool.
az sql db create Creates a single database or a database in an elastic pool.
az sql db update Updates database properties or moves a database into, out of, or between elastic pools.
az group delete Deletes a resource group including all nested resources.

Next steps

For more information on the Azure CLI, see Azure CLI documentation.

Additional SQL Database CLI script samples can be found in the Azure SQL Database documentation.