How to run script on multiple databases

I have a SQL maintenance script that I want to run on multiple databases instead of having to run it manually against each database.  Is there a way of creating a job or something to allow this to happen?

Thank you in advance for your help.
LVL 1
cjrcomputersAsked:
Who is Participating?
 
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
If you create a job, you can have one jobstep for dbOne, and let jobstep two take care of dbTwo, etc etc

//Marten
0
 
cjrcomputersAuthor Commented:
I was hoping I wouldn't have to create a step for each database as there's 11 of them, but if thats the only way I guess I'll have to.
0
 
ralmadaCommented:
Have you considered using the sp_msforeachdb procedure?
Check these links for details:
http://blogs.techrepublic.com.com/datacenter/?p=395 
http://www.mssqltips.com/tip.asp?tip=1414 
0
 
cjrcomputersAuthor Commented:
Well I would use that procedure, but I don't want to do it on all of the databases on the server.  I think I'll just have to created a job with a step for each database.
0
 
ralmadaCommented:
I guess you didn't read the links, you can selectively run the script based on the database name. check this part of the first link:
EXECUTE sp_msforeachdb 'USE ?
IF DB_NAME() NOT IN(''master'',''msdb'',''tempdb'',''model'')
BACKUP DATABASE ? TO DISK = ''G:?.bak, WITH INIT'''

Open in new window

0
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.

All Courses

From novice to tech pro — start learning today.