SQL Server 2005数据库中增加了XML类型,在创建表的时候可以指定某一列为XML类型,示例如下:
CREATE TABLE customers
(
name VARCHAR(20) NOT NULL PRIMARY KEY,
description XML
)
向XML类型的列中插入数据基有三种办法:
1.直接使用字符串,示例如下:
INSERT INTO customers (feedName, feedXML) VALUES
(‘Ramon Liu’, ‘<description>rich customer</description>’)
2.使用FOR XML,示例如下:
GO
--declare xml type variable
DECLARE @xmlDoc XML
SET @xmlDoc =
(
SELECT Name, SalesYTD FROM Sales.SalesTerritory
FOR XML AUTO, ROOT ('territories'), ELEMENTS
)
INSERT INTO customers (name, description) VALUES
('Stanley', @xmlDoc)
3.使用OPENROWSET从文件中加载,示例如下:
GO
INSERT INTO customers (name, description)
SELECT 'Justin', xmlData FROM
(
SELECT * FROM OPENROWSET
(BULK 'd:\desc.xml', SINGLE_NCLOB)
AS xmlData
) AS feed (xmlData)
GO
SELECT * FROM customers