简体   繁体   English

将Excel行保存在SQL Server列(XML数据类型)中。 如何更新特定的单元格值?

[英]Have Excel Row Saved in SQL Server Column (XML data type). How Do I update a particular Cell value?

I have a row of excel data (ie several cells from an excel sheet) saved into a SQL Server table, in a column that is of datatype XML. 我在数据类型为XML的列中有一行Excel数据(即Excel工作表中的几个单元格)保存到SQL Server表中。

I need to update the value, in a particular cell, in this data saved in the XML column, with a new value that is stored in another column in the same table. 我需要使用存储在同一表中另一列中的新值来更新XML列中保存的此数据中特定单元格中的值。 How do I do that? 我怎么做?

I am not able to even select the cell propery using the XMLColumnName.Query() method in T-SQL of SQL Server 2005. 我什至无法使用SQL Server 2005的T-SQL中的XMLColumnName.Query()方法选择单元格属性。

Pasting a sample table, with sample rows here, so you can experiment and let me know if you were able to figure this out! 粘贴一个示例表,此处带有示例行,以便您可以尝试一下,让我知道您是否能够解决这个问题! Thank you!! 谢谢!!

-Shiva -湿婆

    -- 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 ?

You need to get the XML namespaces properly: 您需要正确获取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

Update: 更新:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SQL中,如何获取具有特定列的最大值的行? - In SQL, how do I get the row with the maximum value for a particular column? 如何在SQL Server的ntext列中更新XML字符串? - How do I update a XML string in an ntext column in SQL Server? 如何从 SQL 服务器中的 XML 列中提取值 - How do I extract a value from an XML column in SQL Server 如何从 SQL 服务器上的 XML 列中获取 select 数据? - How do I select data from a XML column on SQL Server? 如何查询包含XML(不是xml列类型)的SQL Server TEXT列中的值 - How can I query a value in SQL Server TEXT column that contains XML (not xml column type) SQL Server 2019。需要更新特定行中的特定列 - SQL Server 2019. Need to update particular column in particular row 从XML数据列中将SQL Server中的多个值检索(分解)到表中的同一单元格(行/列交集)中 - Retrieve (Shredding) Multiple Values in SQL Server into same cell (row/ column intersection) in table from XML data column 在SQL Server中,如何在更改为一列的情况下将行的数据复制到新行中? - In SQL Server, how do I copy a row's data into a new row with a change to one column? SQL:如何获取具有SQL Server中列最大值的行 - SQL:How do i get the row that has the max value of a column in SQL Server 如何在SQL中更新特定列的数据 - How to update the data of a particular column in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM