An Azure relational database service.
Replace the fixed 30-second delay with readiness gates for copy completion, configuration completion, online status, and connection quiescence, then reconcile both database names before retrying any rename.
Recommended automation sequence
- Wait for the copied database to become
ONLINE.
az sql db copyis asynchronous: the target resource appears immediately, while the actual copy can remain inCOPYING. A successful copy changessys.databases.state_desctoONLINE;SUSPECTindicates failure.^1^ - Wait for configuration operations to finish.
Changing compute or serverless settings can start an asynchronous database operation. Poll untilaz sql db op listreturns no operations inInProgressstate.^2^ - Confirm the control-plane status is
Online.
Query each database withaz sql db show --query status; do not proceed merely because the preceding CLI command returned successfully.^3^ - Stop reporting traffic and drain connections.
Azure SQL Database cannot be renamed while other users have open connections. Unlike SQL Server, the documented Azure SQL procedure requires the clients to be disconnected rather than forcingSINGLE_USERmode.^4^ - Rename sequentially and verify each postcondition.
az sql db renameis a supported command, but rename is not idempotent because it changes the resource identifier.^5^ The management operation targets the old database resource and moves it to an ID containing the new name.^6^
A compact PowerShell gate can replace the fixed sleep:
function Wait-DbReady($rg, $server, $db) {
for ($i = 0; $i -lt 60; $i++) {
$status = az sql db show -g $rg -s $server -n $db `
--query status -o tsv 2>$null
$ops = az sql db op list -g $rg -s $server --database $db `
--query "[?state=='InProgress'].name" -o tsv 2>$null
if ($LASTEXITCODE -eq 0 -and $status -eq "Online" -and !$ops) {
return
}
Start-Sleep -Seconds ([Math]::Min(60, 5 * [Math]::Pow(2, $i / 5)))
}
throw "Database '$db' did not become ready before timeout."
}
Also query sys.databases and sys.dm_operation_status from master when possible. The latter records copy, alter, service-tier, restore, and delete operations, including state_desc, completion percentage, and error details.^7^
Retry by observed state—not by repeating the command
After a failed or timed-out rename, query both the old and new names:
- Old exists, new does not: retry the rename with bounded exponential backoff and jitter.
- Old does not exist, new exists: the rename probably completed; continue with validation.
- Both exist: stop; do not overwrite or delete either database automatically.
- Neither exists: stop and escalate; verify subscription, resource group, server, and names.
- Either resource has an in-progress operation: wait, then reconcile again.
Azure Resource Manager can experience intermittent management-operation failures, but Microsoft recommends checking operation state before retrying and determining whether repeating the request is safe.^8^ A ResourceNotFound response specifically means Resource Manager could not retrieve the addressed resource; the documented first checks are resource name, resource group, and subscription.^9^
Serverless and other rename constraints
Auto-resume is not a documented explanation for this exact management-plane 404. A paused serverless database normally resumes on a login attempt; that first connection can return SQL error 40613, and resume generally takes less than one minute.^10^ Disabling auto-pause immediately before rename is therefore not a substitute for polling readiness, particularly when configuration changes are still pending.
Also verify these supported rename prerequisites:
- No active connections.
- The automation identity has
ALTERpermission when using T-SQL. - The database is not in active geo-replication.^4^
- If it belongs to a failover group, remove it from that relationship before rename; database rename is unsupported within a failover group.^11^
There is no authoritative published known issue in the supplied product documentation specifically identifying intermittent 404 ResourceNotFound from az sql db rename. Preserve the failed Activity Log event, timestamp, correlation ID, command parameters, CLI version, and the old/new-name state immediately afterward. If readiness gating does not eliminate the failures, provide those records to Azure Support for backend tracing.
Complete reporting copy
Continue using Azure SQL database copy when the requirement is a complete, transactionally consistent point-in-time copy. After completion, it is an independent, fully functional database; database users, permissions, and SIDs are copied, and the same-server scenario can use the same logins.^1^ This is more suitable than a Data Factory Copy activity when stored procedures and other database objects must be preserved.
The reliability issue is therefore best addressed by keeping database copy but replacing fixed sleeps and blind rename retries with explicit operation-state polling, connection draining, and old/new-name reconciliation.
References
- Copy a transactionally consistent copy of a database in Azure SQL Database
- Lists the names of ongoing operations on a specified Azure SQL Database using Azure CLI in a Cloud Shell environment.
- Retrieves the current status of an Azure SQL Database using Azure CLI by specifying the database name, resource group, and server, then querying the 'status' pr...
- Rename a Database - SQL Server | Microsoft Learn
- az sql db rename
- Databases - Rename - REST API (Azure SQL Database) | Microsoft Learn
- sys.dm_operation_status
- Reliability in Azure Resource Manager
- Resolve errors for resource not found
- Auto-pause and auto-resume in the serverless compute tier for Azure SQL Database
- Configure a failover group for Azure SQL Database (azure-sql-single-db)