March 11, 2008 at 6:40 am
I have a server which has around 40 databases. I have to execute a procedure(EXEC PROC Procname) in all the databases at a schedule time daily. I want to create a single job to execute this procedure since it is difficult to maintain individual jobs for every databases. How can this be done?
March 11, 2008 at 7:00 am
Just out of curiosity, why? What is it you're doing that would require executing a proc in all user databases?
Anyways, do they have to run at the same exact time or can they run one after another?
March 11, 2008 at 7:10 am
Hi thanks for the reply they can run one after another.
But I want to do this in a single job.
March 11, 2008 at 7:23 am
Than you'd need to do either a cursor or a loop ... if the proc name is the same in each database, than simply loop through the databases and call the proc with changing the context of the db name ... there are many a ways to do this, everyone has their preference, but this is probably what I would do ...:
DECLARE
@DBNameSYSNAME
,@ProcNameVARCHAR(16)
,@SQLVARCHAR(128)
,@DebugTINYINT
SET @Debug = 1 -- Change to 0 at run time
SET @ProcName = 'sp_who' -- Change this to your proc name
DECLARE procexec CURSOR FOR
SELECT [name] FROM master.dbo.sysdatabases
WHERE [dbid] > 4
ORDER BY [name]
OPEN procexec
FETCH NEXT FROM procexec INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL=
'
EXEC ' + @DBName + '.dbo.' + @ProcName + '
'
IF @Debug = 1
PRINT (@SQL)
ELSE
EXEC (@SQL)
FETCH NEXT FROM procexec INTO @DBName
END
CLOSE procexec
DEALLOCATE procexec
March 11, 2008 at 7:23 am
Does the stored procedure exists in each of the 40 databases.
You can try using sp_msforeachdb.
sp_msforeachdb " exec procname"
March 11, 2008 at 7:24 am
- So does the proc reside in every individual db ?
If not, make sure you've installed it in master, called it sp_*** and marked it as systemobject (database transparency)
e.g. exec sys.sp_MS_marksystemobject [sp_DBA_RowCount]
- Just for clarity I would create a jobstep per db. (logging per jobstep)
- If on the other hand, you only want one jobstep you could
use the undocumented :sick: sp_msforeachdb
e.g. EXEC sp_MSforeachdb @command1="print '?' DBCC CHECKDB ('?')"
Should I really add a disclamer with this last option :ermm:
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
March 11, 2008 at 7:28 am
Yeah... I wanna know to...
Simple way (although it uses cursors in the background), is to Google sp_MSForEachDB.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2008 at 7:46 am
Yes the procedure name is same in all the database. But there are around 45 databases of which I have to execute the procedure only in 40 databases. The remaining databases does not have any relation with this procedure.
March 11, 2008 at 7:50 am
Ashwin M N (3/11/2008)
Yes the procedure name is same in all the database. But there are around 45 databases of which I have to execute the procedure only in 40 databases. The remaining databases does not have any relation with this procedure.
Than in the cursor I gave you above, simply exclude what ever databases do not contain the procedure in the select statement.
Again, out of curiosity, what are you doing? Depending on the nature of this procedure, we could probably help you to change the logic of your procedure to not have it run from each and every database and/or provide some help in general on how to do a process like this.
March 11, 2008 at 8:07 am
I have a user table in every database. The user table contains many users with their user name and password to log in from application side. Whenever a user logins and logouts I have to update a different
table at the end of the day by using a lastlogindate column in users table. This update is critical for our application.
March 11, 2008 at 8:21 am
Well, since this is sql2005 right ?
As from sp2 you can use login triggers.
So if your application logges into the server connecting to the appropriate database, you might update that "last login" table immediatly in stead of running a proc every x time.
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
March 11, 2008 at 8:44 am
Sorry folks... was in a hurry and didn't see the earlier suggestions when I posted about sp_MSForeEachDB.
I'd rather not use triggers if I can help it... by the same token, once setup, they always work for critical audits that MUST be there.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2008 at 9:51 pm
Ashwin M N (3/11/2008)
I have a user table in every database. The user table contains many users with their user name and password to log in from application side. Whenever a user logins and logouts I have to update a differenttable at the end of the day by using a lastlogindate column in users table. This update is critical for our application.
Actually you could probably do this with a 40-branch Union-ed view and then rewrite your local procedures into a single procedure that just runs against that view.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
March 12, 2008 at 11:47 am
Now there's an idea... and properly partitioned, it would be updateable, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 12, 2008 at 4:40 pm
Jeff Moden (3/12/2008)
Now there's an idea... and properly partitioned, it would be updateable, as well.
Thanks!
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply