将Excel行保存在SQL Server列中(XML数据类型)。如何更新特定的单元格值?
我有一行excel数据,即excel工作表中的几个单元格保存到SQL Server表中,列的数据类型为XML 我需要使用存储在同一表的另一列中的新值更新保存在XML列中的数据中特定单元格中的值。 我该怎么做 在SQLServer2005的T-SQL中,我甚至无法使用XMLColumnName.Query方法选择单元格属性 粘贴一个示例表,在这里有示例行,这样您就可以进行实验,并让我知道您是否能够解决这个问题!谢谢 -湿婆将Excel行保存在SQL Server列中(XML数据类型)。如何更新特定的单元格值?,sql,sql-server-2005,updatexml,xml-column,Sql,Sql Server 2005,Updatexml,Xml Column,我有一行excel数据,即excel工作表中的几个单元格保存到SQL Server表中,列的数据类型为XML 我需要使用存储在同一表的另一列中的新值更新保存在XML列中的数据中特定单元格中的值。 我该怎么做 在SQLServer2005的T-SQL中,我甚至无法使用XMLColumnName.Query方法选择单元格属性 粘贴一个示例表,在这里有示例行,这样您就可以进行实验,并让我知道您是否能够解决这个问题!谢谢 -湿婆 -- reference article for XML data
-- reference article for XML data manipulation
-- http://msdn.microsoft.com/en-us/library/ms345117(v=sql.90).aspx#sql2k5xml_topic3
-- create test table for xml data
CREATE TABLE testdocs (pk INT PRIMARY KEY, xCol XML not null)
-- insert 1 row of test data
insert into testdocs values(1,'<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="TestSheet">
<Table ss:ExpandedColumnCount="509" ss:ExpandedRowCount="1" ss:StyleID="s191">
<Column ss:StyleID="s1" ss:AutoFitWidth="0" ss:Width="55" />
<Column ss:StyleID="s2" ss:AutoFitWidth="0" ss:Width="55" />
<Row>
<Cell>
<Data ss:Type="String">TestValue1</Data>
</Cell>
<Cell>
<Data ss:Type="String">TestValue2</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>')
-- select records, and inspect the XML in SSMS
select * from testdocs
-- want to replace / update "TestValue2" in the XML for the 1s rows, to "New TestValue2"
-- location in XML hierarchy is as follows
/Workbook/Worksheet/Table/Row/Cell/Data
-- how do i do that ?
您需要正确获取XML名称空间:
with xmlnamespaces (
DEFAULT 'urn:schemas-microsoft-com:office:spreadsheet'
, 'urn:schemas-microsoft-com:office:office' as o
, 'urn:schemas-microsoft-com:office:excel' as x
, 'urn:schemas-microsoft-com:office:spreadsheet' as ss
, 'http://www.w3.org/TR/REC-html40' as html)
select xCol.value ('(/Workbook/Worksheet/Table/Row/Cell/Data)[1]','varchar(100)') as Data
from testdocs
更新:
with xmlnamespaces (
DEFAULT 'urn:schemas-microsoft-com:office:spreadsheet'
, 'urn:schemas-microsoft-com:office:office' as o
, 'urn:schemas-microsoft-com:office:excel' as x
, 'urn:schemas-microsoft-com:office:spreadsheet' as ss
, 'http://www.w3.org/TR/REC-html40' as html)
update testdocs
set xCol.modify (' replace value of (/Workbook/Worksheet/Table/Row/Cell/Data/text())[1]
with "ReplacedValue1"');
select * from testdocs
您需要正确获取XML名称空间:
with xmlnamespaces (
DEFAULT 'urn:schemas-microsoft-com:office:spreadsheet'
, 'urn:schemas-microsoft-com:office:office' as o
, 'urn:schemas-microsoft-com:office:excel' as x
, 'urn:schemas-microsoft-com:office:spreadsheet' as ss
, 'http://www.w3.org/TR/REC-html40' as html)
select xCol.value ('(/Workbook/Worksheet/Table/Row/Cell/Data)[1]','varchar(100)') as Data
from testdocs
更新:
with xmlnamespaces (
DEFAULT 'urn:schemas-microsoft-com:office:spreadsheet'
, 'urn:schemas-microsoft-com:office:office' as o
, 'urn:schemas-microsoft-com:office:excel' as x
, 'urn:schemas-microsoft-com:office:spreadsheet' as ss
, 'http://www.w3.org/TR/REC-html40' as html)
update testdocs
set xCol.modify (' replace value of (/Workbook/Worksheet/Table/Row/Cell/Data/text())[1]
with "ReplacedValue1"');
select * from testdocs