I am trying to insert data into a table in SQL from XML data. The XML file was created from Microsoft Excel, which gives it this header:
<?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">
I am using this query to parse it:
;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)
which parses for about a half a second and then gives me this error:
XQuery [value()]: ")" was expected.
The data within the XML being parsed contain phone numbers, some of which contain (
and )
eg:
<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>
but I don't think that an open parentheses within the would cause a problem.
My question is, has anybody else encountered this error before, since I can't seem to find any help through an online search?
EDIT I think I may have been going in the wrong direction in this case. I have posted a new question here: Separating XML values with the same tags into different rows SQL Server
You asked very similar questions. I took the information from both and built this working example. Be aware of the xmlns-namespace which must be declared as "DEFAULT":
Simplified your XML, but the idea should be OK...
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)
It's probably not the parenthesis in the phone number that causes the error, but your XQuery query, which I think has a syntax error.
The @ is not at the proper location, and may also need to go since Data is not an attribute but an element. You could try to change
select X.value('(ss:Row/ss:Cell/ss:@Data)','varchar(max)')
from @allUsers.nodes('Workbook/Worksheet/Table') as T(X)
to
select X.value('(ss:Row/ss:Cell/ss:Data)[1]','varchar(max)')
from @allUsers.nodes('ss:Workbook/ss:Worksheet/ss:Table') as T(X)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.