single procedure against multiple databases

Hi
I want to create a single procedure in master that will be called within a loop in each database of my server.
The goal is to count objects of each of my 500 databases and to report them.

It works when I launch the dynamic query while connected in a database, but it returns the report for master when I exec the proc.

use mydb
exec master..myproc

returns number of objects by type for the master database.


Stored procedure that I want to exec in each of my 500 databases:

create proc myproc
as

set nocount on
print 'Server : ' + @@servername
print 'Database : ' + db_name()
print ''
print '********************************************************************************************'
print 'Object number by Type for database : ' + db_name()
print '********************************************************************************************'
print ''

select substring(v.name,5,31) as 'Type d''objet', count(o.xtype) as 'Nombre d''objets'
from sysobjects o, master.dbo.spt_values v
where o.xtype = substring(v.name,1,2) collate database_default and v.type = 'O9T'
group by v.name
order by v.name


TIA

Fred
LVL 1
fhoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

acampomaCommented:
set nocount on

Declare @SQLStr varchar(4000)
Declare @Databases table (Name varchar(60),MyId int identity(1,1))
Declare @Current int
Declare @Max int
declare @NAme varchar(60)
insert into @Databases Select name  from master.dbo.sysdatabases
Select @Current=min(MYId),@Max=max(MyId) from @Databases
While @Current<=@Max Begin
select @Name=Name from @Databases where MyId=@Current
print 'Server : ' + @@servername
print 'Database : ' + @NAme
print ''
print '********************************************************************************************'
print 'Object number by Type for database : ' + @NAme
print '********************************************************************************************'
print ''

Set @SQLStr='select substring(v.name,5,31) as ''Type d''''objet'', count(o.xtype) as ''Nombre d''''objets''
from '+@Name+'.dbo.sysobjects o, master.dbo.spt_values v
where o.xtype = substring(v.name,1,2) collate database_default and v.type = ''O9T''
group by v.name
order by v.name
'

exec( @SQLStr)
set @Current=@Current+1
ENd
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.