-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_list.sql
More file actions
31 lines (28 loc) · 788 Bytes
/
database_list.sql
File metadata and controls
31 lines (28 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- This query must return a single column named [name]
set nocount on
/*
Just an example
*/
select [name]
from sys.databases
where [name] = N'master'
/*
-- Selects all user databases and skips databases on AlwaysOn secondary replicas
select [name]
from sys.databases
where 1 = 1
and [name] not in (N'distribution')
and [name] not in (select agbd.[database_name] from sys.availability_databases_cluster agbd)
and database_id > 4
and state_desc = N'ONLINE'
union all
select db.[database_name]
from sys.dm_hadr_availability_group_states st
join sys.availability_groups gr
on gr.group_id = st.group_id
join sys.availability_databases_cluster db
on db.group_id = gr.group_id
where 1 = 1
and db.[database_name] not in (N'distribution')
and st.primary_replica = @@servername
*/