1 --以默认的包裹一个行对象,列名为属性名,值为行值 2 SELECT TOP 1000 * FROM [AdventureWorks2008R2].[Person].[Person] FOR XML RAW 3 4 --以默认的
包裹一个行对象,列名为元素名,包裹行值 5 SELECT TOP 1000 * FROM [AdventureWorks2008R2].[Person].[Person] FOR XML RAW , ELEMENTS 6 7 --以自定义
包裹一个行对象,列名为元素名,包裹行值 8 SELECT TOP 1000 * FROM [AdventureWorks2008R2].[Person].[Person] FOR XML RAW('PersonDetails') , ELEMENTS 9 10 11 --与FOR XML RAW 差不多,只是会自动以表名称包裹一个行对象,12 SELECT TOP 1000 * FROM [Person].[Person] FOR XML AUTO13 14 --15 CREATE TABLE xmltest16 (17 xid INT NOT NULL PRIMARY KEY,18 xdoc XML NOT NULL19 )20 21 SELECT * FROM dbo.xmltest22 23 INSERT INTO dbo.xmltest24 VALUES ( 1, '25 26 39 ' )40 41 42 INSERT INTO dbo.xmltest43 VALUES ( 2, '4427 CA28 32California 29Berkeley 30Los Angeles 3133 DE34 38Delaware 35Newark 36Wilmington 3745 58 ' )59 60 SELECT * FROM dbo.xmltest61 62 63 6446 CA47 51California 48Berkeley 49Los Angeles 5052 DE53 57Delaware 54Newark 55Wilmington 56
1 DECLARE @xmlDoc XML; 2 3 SET @xmlDoc = '4 ' 8 9 10 SELECT @xmlDoc.query('/book/title')11 12 SELECT @xmlDoc.value('(/book/title)[1]', 'nvarchar(max)')13 14 SELECT @xmlDoc.value('(/book/@id)[1]', 'nvarchar(max)')C Program 5David 621 7