SQL Server 2005中三种插入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 variableDECLARE @xmlDoc XMLSET @xmlDoc = ( SELECT Name, SalesYTD FROM Sales.SalesTerritory FOR XML AUTO, ROOT ('territories'), ELEMENTS )INSERT INTO customers (name, description) VALUES ('Stanley', @xmlDoc)
3.使用OPENROWSET从文件中加载,示例如下:
GOINSERT INTO customers (name, description)SELECT 'Justin', xmlData FROM ( SELECT * FROM OPENROWSET (BULK 'd:desc.xml', SINGLE_NCLOB) AS xmlData ) AS feed (xmlData)GOSELECT * FROM customers