Sqlserver中xml数据类型的使用
sqlserver中xml数据类型使用较少,不是很清楚,现在想试试。比如,我在数据库中有一张表test,里面的字段设为:
id int //自增
content xml //
那么现在我如果想将下面的xml文件插入进去,在查询分析器中该怎么写呢。
<?xml version="1.0" encoding="gb2312"?><books> <book id="0">大江东去</book> <book id="1">东周列国</book> <book id="2">先秦故事</book> <book id="3">三晋之家</book></books>
--> 测试数据:[tb]IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]GO CREATE TABLE [tb]([id] INT IDENTITY(1,1) PRIMARY KEY ,[content] XML)INSERT [tb] SELECT '<?xml version="1.0" encoding="gb2312"?><books> <book id="0">大江东去</book> <book id="1">东周列国</book> <book id="2">先秦故事</book> <book id="3">三晋之家</book></books>'--------------开始查询--------------------------SELECT * FROM [tb]
[解决办法]
这样写就可以了。
INSERT into [tb](content,comm) values((SELECT '<?xml version="1.0" encoding="gb2312"?><books> <book id="0">大江东去</book> <book id="1">东周列国</book> <book id="2">先秦故事</book> <book id="3">三晋之家</book></books>'),'hellworld');
[解决办法]
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]GO CREATE TABLE [tb]([id] INT IDENTITY(1,1) PRIMARY KEY ,[content] XML)INSERT [tb] SELECT '<?xml version="1.0" encoding="gb2312"?><books> <book id="0">大江东去</book> <book id="1">东周列国</book> <book id="2">先秦故事</book> <book id="3">三晋之家</book></books>'--------------开始查询--------------------------SELECT * FROM [tb]-----------------------------------------------INSERT into [tb](content,comm) values((SELECT '<?xml version="1.0" encoding="gb2312"?><books> <book id="0">大江东去</book> <book id="1">东周列国</book> <book id="2">先秦故事</book> <book id="3">三晋之家</book></books>'),'hellworld');
[解决办法]
INSERT into [tb](content,comm) values('<?xml version="1.0" encoding="gb2312"?>
<books>
<book id="0">大江东去</book>
<book id="1">东周列国</book>
<book id="2">先秦故事</book>
<book id="3">三晋之家</book>
</books>','hellworld');
把select去掉。
或者去掉括号。 select 'aa','bb'