-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckAllServerDatabasesForPresenceOfData.sql
More file actions
47 lines (36 loc) · 1.3 KB
/
CheckAllServerDatabasesForPresenceOfData.sql
File metadata and controls
47 lines (36 loc) · 1.3 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*******************************************************************************
Sometimes it would be convenient to be able to check a particular table in
every database on a server. Handy if you have multiple databases with the same schema
on a single server (as is the case in some boxed products like veracore/promail).
In this example we're looking for any database with more than 4 employees
in the HumanResources Employee table.
1. This script gets a list of online databases that have a name matching your filter.
2. Once it has that list it uses a cursor to run a small bit of code in each database
and lists any database that matches the criteria…
*******************************************************************************/
declare @cDBName varchar(150)
,@sql varchar(max)
declare cDB cursor local fast_forward for
select name
from sys.databases
where state_desc = 'ONLINE'
and name like '%Adventure%' --This is simply a filter you might apply to the name
order by name
open cDB
while 1=1
begin
fetch next from cDB into @cDBName
if @@fetch_status <> 0
begin
break;
end
select @sql = 'if exists(select 1
from [{DBNAME}].HumanResources.Employee
where EmployeeID > 4
select ''{DBNAME}'''
set @sql = replace(@sql, '{DBNAME}',@cDBName)
exec(@sql)
--print @sql
end
close cDB
deallocate cDB