mysql操作xml字段_SQL XML 字段操作
DECLARE @myDoc xml
SET @myDoc =
'
'
--SELECT @myDoc
-- 插入item的第1個子節點,此時不需要指定as first或as last
SET @myDoc.modify(N'
insert 張洪舉
into (/root/item)[1]')
SELECT @myDoc
-- 插入item的第2個子節點,as first指定插入到同級子節點的前面
SET @myDoc.modify(N'
insert
SQL Server 2005開發寶典as first into (/root/item)[1]')
SELECT @myDoc
-- 插入第2個item節點
SET @myDoc.modify('
insert
into (/root)[1]')
SELECT @myDoc
-- 向第2個item中插入第1個子節點
SET @myDoc.modify('
insert
SQL Server 2005的新增功能as first into (/root/item)[2]')
SELECT @myDoc
GO
---------------------------------------------------二
DECLARE @myDoc xml
SET @myDoc = '
'
SELECT @myDoc
SET @myDoc.modify('
insert (
SQL Server 2005開發寶典,張洪舉
)
into (/root/item)[1]');
SELECT @myDoc
GO
---------------------------------------------------三
DECLARE @myDoc xml
SET @myDoc = N'
Ajax實戰張洪舉
Ajax實戰張洪舉
'
SELECT @myDoc
SET @myDoc.modify(N'
insert attribute ShipTo {"廣州"}
into (/root/item[@ID=1])[1]');
SET @myDoc.modify(N'
insert attribute ShipVia {"UPS"}
into (/root/item[@ID=1])[1]');
SELECT @myDoc
-- 通過一個sql變量指定要插入屬性ShipDate的值
DECLARE @ShipDate char(11)
SET @ShipDate='2006-01-23Z'
SET @myDoc.modify('
insert attribute ShipDate {sql:variable("@ShipDate") cast as xs:date ?}
into (/root/item[@ID=1])[1]') ;
SELECT @myDoc
-- 插入多個屬性,屬性之間使用逗號分隔,并包含在括號內
SET @myDoc.modify('
insert (
attribute PostCode {"253020" },
attribute Weight {"1.5"}
)
into (/root/item[@ID=1])[1]');
SELECT @myDoc
GO
---------------------------------------------------四
DECLARE @myDoc xml
SET @myDoc = N'
Ajax實戰張洪舉
ASP.NET實戰盧桂章
'
SET @myDoc.modify('
insert
after (/root/item[@ID=2]/title)[1]');
SELECT @myDoc
GO
---------------------------------------------------五
DECLARE @myDoc xml
SET @myDoc = N'
Ajax實戰張洪舉
ASP.NET實戰盧桂章
'
SET @myDoc.modify(N'
insert 上門未收]]>
into (/root/item[@ID=2])[1] ') ;
SELECT @myDoc
GO
---------------------------------------------------六
DECLARE @myDoc xml
SET @myDoc = N'
Ajax實戰張洪舉
'
SET @myDoc.modify(N'
insert text{"訂單列表"}
as first into (/root)[1]');
SELECT @myDoc
GO
---------------------------------------------------六
use jobproject
CREATE XML SCHEMA COLLECTION MySchemas
AS
N'<?xml version = "1.0"?>
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
';GO
-- 創建包含xml數據類型列的表
CREATE TABLE MyCustomer
(CustomerID int IDENTITY PRIMARY KEY,
CustomerItem xml(MySchemas));
GO
-- 向表中插入XML,該XML應當符合http://schemas.mybook.com/customerschemas命名空間架構的定義
INSERT INTO MyCustomer
VALUES
(N'
北方書城
北京市海淀區知春路22號2222222
劉先生
');
-- 使用XML DML insert插入另一個item節點到XML中
UPDATE MyCustomer
SET CustomerItem.modify('
declare namespace CS="http://schemas.mybook.com/customerschemas";
insert (
東圖大廈
長春市朝陽大街99號1111111
孫×××
)
into (/CS:customer)[1] ')
WHERE CustomerID=1;
SELECT CustomerItem
FROM Mycustomer;
GO
---------------------------------------------------七
DECLARE @myDoc xml
SET @myDoc = '<?Instructions for=TheWC.exe ?>
這里是文本
Ajax實戰張洪舉
ASP.NET實戰盧桂章
'
SELECT @myDoc
-- 刪除注釋
SET @myDoc.modify('
delete /root/comment()
')
SELECT @myDoc
-- 刪除所有指令
SET @myDoc.modify('
delete //processing-instruction()
')
SELECT @myDoc
-- 刪除ID為1的item中的文本節點
SET @myDoc.modify('
delete /root/item[@ID=1]/text()
')
SELECT @myDoc
-- 刪除一個屬性
SET @myDoc.modify('
delete /root/item[@ID=1]/@ShipTo
')
SELECT @myDoc
-- 刪除一個元素
SET @myDoc.modify('
delete /root/item[@ID=2]/author
')
SELECT @myDoc
-- 刪除ID為2的item節點
SET @myDoc.modify('
delete /root/item[@ID=2]
')
SELECT @myDoc
GO
---------------------------------------------------八
UPDATE MyCustomer
SET CustomerItem.modify('
declare namespace CS="http://schemas.mybook.com/customerschemas";
delete /CS:customer/item[@ID=2]
');
SELECT CustomerItem FROM MyCustomer;
GO
---------------------------------------------------九
DECLARE @myDoc xml
SET @myDoc = '
Ajax實戰張洪舉
ASP.NET實戰盧桂章
'
SELECT @myDoc
-- 更新ID為1的item中的title元素的文本
SET @myDoc.modify('
replace value of (/root/item[@ID=1]/title/text())[1]
with "Ajax實戰攻略"
')
SELECT @myDoc
-- 更新屬性值
SET @myDoc.modify('
replace value of (/root/item[@ID=2]/@ID)[1]
with "3"
')
SELECT @myDoc
---------------------------------------------------九
declare @xdoc xml
set @xdoc = N'
'
-------第一方法
select excel_path = t.c.value('@path'???? ,'varchar(255)')
,excel_name = t.c.value('@filename' ,'varchar(255)')
from @xdoc.nodes('/conn/excel') t (c)
where t.c.value('@id', 'int') = 1
-------第二方法
select excel_path = @xdoc.value('(/conn/excel[@id="1"]/@path)[1]', 'varchar(255)')
,excel_name = @xdoc.value('(/conn/excel[@id="1"]/@filename)[1]', 'varchar(255)')
-------第三方法
declare @excel_id int
set @excel_id = 1
select excel_path = @xdoc.value('(/conn/excel[@id=sql:variable("@excel_id")]/@path)[1]', 'varchar(255)')
,excel_name = @xdoc.value('(/conn/excel[@id=sql:variable("@excel_id")]/@filename)[1]', 'varchar(255)')
總結
以上是生活随笔為你收集整理的mysql操作xml字段_SQL XML 字段操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿凡达2不适合国人?国产科幻大片《流浪地
- 下一篇: 理想汽车CEO曾试图接触威马沈晖?本人回