我正在尝试使用XML修改SQL服务器来生成属性。 我想要生成的属性是
HRef="#'2'!A1"
我正在使用的查询是
set @xml.modify('declare default element namespace "urn:schemas-microsoft-com:office:spreadsheet";
declare namespace ss="urn:schemas-microsoft-com:office:spreadsheet";
declare namespace x="urn:schemas-microsoft-com:office:excel";
insert attribute ss:HRef {"#''sql:variable(""@worksheets"")''!A1"}
into (/Workbook/Worksheet[1]/Table/Row[sql:variable("@worksheets")]/Cell)[1]');
sql:variable(“@ worksheets”)来自循环变量。每个循环的值为1,2,3。 因此,该属性中需要的值应为
HRef="#'1'!A1"
HRef="#'2'!A1"
HRef="#'3'!A1"
现在的问题是,XML的值没有变化。如果我使用以下查询
insert attribute ss:HRef {"#''sql:variable(""@worksheets"")''!A1"}
into (/Workbook/Worksheet[1]/Table/Row[sql:variable("@worksheets")]/Cell)[1]');
以XML格式生成的属性是
SS:HREF = “# 'SQL:可变( ”@工作表“)' A1!”
这是worng,它应该生成为
HREF = “# '2'!A1”
根据我的理解,sql:variable(“@ worksheets”)的值不会被替换。 如果我输入一些硬编码值而不是sql:variable(“@ worksheets”),则值在xml中可以正确显示。 任何人都可以建议我做错了什么。
我正在使用的示例XML
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Styles xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000" ss:Bold="1"/>
</Style>
<Style ss:ID="s63" ss:Name="Hyperlink">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#0000FF" ss:Underline="Single"/>
</Style>
</Styles>
<Worksheet xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="1">
<Table xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:DefaultColumnWidth="125">
<Column ss:AutoFitWidth="1"/>
<Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Worksheet #</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String"/>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">1</Data>
</Cell>
</Row>
<Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:HRef="#'sql:variable("@worksheets")'!A1">
<Data ss:Type="String">1</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Data ss:Type="String"/>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Data ss:Type="String">1</Data>
</Cell>
</Row>
</Table>
</Worksheet>
<Worksheet xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="2">
<Table xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:DefaultColumnWidth="125">
<Column ss:AutoFitWidth="1"/>
<Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Audit ID</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Audit Subcategory ID</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">2</Data>
</Cell>
</Row>
<Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Data ss:Type="String">105076</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Data ss:Type="String">10</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Data ss:Type="String">2</Data>
</Cell>
</Row>
</Table>
</Worksheet>
<Worksheet xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="3">
<Table xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:DefaultColumnWidth="125">
<Column ss:AutoFitWidth="1"/>
<Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Audit ID</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Audit Subcategory ID</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">3</Data>
</Cell>
</Row>
</Table>
</Worksheet>
<Worksheet xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="4">
<Table xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:DefaultColumnWidth="125">
<Column ss:AutoFitWidth="1"/>
<Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Audit ID</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">Audit Subcategory ID</Data>
</Cell>
<Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:StyleID="s62">
<Data ss:Type="String">4</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
答案 0 :(得分:2)
问题是这里
insert attribute ss:HRef {"#''sql:variable(""@worksheets"")''!A1"}
好像你期待xquery和#34;神奇地&#34;连接这些字符串。你需要指定它。您需要添加额外的引号和撇号这一事实意味着它将其视为单个字符串。
您的插页应该如下所示
set @xml.modify('declare default element namespace "urn:schemas-microsoft-com:office:spreadsheet";
declare namespace ss="urn:schemas-microsoft-com:office:spreadsheet";
declare namespace x="urn:schemas-microsoft-com:office:excel";
insert attribute ss:HRef {concat("#", sql:variable("@worksheets") cast as xs:string?, "!A1")}
into (/Workbook/Worksheet[1]/Table/Row[sql:variable("@worksheets")]/Cell)[1]');
我假设@worksheets
是一个数字变量;这是cast as xs:string
。