Мне нужно вывести следующий запрос в формате XML для SQL 2000. Кажется, что у меня слишком много подзапросов, а уровни вложенности для XML в SQL 2000 должны быть точными. Любая помощь приветствуется.Проблемы с преобразованием SQL-запроса в XML-вывод для SQL Server 2000
SELECT sd.dbid AS DatabaseID
,NAME AS DatabaseName
,CASE
WHEN NAME IN (
'master'
,'msdb'
,'model'
,'tempdb'
,'distribution'
)
THEN 'S'
ELSE 'U'
END AS SysUserType
,cmptlevel AS CompatibilityLevel
,databasepropertyex(NAME, 'Collation') AS CollationName
,CASE
WHEN databasepropertyex(NAME, 'Status') = 'ONLINE'
THEN 0
WHEN databasepropertyex(NAME, 'Status') = 'RESTORING'
THEN 1
WHEN databasepropertyex(NAME, 'Status') = 'RECOVERING'
THEN 2
WHEN databasepropertyex(NAME, 'Status') = 'RECOVERY_PENDING'
THEN 3
WHEN databasepropertyex(NAME, 'Status') = 'SUSPECT'
THEN 4
WHEN databasepropertyex(NAME, 'Status') = 'EMERGENCY'
THEN 5
WHEN databasepropertyex(NAME, 'Status') = 'OFFLINE'
THEN 6
WHEN databasepropertyex(NAME, 'Status') = 'COPYING'
THEN 7
END AS STATE
,databasepropertyex(NAME, 'Status') AS StateDesc
,CASE
WHEN databasepropertyex(NAME, 'Recovery') = 'FULL'
THEN 1
WHEN databasepropertyex(NAME, 'Recovery') = 'BULK_LOGGED'
THEN 2
WHEN databasepropertyex(NAME, 'Recovery') = 'SIMPLE'
THEN 3
END AS RecoveryModel
,databasepropertyex(NAME, 'Recovery') AS RecoveryModelDesc
,crdate AS DatabaseCreationDate
,B.last_db_backup_date AS LastBackupDate
,a.SizeMB AS SizeMB
,c.NumberOfConnections AS ActiveDBConnections
,SERVERPROPERTY('Machinename') AS SQLServerName
,CASE
WHEN SERVERPROPERTY('Instancename') IS NULL
THEN 'Default'
ELSE SERVERPROPERTY('Instancename')
END AS SQLServerInstanceName
,SERVERPROPERTY('ProductVersion') AS SQLServerVersion
,SERVERPROPERTY('Edition') AS SQLServerEdition
FROM (
SELECT CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SERVER
,msdb.dbo.backupset.database_name
,MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
GROUP BY msdb.dbo.backupset.database_name
) AS B
FULL JOIN sysdatabases sd ON sd.NAME = b.database_name
INNER JOIN (
SELECT (SUM(size) * 8/1024) AS SizeMB
,dbid
FROM sysaltfiles
GROUP BY dbid
) AS A ON sd.dbid = a.dbid
FULL JOIN (
SELECT DB_NAME(dbid) AS DBName
,COUNT(dbid) AS NumberOfConnections
FROM sysprocesses
WHERE dbid > 0
AND spid >= 51
GROUP BY dbid
) AS C ON sd.NAME = C.DBName
ORDER BY sd.dbid
Я смог написать это для SQL 2005 и выше и отлично работает! Но не могу за жизнь мне получить его для SQL 2000
SELECT sd.database_id AS DatabaseID
,sd.NAME AS DatabaseName
,CASE
WHEN sd.NAME IN (
'master'
,'msdb'
,'model'
,'tempdb'
,'distribution'
)
THEN 'S'
ELSE 'U'
END AS SysUserType
,sd.compatibility_level AS CompatibilityLevel
,sd.collation_name AS CollationName
,sd.STATE AS STATE
,sd.state_desc AS StateDesc
,recovery_model AS RecoveryModel
,recovery_model_desc AS RecoveryModelDesc
,create_date AS DatabaseCreationDate
,B.last_db_backup_date AS LastBackupDate
,a.SizeMB AS SizeMB
,c.NumberOfConnections AS ActiveDBConnections
,SERVERPROPERTY('Machinename') AS SQLServerName
,CASE
WHEN SERVERPROPERTY('Instancename') IS NULL
THEN 'Default'
ELSE SERVERPROPERTY('Instancename')
END AS SQLServerInstanceName
,SERVERPROPERTY('ProductVersion') AS SQLServerVersion
,SERVERPROPERTY('Edition') AS SQLServerEdition
FROM (
SELECT CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS SERVER
,msdb.dbo.backupset.database_name
,MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date
FROM msdb.dbo.backupmediafamily
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id
GROUP BY msdb.dbo.backupset.database_name
) AS B
RIGHT JOIN sys.databases sd ON sd.NAME = B.database_name
INNER JOIN (
SELECT (SUM(size) * 8/1024) AS SizeMB
,database_id
FROM sys.master_files
GROUP BY database_id
) AS A ON sd.database_id = a.database_id
FULL JOIN (
SELECT DB_NAME(dbid) AS DBName
,COUNT(dbid) AS NumberOfConnections
FROM sys.sysprocesses
WHERE dbid > 0
AND spid >= 51
GROUP BY dbid
) AS C ON sd.NAME = C.DBName
ORDER BY sd.database_id
FOR XML RAW ('DATABASES'),
ROOT ('SERVERROOT'),
Elements
Есть ли инструмент, который может сделать это преобразование для меня? –