Thursday, 15 January 2015

sql server - Execute SQL query on all DB's -



sql server - Execute SQL query on all DB's -

i have sql server 17 databases have same table structure. want count query each of table in each db

declare @command varchar(1000) select @command = 'if ''?'' not in(''master'', ''model'', ''msdb'', ''tempdb'', ''reportserver'', ''reportservertempdb'') begin utilize ? exec(''select count(model) rptdevicecount dbo.devices model not null'') end' exec sp_msforeachdb @command

now have 2 questions:

how can combine count results? want know how many devices there in total, not per db.

when running query, error

msg 911, level 16, state 1, line 2 database 'ch' not exist. create sure name entered correctly.

i should prepare adding if exists. have set that?

thank you!

something this:

create table #t1 (dbname varchar(20), qty int) execute master.sys.sp_msforeachdb 'use [?]; if db_name() not in (''master'',''model'',''msdb'',''tempdb'',''reportserver'',''reportservertempdb'') begin insert #t1 select db_name(), count(*) sys.tables end ' select sum(qty) #t1 drop table #t1

sql sql-server database

No comments:

Post a Comment