跨 DB 要參考系統物件,又不想在各資料庫建立相同的預存程序,且要跳過一些 DB,以執行特定的維護作業

簡單 sample code 如下:

USE master
GO

CREATE proc sp_EachDB
as
select db_name(),count(*) from sys.objects
go

EXEC sp_MS_marksystemobject ‘sp_EachDB’
GO

exec sp_MSforeachdb ‘use [?];if not “?" in (“master","msdb","model","tempdb") exec sp_EachDB’

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: