Technical Article

Perform operation on all databases on a server/ins

,

Sometime one needs to perform an operation on all or selected databases on a server/instance. Microsoft provides undocumented store proc sp_MSForEachDb to achieve this. I use it for some clean up process as a SQL Job.

exec master.dbo.sp_msforeachdb 
"USE [?] 
IF EXISTS (SELECT 1 
           FROM dbo.sysobjects
           WHERE id = OBJECT_ID('dbo.ap_Clean'))
BEGIN
    EXEC dbo.ap_Clean
END"

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating