Having trouble converting SQL query to XML ouput for SQL Server 2000

47 views Asked by At

I need to output the following query in XML format for SQL 2000. It seems that I have too many subqueries and the nesting levels for XML in SQL 2000 must be exact. Any help is appreciated.

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

I was able to write this for SQL 2005 and above and works fine! But cannot for the life of me get it for 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
1

There are 1 answers

1
Shnugo On BEST ANSWER

Try this: I simplified your query by calling the three columns LastBackupDate, SizeMB and ActiveDBConnections directly without your bloating joins. No SQL-Server-2000 ready to test, but I'd assume this should work:

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 databasepropertyex(NAME, 'Status') 
        WHEN 'ONLINE'              THEN 0
        WHEN 'RESTORING'           THEN 1
        WHEN 'RECOVERING'          THEN 2
        WHEN 'RECOVERY_PENDING'    THEN 3
        WHEN 'SUSPECT'             THEN 4
        WHEN 'EMERGENCY'           THEN 5
        WHEN 'OFFLINE'             THEN 6
        WHEN 'COPYING'             THEN 7
     END AS [STATE]
    ,databasepropertyex(NAME, 'Status') AS StateDesc
    ,CASE databasepropertyex(NAME, 'Recovery')
        WHEN 'FULL'                THEN 1
        WHEN 'BULK_LOGGED'         THEN 2
        WHEN 'SIMPLE'              THEN 3
     END AS RecoveryModel
    ,databasepropertyex(NAME, 'Recovery') AS RecoveryModelDesc
    ,crdate AS DatabaseCreationDate
    ,(SELECT MAX(bs.backup_finish_date) FROM msdb.dbo.backupset AS bs WHERE bs.database_name=sd.name) AS LastBackupDate
    ,(SELECT (SUM(saf.size) * 8 / 1024) FROM sysaltfiles AS saf WHERE saf.dbid=sd.dbid) AS SizeMB
    ,(SELECT COUNT(sp.dbid) FROM sysprocesses AS sp WHERE dbid > 0 AND spid >= 51 AND sp.dbid=sd.dbid) 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 sysdatabases sd 
ORDER BY sd.dbid

One hint

As you probably know, SQL-Server did not yet know the type XML, but there was FOR XML RAW, FOR XML AUTO and FOR XML EXPLICIT returning XML in string type.