使用格式将SQL查询到XML中

时间:2015-01-26 19:10:35

标签: sql sql-server xml tsql

我之前从未使用过XML。我有以下查询 - 需要以特定格式输出到XML文件

---- Show Server details   
GO  
SELECT   
@@servername as ServerName,  
@@version as Environment,  
SERVERPROPERTY('productversion'),   
SERVERPROPERTY ('InstanceName')  

-- Show DB details  
SELECT   
        name AS DBName ,  
        Collation_name as Collation,  
        User_access_Desc as UserAccess,  
        Compatibility_level AS CompatiblityLevel ,  
        state_desc as Status,  
        Recovery_model_desc as RecoveryModel  
FROM    sys.databases  
ORDER BY Name   

-- Sysadmin Roles  
SELECT  
     p.name AS [Name],
     r.type_desc,
     r.is_disabled,
     r.default_database_name  
FROM  
    sys.server_principals r  
INNER JOIN 
    sys.server_role_members m ON r.principal_id = m.role_principal_id  
INNER JOIN 
    sys.server_principals p ON p.principal_id = m.member_principal_id  
WHERE 
    r.type = 'R' and r.name = N'sysadmin'  

-- Find all users associated with a database  
DECLARE @DB_USers TABLE  
(DBName sysname, UserName varchar(max), LoginType sysname, AssociatedRole       varchar(max))--,create_date datetime,modify_date datetime)  

INSERT @DB_USers  
EXEC sp_MSforeachdb  

use [?]  
SELECT ''?'' AS DB_Name,  
case prin.name when ''dbo'' then prin.name + '' (''+ (select         SUSER_SNAME(owner_sid) from master.sys.databases where name =''?'') + '')'' else       prin.name end AS UserName,  
prin.type_desc AS LoginType,  
isnull(USER_NAME(mem.role_principal_id),'''') AS AssociatedRole   
FROM sys.database_principals prin  
LEFT OUTER JOIN sys.database_role_members mem ON          prin.principal_id=mem.member_principal_id  
WHERE prin.sid IS NOT NULL and prin.sid NOT IN (0x00) and  
prin.is_fixed_role <> 1 AND prin.name NOT LIKE ''##%'''  

SELECT  
DBName,UserName ,LoginType ,  

STUFF(  
(  
SELECT ',' + CONVERT(VARCHAR(500), AssociatedRole)  
FROM @DB_USers user2  
WHERE  
user1.DBName=user2.DBName AND user1.UserName=user2.UserName  
FOR XML PATH('')  
)  
,1,1,'') AS Permissions_user  
FROM @DB_USers user1  
GROUP BY  
DBName,UserName ,LoginType --,create_date ,modify_date  
ORDER BY DBName,UserName  

--List of all the jobs currently running on server  
SELECT  
     job.job_id,  
     notify_level_email,  
     name,  
     enabled,  
     description,  
     step_name,  
     command,  
     server,  
     database_name  
FROM  
     msdb.dbo.sysjobs job  
INNER JOIN   
    msdb.dbo.sysjobsteps steps          
ON  
    job.job_id = steps.job_id  

 -- Show details of extended stored procedures     
 SELECT * FROM master.sys.extended_procedures    

我不知道从哪里开始

0 个答案:

没有答案