Azure SQL Database Elastic Pools – TSQL to get current Elastic Pool

All we need is an easy explanation of the problem, so here it is.

I would like to be able to fetch the name of the elastic pool that the current database belongs to using TSQL but the only DMV I can find with a column containing elastic is sys.elastic_pool_resource_stats
& I don’t see any reference to databases there. I also checked sys.databases to see if an extra column got sneaked on there but nothing I could recognise.

Ideally this should work like @@servername or db_name()
(when on earth will they get around to creating server_name() or @@dbname so that we can code consistently ?)

Does anyone know a suitable command ?

(btw no tag for "elastic pools")

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

What about this?

-- TSQL to find Databases, corresponding elastic pool names and DB edition
       @@SERVERNAME as [ServerName],
       dso.elastic_pool_name, as DatabaseName,
       sys.databases d inner join sys.database_service_objectives dso on d.database_id = dso.database_id
WHERE d.Name <> 'master'
ORDER BY, dso.elastic_pool_name

Method 2

Found it, though the solution is rather limited, because this DMV is only available in the master database and you can’t actually refer to it from within a user database.

select,  o.* 
from sys.database_service_objectives o
join sys.databases d on
  o.database_id = d.database_id

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply