我的表包含特定服务器上所有数据库的名称。我希望能够使用相关的DB大小更新DBSizeMB列。
到目前为止,我的代码是:
DECLARE @DatabaseName VARCHAR(100)
UPDATE master.dbo.mytableName
SET DBsizeMB = (SELECT total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
FROM master.sys.master_files)
WHERE DBSizeMB = NULL
AND DatabaseName = @DatabaseName
现在SELECT总大小部分正在总结所有数据库'大小。
如何设置它来更新每个数据库?
答案 0 :(得分:2)
Correlate
sub-query
UPDATE mt
SET DBsizeMB = (SELECT total_size_mb = Cast(Sum(size) * 8. / 1024 AS DECIMAL(8, 2))
FROM master.sys.master_files f
JOIN sys.databases d
ON f.database_id = d.database_id
WHERE d.NAME = mt.DatabaseName)
FROM master.dbo.mytableName mt
WHERE DBSizeMB IS NULL
JOIN
版
WITH cte
AS (SELECT total_size_mb = Cast(Sum(size) * 8. / 1024 AS DECIMAL(8, 2)),d.name
FROM master.sys.master_files f
JOIN sys.databases d
ON f.database_id = d.database_id)
UPDATE mt
SET DBsizeMB = c.total_size_mb
FROM master.dbo.mytableName mt
JOIN cte c
ON c.NAME = mt.DatabaseName
WHERE DBSizeMB IS NULL
同样如评论中所述,不要在Master
数据库中创建表。即使在MSDN中也提到了
不要在master中创建用户对象。