我试图将数据从XML数据插入SQL的表中。XML文件是从Microsoft Excel创建的,该文件具有以下标头:
<?xml version="1.0"?> <?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">
我正在使用此查询来解析它:
;WITH XMLNAMESPACES ('urn:schemas-microsoft-com:office:spreadsheet' as ss) select X.value('(ss:Row/ss:Cell/ss:@Data)','varchar(max)') from @allUsers.nodes('Workbook/Worksheet/Table') as T(X)
它解析大约半秒钟,然后给我这个错误:
XQuery [value()]:“)”。
解析的XML中的数据包含电话号码,其中一些包含(和)例如:
(
)
<Row ss:AutoFitHeight="0" ss:Height="30"> <Cell ss:StyleID="s22"/> <Cell ss:StyleID="s24"><Data ss:Type="String">JohnSmith</Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">JohnSmith</Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">XYZ</Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">(555) 555-5555</Data></Cell> <Cell ss:StyleID="s22"/> </Row>
但我不认为括号内的圆括号会引起问题。
我的问题是,由于我似乎无法通过在线搜索找到任何帮助,因此之前是否有人遇到过此错误?
您问了非常类似的问题。我从两者中获取了信息,并构建了这个工作示例。请注意必须声明为“ DEFAULT”的xmlns-namespace:
简化了您的XML,但是这个主意应该可以…
DECLARE @allUsers XML= '<?xml version="1.0"?> <?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> <Table> <Row ss:AutoFitHeight="0" ss:Height="30"> <Cell ss:StyleID="s22"/> <Cell ss:StyleID="s24"><Data ss:Type="String">Jane Doe</Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">JaneDoe</Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">XYZ</Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">(555) 555-5555</Data></Cell> <Cell ss:StyleID="s22"/> </Row> </Table> </Worksheet> </Workbook>'; ;WITH XMLNAMESPACES ('urn:schemas-microsoft-com:office:spreadsheet' as ss ,DEFAULT 'urn:schemas-microsoft-com:office:spreadsheet') SELECT T.X.value('Cell[1]/Data[1]','varchar(max)') AS DontKnow1 ,T.X.value('Cell[2]/Data[1]','varchar(max)') AS Name ,T.X.value('Cell[3]/Data[1]','varchar(max)') AS UserName ,T.X.value('Cell[4]/Data[1]','varchar(max)') AS DontKnow2 ,T.X.value('Cell[5]/Data[1]','varchar(max)') AS Telephone ,T.X.value('Cell[6]/Data[1]','varchar(max)') AS DontKnow3 FROM @allUsers.nodes('/Workbook/Worksheet/Table/Row') as T(X)