无法将SQL查询转换为SQL Server 2000的XML输出

时间:2017-01-09 19:26:50

标签: sql xml sql-server-2000

我需要以XML格式为SQL 2000输出以下查询。似乎我有太多的子查询,并且SQL 2000中XML的嵌套级别必须准确。任何帮助表示赞赏。

var x = document.getElementsByTagName('INPUT');
var total = 0;
for (var i = 0 ; i < x.length ; i++) 
  if (x[i].name && /^altLocation\[.*\[name\]$/.test(x[i].name))
    total++;

alert('Length is: '+ total);

我能够为SQL 2005及以上版本编写这个并且工作正常!但不能为我的生活得到它为SQL 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

1 个答案:

答案 0 :(得分:1)

试试这个:我通过直接调用三列LastBackupDateSizeMBActiveDBConnections简化了您的查询,而没有您的膨胀连接。没有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 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

一个提示

正如您可能知道的那样,SQL-Server还不知道XML类型,但是FOR XML RAWFOR XML AUTOFOR XML EXPLICIT以字符串类型返回XML。