这个查询的结果提供任何数据库的清单,这些数据库产生于上次我更新应用元数据和服务器时,它不仅是跨域的数据库创建活动的通知,也是致力于更新两个数据库来符合应用程序信息的数据清单。这个查询也适合SQL Server Reporting Services报告的数据表,而当我不在办公室时,SQL Server Reporting Services报告也为我提供了一个新的数据库到我的黑莓(BlackBerry)的日常通知。
最后,我创建了以下存储程序,由此用任何新的数据库信息来合并dbo.Applications表和dbo.Database_Applications 表。它接受三个参数:服务器,数据库和应用程序。如果应用程序已经不存在于dbo.Applications表中,它就会被补充。然后一个记录被插入到服务器/数据库/应用程序关系中的dbo.Applications表。
CREATE PROCEDURE [dbo].[pAdd_Application] @ServerName varchar(50), @DatabaseName varchar(100), @ApplicationName varchar(100)
AS
--Add any new databases created, but not recorded in the repository, to the repository
UPDATE dbo.Database_Applications
SET ApplicationName = @ApplicationName
WHERE ServerName = @ServerName
AND DatabaseName = @DatabaseName
AND ApplicationName IS NULL
--Determine if there is already an application for this database in the repository, if not, then add it
IF (SELECT COUNT(*) FROM dbo.Applications WHERE ApplicationName = @ApplicationName) = 0
BEGIN
INSERT INTO dbo.Applications (ApplicationName)
VALUES (@ApplicationName)
PRINT 'Added new Application: ' + @ApplicationName + ' to Applications table'
SELECT * FROM dbo.Applications WHERE ApplicationName = @ApplicationName
END
--List the new record in the repository
SELECT ServerName, DatabaseName, ApplicationName
FROM dbo.Database_Applications
WHERE ServerName = @ServerName
AND DatabaseName = @DatabaseName
AND ApplicationName = @ApplicationName
AS
--Add any new databases created, but not recorded in the repository, to the repository
UPDATE dbo.Database_Applications
SET ApplicationName = @ApplicationName
WHERE ServerName = @ServerName
AND DatabaseName = @DatabaseName
AND ApplicationName IS NULL
--Determine if there is already an application for this database in the repository, if not, then add it
IF (SELECT COUNT(*) FROM dbo.Applications WHERE ApplicationName = @ApplicationName) = 0
BEGIN
INSERT INTO dbo.Applications (ApplicationName)
VALUES (@ApplicationName)
PRINT 'Added new Application: ' + @ApplicationName + ' to Applications table'
SELECT * FROM dbo.Applications WHERE ApplicationName = @ApplicationName
END
--List the new record in the repository
SELECT ServerName, DatabaseName, ApplicationName
FROM dbo.Database_Applications
WHERE ServerName = @ServerName
AND DatabaseName = @DatabaseName
AND ApplicationName = @ApplicationName
虽然我可以很容易地把这个存储程序的执行整合为SQL Server集成服务(SSIS)程序包中的最后一步,而这个程序包能够组装我的存储数据库,但我选择不这样做,这是为了在我的环境里,我能密切关注围绕新的数据库创造而展开的活动。