How to read XML HTTP Response in SQL Server - sql-server

I need help with reading some nodes in the below XML, it's an HTTP XML response which I store in a variable and need to read the values.
I want to read all the nodes that have the v31,v11,v12 namespace prefix into a table using XQuery
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.mic.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3" xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<cor:SOATransactionID>96514584-be43-40f7-9335-b17f6d6669bd</cor:SOATransactionID>
</soapenv:Header>
<soapenv:Body xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.fl.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3" xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<v11:InfoResponse xmlns:v11="http://xmlns.fl.com/InfoResponse/V1">
<v31:ResponseHeader xmlns:v31="http://xmlns.fl.com/ResponseHeader/V3">
<v31:GeneralResponse>
<v31:correlationID>AD-nXfJhT5ZMVEmKkut</v31:correlationID>
<v31:status>OK</v31:status>
<v31:code>Successfully-001</v31:code>
<v31:description>successfully.</v31:description>
</v31:GeneralResponse>
</v31:ResponseHeader>
<v11:responseBody>
<v11:msisdn>+149012098788</v11:msisdn>
<v11:customer>
<v12:name xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:name>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/CustomerType/V1">5690001212</v12:clientCode>
<v12:firstName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:firstName>
<v12:lastName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">RA</v12:lastName>
<v12:birthDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">1997-08-07</v12:birthDate>
<v12:gender xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Female</v12:gender>
<v12:address xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:address>
<v12:email xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:email>
<v12:nationality xmlns:v12="http://xmlns.fl.com/CustomerType/V1">233</v12:nationality>
<v12:clientGrade xmlns:v12="http://xmlns.fl.com/CustomerType/V1">CustomerLevel2</v12:clientGrade>
<v12:clientType xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Company</v12:clientType>
<v12:clientState xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Unspecified</v12:clientState>
<v12:admissionDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">2022-07-16T08:19:20</v12:admissionDate>
<v12:additionalProperties xmlns:v12="http://xmlns.fl.com/CustomerType/V1">
<v2:ParameterType>
<v2:parameterName>Value</v2:parameterName>
<v2:parameterValue>0.0</v2:parameterValue>
</v2:ParameterType>
</v12:additionalProperties>
</v11:customer>
<v11:subscriber>
<v12:imsi xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990423434534</v12:imsi>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">7870559304234</v12:clientCode>
<v12:brandId xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">120</v12:brandId>
<v12:language xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:language>
<v12:smsLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:smsLanguage>
<v12:ussdLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:ussdLanguage>
<v12:customerType xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">Foreign</v12:customerType>
<v12:initialCredit xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">30</v12:initialCredit>
<v12:mainProductID xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990877687</v12:mainProductID>
</v11:subscriber>
</v11:responseBody>
</v11:InfoResponse>
</soapenv:Body>
</soapenv:Envelope>

Please try the following.
The XML sample has lots of namespaces. You should be disciplined with them as well as with XPath expressions.
SQL
DECLARE #xml XML =
N'<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.mic.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3"
xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<cor:SOATransactionID>96514584-be43-40f7-9335-b17f6d6669bd</cor:SOATransactionID>
</soapenv:Header>
<soapenv:Body xmlns:v1="http://xmlns.fl.com/InfoRequest/V1" xmlns:cor="http://soa.fl.co.af/coredata_1" xmlns:v3="http://xmlns.fl.com/RequestHeader/V3" xmlns:v2="http://xmlns.fl.com/ParameterType/V2">
<v11:InfoResponse xmlns:v11="http://xmlns.fl.com/InfoResponse/V1">
<v31:ResponseHeader xmlns:v31="http://xmlns.fl.com/ResponseHeader/V3">
<v31:GeneralResponse>
<v31:correlationID>AD-nXfJhT5ZMVEmKkut</v31:correlationID>
<v31:status>OK</v31:status>
<v31:code>Successfully-001</v31:code>
<v31:description>successfully.</v31:description>
</v31:GeneralResponse>
</v31:ResponseHeader>
<v11:responseBody>
<v11:msisdn>+149012098788</v11:msisdn>
<v11:customer>
<v12:name xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:name>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/CustomerType/V1">5690001212</v12:clientCode>
<v12:firstName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">FL Company</v12:firstName>
<v12:lastName xmlns:v12="http://xmlns.fl.com/CustomerType/V1">RA</v12:lastName>
<v12:birthDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">1997-08-07</v12:birthDate>
<v12:gender xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Female</v12:gender>
<v12:address xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:address>
<v12:email xmlns:v12="http://xmlns.fl.com/CustomerType/V1">example#fl.com</v12:email>
<v12:nationality xmlns:v12="http://xmlns.fl.com/CustomerType/V1">233</v12:nationality>
<v12:clientGrade xmlns:v12="http://xmlns.fl.com/CustomerType/V1">CustomerLevel2</v12:clientGrade>
<v12:clientType xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Company</v12:clientType>
<v12:clientState xmlns:v12="http://xmlns.fl.com/CustomerType/V1">Unspecified</v12:clientState>
<v12:admissionDate xmlns:v12="http://xmlns.fl.com/CustomerType/V1">2022-07-16T08:19:20</v12:admissionDate>
<v12:additionalProperties xmlns:v12="http://xmlns.fl.com/CustomerType/V1">
<v2:ParameterType>
<v2:parameterName>Value</v2:parameterName>
<v2:parameterValue>0.0</v2:parameterValue>
</v2:ParameterType>
</v12:additionalProperties>
</v11:customer>
<v11:subscriber>
<v12:imsi xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990423434534</v12:imsi>
<v12:clientCode xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">7870559304234</v12:clientCode>
<v12:brandId xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">120</v12:brandId>
<v12:language xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:language>
<v12:smsLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:smsLanguage>
<v12:ussdLanguage xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">English</v12:ussdLanguage>
<v12:customerType xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">Foreign</v12:customerType>
<v12:initialCredit xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">30</v12:initialCredit>
<v12:mainProductID xmlns:v12="http://xmlns.fl.com/SubscriberType/V1">990877687</v12:mainProductID>
</v11:subscriber>
</v11:responseBody>
</v11:InfoResponse>
</soapenv:Body>
</soapenv:Envelope>';
;WITH XMLNAMESPACES(DEFAULT 'http://schemas.xmlsoap.org/soap/envelope/'
, 'http://xmlns.fl.com/InfoResponse/V1' AS v11
, 'http://xmlns.fl.com/CustomerType/V1' AS v12
, 'http://xmlns.fl.com/SubscriberType/V1' AS v121)
SELECT r.value('(v11:msisdn/text())[1]','varchar(50)') AS msisdn
, c.value('(v12:name/text())[1]','varchar(50)') AS custname
, c.value('(v12:clientCode/text())[1]','varchar(50)') AS clientCode
, s.value('(v121:customerType/text())[1]','varchar(50)') AS customerType
, s.value('(v121:initialCredit/text())[1]','INT') AS initialCredit
FROM #XML.nodes('/Envelope/Body/v11:InfoResponse/v11:responseBody') AS t1(r)
CROSS APPLY t1.r.nodes('v11:customer') AS t2(c)
CROSS APPLY t1.r.nodes('v11:subscriber') AS t3(s);
Output
+---------------+------------+------------+--------------+---------------+
| msisdn | custname | clientCode | customerType | initialCredit |
+---------------+------------+------------+--------------+---------------+
| +149012098788 | FL Company | 5690001212 | Foreign | 30 |
+---------------+------------+------------+--------------+---------------+

Related

How to compare two array of string columns in Pyspark

I want to compare two arrays and filter the data frame
condition_1 = AAA
condition_2 = ["AAA","BBB","CCC"]
My spark data frame has a column with array of strings
df = df.withColumn("array_column", F.lit(["XXX","YYY","AAA"]))
# to filter a string condition_1 with the array column
df = df.filter(
F.col('array_column').isin(condition_1) &
# second filter here
)
But how can I filter condition_2 in in a similar way? since they are both arrays?
Code I tried:
df = df.filter(
F.col('array_column').isin(condition_1) &
any(x in condition_2 for x in F.col('array_column'))
)
But I get an error - Column is not iterable.
I also tried - bool(set(F.col('array_column')).intersection(condition_2))
But still have the same error. Can anyone help me with this?
Hope I got your question right. It wasnt as clear. Use pyspark's array functions
Data
condition_1 = 'AAA'
condition_2 = ["AAA","BBB","CCC"]
df=spark.createDataFrame([('1A', '3412asd','value-1', ['XXX', 'YYY', 'AAA']),
('2B', '2345tyu','value-2', ['DDD', 'YFFFYY', 'GGG']),
('3C', '9800bvd', 'value-3', ['AAA']),
('3C', '9800bvd', 'value-1', ['AAA', 'YYY', 'CCCC'])],
('ID', 'Company_Id', 'value' ,'array_column'))
df.show()
+---+----------+-------+------------------+
| ID|Company_Id| value| array_column|
+---+----------+-------+------------------+
| 1A| 3412asd|value-1| [XXX, YYY, AAA]|
| 2B| 2345tyu|value-2|[DDD, YFFFYY, GGG]|
| 3C| 9800bvd|value-3| [AAA]|
| 3C| 9800bvd|value-1| [AAA, YYY, CCCC]|
+---+----------+-------+------------------+
Code
df.where((array_contains(col('array_column'), lit(condition_1)))&(size(array_intersect(col('array_column'),array([lit(x) for x in condition_2])))!=0)).show(truncate=False)
Outcome
+---+----------+-------+----------------+
|ID |Company_Id|value |array_column |
+---+----------+-------+----------------+
|1A |3412asd |value-1|[XXX, YYY, AAA] |
|3C |9800bvd |value-3|[AAA] |
|3C |9800bvd |value-1|[AAA, YYY, CCCC]|
+---+----------+-------+----------------+
How it works
condition_1 ; get a boolean selection of where column contains string
array_contains(col('array_column'), lit(condition_1))
condition_2 ; This happens in stages
Intersect column with the list
array_intersect(col('array_column'),array([lit(x) for x in condition_2]))
get the size of the outcome of 1 above
size(array_intersect(col('array_column'),array([lit(x) for x in` condition_2])))
Check that the intersection contains at least one item
size(array_intersect(col('array_column'),array([lit(x) for x in condition_2])))!=0
Finally, chain condition_1 and condition_2 using operant & and pass into the df.where() or df.filter() methods

Compare two arrays from two different dataframes in Pyspark

I have two dataframes ecah has an array(string) columns.
I am trying to create a new data frame that only filters rows where one of the array element in a row matches with other.
#first dataframe
main_df = spark.createDataFrame([('1', ['YYY', 'MZA']),
('2', ['XXX','YYY']),
('3',['QQQ']),
('4', ['RRR', 'ZZZ', 'BBB1'])],
('No', 'refer_array_col'))
#second dataframe
df = spark.createDataFrame([('1A', '3412asd','value-1', ['XXX', 'YYY', 'AAA']),
('2B', '2345tyu','value-2', ['DDD', 'YFFFYY', 'GGG', '1']),
('3C', '9800bvd', 'value-3', ['AAA']),
('3C', '9800bvd', 'value-1', ['AAA', 'YYY', 'CCCC'])],
('ID', 'Company_Id', 'value' ,'array_column'))
df.show()
+---+----------+-------+--------------------+
| ID|Company_Id| value| array_column |
+---+----------+-------+--------------------+
| 1A| 3412asd|value-1| [XXX, YYY, AAA] |
| 2B| 2345tyu|value-2|[DDD, YFFFYY, GGG, 1]|
| 3C| 9800bvd|value-3| [AAA] |
| 3C| 9800bvd|value-1| [AAA, YYY, CCCC] |
+---+----------+-------+---------------------+
Code I tried:
The main idea is to use rdd.toLocalIterator() as there are some other functions inside the same for loop that are depending on this filters
for x in main_df.rdd.toLocalIterator:
a = main_df["refer_array_col"]
b = main_df["No"]
some_x_filter = F.col('array_coulmn').isin(b)
final_df = df.filter(
# filter 1
some_x_filter &
# second filter is to compare 'a' with array_column - i tried using F.array_contains
(F.array_contains(F.col('array_column'), F.lit(a)))
)
some_x_filter is also working in a similar way
some_x_filter is comparing a string value in a array of strings column.
But now a contains a list of strings and I am unable to compare it with array_column
With my code I am getting an error for array contains
Error
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.sql.functions.lit.
: java.lang.RuntimeException: Unsupported literal type class java.util.ArrayList ['YYY', 'MZA']
Can anyone tell me what can i use at the second filter alternatively?
From what I understood based on our conversation in the comments.
Essentially your requirement is to compare an array column with a Python List.
Thus, this would do the job
df.withColumn("asArray", F.array(*[F.lit(x) for x in b]))

spring data select query performance problems when using IN clause with a collection of objects

I am experiencing a weird behavior using select query IN clause with a collection (I am using MSSQL on Azure).
I have the following code:
final Query query = em.createNativeQuery("select p.id as productId, p.external_id as externalId from products p where p.id in (:ids)");
// the ids list is of size 750
query.setParameter("ids", ids);
query.getResultList();
This code takes about 4 minutes to run, however running the query directly from the MSSQL client, it takes 1-2 seconds.
I was suspecting the in clause and I modified the code:
final String queryString = "select p.id as productId, p.external_id as externalId from products p where p.id in (%s)";
String inClause = ...; // generate a string of type 'id1', 'id2',...'id750'
final Query query = em.createNativeQuery(String.format(queryString, inClause));
query.getResultList();
The second version takes only 10 seconds to run.
Do you know any specific known problem running in clause queries? Can you suggest any solution how to get this working?
UPDATE:
I found out that the problem is actually happening with parameterized query, I tried to use a jdbc template and use my own wrapper to iterate over the results set. I iterated over the results set only printed the index, I could see that after about 590 results the results set hangs for about 3-4 minutes and then continues and finishes.
UPDATE: the id column which is the PK, is a char[36] column
I can see the query generated on Azure MSSSQL:
(#P749 nvarchar(4000),#P748 nvarchar(4000),#P747 nvarchar(4000),#P746 nvarchar(4000),#P745 nvarchar(4000),#P744 nvarchar(4000),#P743 nvarchar(4000),#P742 nvarchar(4000),#P741 nvarchar(4000),#P740 nvarchar(4000),#P739 nvarchar(4000),#P738 nvarchar(4000),#P737 nvarchar(4000),#P736 nvarchar(4000),#P735 nvarchar(4000),#P734 nvarchar(4000),#P733 nvarchar(4000),#P732 nvarchar(4000),#P731 nvarchar(4000),#P730 nvarchar(4000),#P729 nvarchar(4000),#P728 nvarchar(4000),#P727 nvarchar(4000),#P726 nvarchar(4000),#P725 nvarchar(4000),#P724 nvarchar(4000),#P723 nvarchar(4000),#P722 nvarchar(4000),#P721 nvarchar(4000),#P720 nvarchar(4000),#P719 nvarchar(4000),#P718 nvarchar(4000),#P717 nvarchar(4000),#P716 nvarchar(4000),#P715 nvarchar(4000),#P714 nvarchar(4000),#P713 nvarchar(4000),#P712 nvarchar(4000),#P711 nvarchar(4000),#P710 nvarchar(4000),#P709 nvarchar(4000),#P708 nvarchar(4000),#P707 nvarchar(4000),#P706 nvarchar(4000),#P705 nvarchar(4000),#P704 nvarchar(4000),#P703 nvarchar(4000),#P702 nvarchar(4000),#P701 nvarchar(4000),#P700 nvarchar(4000),#P699 nvarchar(4000),#P698 nvarchar(4000),#P697 nvarchar(4000),#P696 nvarchar(4000),#P695 nvarchar(4000),#P694 nvarchar(4000),#P693 nvarchar(4000),#P692 nvarchar(4000),#P691 nvarchar(4000),#P690 nvarchar(4000),#P689 nvarchar(4000),#P688 nvarchar(4000),#P687 nvarchar(4000),#P686 nvarchar(4000),#P685 nvarchar(4000),#P684 nvarchar(4000),#P683 nvarchar(4000),#P682 nvarchar(4000),#P681 nvarchar(4000),#P680 nvarchar(4000),#P679 nvarchar(4000),#P678 nvarchar(4000),#P677 nvarchar(4000),#P676 nvarchar(4000),#P675 nvarchar(4000),#P674 nvarchar(4000),#P673 nvarchar(4000),#P672 nvarchar(4000),#P671 nvarchar(4000),#P670 nvarchar(4000),#P669 nvarchar(4000),#P668 nvarchar(4000),#P667 nvarchar(4000),#P666 nvarchar(4000),#P665 nvarchar(4000),#P664 nvarchar(4000),#P663 nvarchar(4000),#P662 nvarchar(4000),#P661 nvarchar(4000),#P660 nvarchar(4000),#P659 nvarchar(4000),#P658 nvarchar(4000),#P657 nvarchar(4000),#P656 nvarchar(4000),#P655 nvarchar(4000),#P654 nvarchar(4000),#P653 nvarchar(4000),#P652 nvarchar(4000),#P651 nvarchar(4000),#P650 nvarchar(4000),#P649 nvarchar(4000),#P648 nvarchar(4000),#P647 nvarchar(4000),#P646 nvarchar(4000),#P645 nvarchar(4000),#P644 nvarchar(4000),#P643 nvarchar(4000),#P642 nvarchar(4000),#P641 nvarchar(4000),#P640 nvarchar(4000),#P639 nvarchar(4000),#P638 nvarchar(4000),#P637 nvarchar(4000),#P636 nvarchar(4000),#P635 nvarchar(4000),#P634 nvarchar(4000),#P633 nvarchar(4000),#P632 nvarchar(4000),#P631 nvarchar(4000),#P630 nvarchar(4000),#P629 nvarchar(4000),#P628 nvarchar(4000),#P627 nvarchar(4000),#P626 nvarchar(4000),#P625 nvarchar(4000),#P624 nvarchar(4000),#P623 nvarchar(4000),#P622 nvarchar(4000),#P621 nvarchar(4000),#P620 nvarchar(4000),#P619 nvarchar(4000),#P618 nvarchar(4000),#P617 nvarchar(4000),#P616 nvarchar(4000),#P615 nvarchar(4000),#P614 nvarchar(4000),#P613 nvarchar(4000),#P612 nvarchar(4000),#P611 nvarchar(4000),#P610 nvarchar(4000),#P609 nvarchar(4000),#P608 nvarchar(4000),#P607 nvarchar(4000),#P606 nvarchar(4000),#P605 nvarchar(4000),#P604 nvarchar(4000),#P603 nvarchar(4000),#P602 nvarchar(4000),#P601 nvarchar(4000),#P600 nvarchar(4000),#P599 nvarchar(4000),#P598 nvarchar(4000),#P597 nvarchar(4000),#P596 nvarchar(4000),#P595 nvarchar(4000),#P594 nvarchar(4000),#P593 nvarchar(4000),#P592 nvarchar(4000),#P591 nvarchar(4000),#P590 nvarchar(4000),#P589 nvarchar(4000),#P588 nvarchar(4000),#P587 nvarchar(4000),#P586 nvarchar(4000),#P585 nvarchar(4000),#P584 nvarchar(4000),#P583 nvarchar(4000),#P582 nvarchar(4000),#P581 nvarchar(4000),#P580 nvarchar(4000),#P579 nvarchar(4000),#P578 nvarchar(4000),#P577 nvarchar(4000),#P576 nvarchar(4000),#P575 nvarchar(4000),#P574 nvarchar(4000),#P573 nvarchar(4000),#P572 nvarchar(4000),#P571 nvarchar(4000),#P570 nvarchar(4000),#P569 nvarchar(4000),#P568 nvarchar(4000),#P567 nvarchar(4000),#P566 nvarchar(4000),#P565 nvarchar(4000),#P564 nvarchar(4000),#P563 nvarchar(4000),#P562 nvarchar(4000),#P561 nvarchar(4000),#P560 nvarchar(4000),#P559 nvarchar(4000),#P558 nvarchar(4000),#P557 nvarchar(4000),#P556 nvarchar(4000),#P555 nvarchar(4000),#P554 nvarchar(4000),#P553 nvarchar(4000),#P552 nvarchar(4000),#P551 nvarchar(4000),#P550 nvarchar(4000),#P549 nvarchar(4000),#P548 nvarchar(4000),#P547 nvarchar(4000),#P546 nvarchar(4000),#P545 nvarchar(4000),#P544 nvarchar(4000),#P543 nvarchar(4000),#P542 nvarchar(4000),#P541 nvarchar(4000),#P540 nvarchar(4000),#P539 nvarchar(4000),#P538 nvarchar(4000),#P537 nvarchar(4000),#P536 nvarchar(4000),#P535 nvarchar(4000),#P534 nvarchar(4000),#P533 nvarchar(4000),#P532 nvarchar(4000),#P531 nvarchar(4000),#P530 nvarchar(4000),#P529 nvarchar(4000),#P528 nvarchar(4000),#P527 nvarchar(4000),#P526 nvarchar(4000),#P525 nvarchar(4000),#P524 nvarchar(4000),#P523 nvarchar(4000),#P522 nvarchar(4000),#P521 nvarchar(4000),#P520 nvarchar(4000),#P519 nvarchar(4000),#P518 nvarchar(4000),#P517 nvarchar(4000),#P516 nvarchar(4000),#P515 nvarchar(4000),#P514 nvarchar(4000),#P513 nvarchar(4000),#P512 nvarchar(4000),#P511 nvarchar(4000),#P510 nvarchar(4000),#P509 nvarchar(4000),#P508 nvarchar(4000),#P507 nvarchar(4000),#P506 nvarchar(4000),#P505 nvarchar(4000),#P504 nvarchar(4000),#P503 nvarchar(4000),#P502 nvarchar(4000),#P501 nvarchar(4000),#P500 nvarchar(4000),#P499 nvarchar(4000),#P498 nvarchar(4000),#P497 nvarchar(4000),#P496 nvarchar(4000),#P495 nvarchar(4000),#P494 nvarchar(4000),#P493 nvarchar(4000),#P492 nvarchar(4000),#P491 nvarchar(4000),#P490 nvarchar(4000),#P489 nvarchar(4000),#P488 nvarchar(4000),#P487 nvarchar(4000),#P486 nvarchar(4000),#P485 nvarchar(4000),#P484 nvarchar(4000),#P483 nvarchar(4000),#P482 nvarchar(4000),#P481 nvarchar(4000),#P480 nvarchar(4000),#P479 nvarchar(4000),#P478 nvarchar(4000),#P477 nvarchar(4000),#P476 nvarchar(4000),#P475 nvarchar(4000),#P474 nvarchar(4000),#P473 nvarchar(4000),#P472 nvarchar(4000),#P471 nvarchar(4000),#P470 nvarchar(4000),#P469 nvarchar(4000),#P468 nvarchar(4000),#P467 nvarchar(4000),#P466 nvarchar(4000),#P465 nvarchar(4000),#P464 nvarchar(4000),#P463 nvarchar(4000),#P462 nvarchar(4000),#P461 nvarchar(4000),#P460 nvarchar(4000),#P459 nvarchar(4000),#P458 nvarchar(4000),#P457 nvarchar(4000),#P456 nvarchar(4000),#P455 nvarchar(4000),#P454 nvarchar(4000),#P453 nvarchar(4000),#P452 nvarchar(4000),#P451 nvarchar(4000),#P450 nvarchar(4000),#P449 nvarchar(4000),#P448 nvarchar(4000),#P447 nvarchar(4000),#P446 nvarchar(4000),#P445 nvarchar(4000),#P444 nvarchar(4000),#P443 nvarchar(4000),#P442 nvarchar(4000),#P441 nvarchar(4000),#P440 nvarchar(4000),#P439 nvarchar(4000),#P438 nvarchar(4000),#P437 nvarchar(4000),#P436 nvarchar(4000),#P435 nvarchar(4000),#P434 nvarchar(4000),#P433 nvarchar(4000),#P432 nvarchar(4000),#P431 nvarchar(4000),#P430 nvarchar(4000),#P429 nvarchar(4000),#P428 nvarchar(4000),#P427 nvarchar(4000),#P426 nvarchar(4000),#P425 nvarchar(4000),#P424 nvarchar(4000),#P423 nvarchar(4000),#P422 nvarchar(4000),#P421 nvarchar(4000),#P420 nvarchar(4000),#P419 nvarchar(4000),#P418 nvarchar(4000),#P417 nvarchar(4000),#P416 nvarchar(4000),#P415 nvarchar(4000),#P414 nvarchar(4000),#P413 nvarchar(4000),#P412 nvarchar(4000),#P411 nvarchar(4000),#P410 nvarchar(4000),#P409 nvarchar(4000),#P408 nvarchar(4000),#P407 nvarchar(4000),#P406 nvarchar(4000),#P405 nvarchar(4000),#P404 nvarchar(4000),#P403 nvarchar(4000),#P402 nvarchar(4000),#P401 nvarchar(4000),#P400 nvarchar(4000),#P399 nvarchar(4000),#P398 nvarchar(4000),#P397 nvarchar(4000),#P396 nvarchar(4000),#P395 nvarchar(4000),#P394 nvarchar(4000),#P393 nvarchar(4000),#P392 nvarchar(4000),#P391 nvarchar(4000),#P390 nvarchar(4000),#P389 nvarchar(4000),#P388 nvarchar(4000),#P387 nvarchar(4000),#P386 nvarchar(4000),#P385 nvarchar(4000),#P384 nvarchar(4000),#P383 nvarchar(4000),#P382 nvarchar(4000),#P381 nvarchar(4000),#P380 nvarchar(4000),#P379 nvarchar(4000),#P378 nvarchar(4000),#P377 nvarchar(4000),#P376 nvarchar(4000),#P375 nvarchar(4000),#P374 nvarchar(4000),#P373 nvarchar(4000),#P372 nvarchar(4000),#P371 nvarchar(4000),#P370 nvarchar(4000),#P369 nvarchar(4000),#P368 nvarchar(4000),#P367 nvarchar(4000),#P366 nvarchar(4000),#P365 nvarchar(4000),#P364 nvarchar(4000),#P363 nvarchar(4000),#P362 nvarchar(4000),#P361 nvarchar(4000),#P360 nvarchar(4000),#P359 nvarchar(4000),#P358 nvarchar(4000),#P357 nvarchar(4000),#P356 nvarchar(4000),#P355 nvarchar(4000),#P354 nvarchar(4000),#P353 nvarchar(4000),#P352 nvarchar(4000),#P351 nvarchar(4000),#P350 nvarchar(4000),#P349 nvarchar(4000),#P348 nvarchar(4000),#P347 nvarchar(4000),#P346 nvarchar(4000),#P345 nvarchar(4000),#P344 nvarchar(4000),#P343 nvarchar(4000),#P342 nvarchar(4000),#P341 nvarchar(4000),#P340 nvarchar(4000),#P339 nvarchar(4000),#P338 nvarchar(4000),#P337 nvarchar(4000),#P336 nvarchar(4000),#P335 nvarchar(4000),#P334 nvarchar(4000),#P333 nvarchar(4000),#P332 nvarchar(4000),#P331 nvarchar(4000),#P330 nvarchar(4000),#P329 nvarchar(4000),#P328 nvarchar(4000),#P327 nvarchar(4000),#P326 nvarchar(4000),#P325 nvarchar(4000),#P324 nvarchar(4000),#P323 nvarchar(4000),#P322 nvarchar(4000),#P321 nvarchar(4000),#P320 nvarchar(4000),#P319 nvarchar(4000),#P318 nvarchar(4000),#P317 nvarchar(4000),#P316 nvarchar(4000),#P315 nvarchar(4000),#P314 nvarchar(4000),#P313 nvarchar(4000),#P312 nvarchar(4000),#P311 nvarchar(4000),#P310 nvarchar(4000),#P309 nvarchar(4000),#P308 nvarchar(4000),#P307 nvarchar(4000),#P306 nvarchar(4000),#P305 nvarchar(4000),#P304 nvarchar(4000),#P303 nvarchar(4000),#P302 nvarchar(4000),#P301 nvarchar(4000),#P300 nvarchar(4000),#P299 nvarchar(4000),#P298 nvarchar(4000),#P297 nvarchar(4000),#P296 nvarchar(4000),#P295 nvarchar(4000),#P294 nvarchar(4000),#P293 nvarchar(4000),#P292 nvarchar(4000),#P291 nvarchar(4000),#P290 nvarchar(4000),#P289 nvarchar(4000),#P288 nvarchar(4000),#P287 nvarchar(4000),#P286 nvarchar(4000),#P285 nvarchar(4000),#P284 nvarchar(4000),#P283 nvarchar(4000),#P282 nvarchar(4000),#P281 nvarchar(4000),#P280 nvarchar(4000),#P279 nvarchar(4000),#P278 nvarchar(4000),#P277 nvarchar(4000),#P276 nvarchar(4000),#P275 nvarchar(4000),#P274 nvarchar(4000),#P273 nvarchar(4000),#P272 nvarchar(4000),#P271 nvarchar(4000),#P270 nvarchar(4000),#P269 nvarchar(4000),#P268 nvarchar(4000),#P267 nvarchar(4000),#P266 nvarchar(4000),#P265 nvarchar(4000),#P264 nvarchar(4000),#P263 nvarchar(4000),#P262 nvarchar(4000),#P261 nvarchar(4000),#P260 nvarchar(4000),#P259 nvarchar(4000),#P258 nvarchar(4000),#P257 nvarchar(4000),#P256 nvarchar(4000),#P255 nvarchar(4000),#P254 nvarchar(4000),#P253 nvarchar(4000),#P252 nvarchar(4000),#P251 nvarchar(4000),#P250 nvarchar(4000),#P249 nvarchar(4000),#P248 nvarchar(4000),#P247 nvarchar(4000),#P246 nvarchar(4000),#P245 nvarchar(4000),#P244 nvarchar(4000),#P243 nvarchar(4000),#P242 nvarchar(4000),#P241 nvarchar(4000),#P240 nvarchar(4000),#P239 nvarchar(4000),#P238 nvarchar(4000),#P237 nvarchar(4000),#P236 nvarchar(4000),#P235 nvarchar(4000),#P234 nvarchar(4000),#P233 nvarchar(4000),#P232 nvarchar(4000),#P231 nvarchar(4000),#P230 nvarchar(4000),#P229 nvarchar(4000),#P228 nvarchar(4000),#P227 nvarchar(4000),#P226 nvarchar(4000),#P225 nvarchar(4000),#P224 nvarchar(4000),#P223 nvarchar(4000),#P222 nvarchar(4000),#P221 nvarchar(4000),#P220 nvarchar(4000),#P219 nvarchar(4000),#P218 nvarchar(4000),#P217 nvarchar(4000),#P216 nvarchar(4000),#P215 nvarchar(4000),#P214 nvarchar(4000),#P213 nvarchar(4000),#P212 nvarchar(4000),#P211 nvarchar(4000),#P210 nvarchar(4000),#P209 nvarchar(4000),#P208 nvarchar(4000),#P207 nvarchar(4000),#P206 nvarchar(4000),#P205 nvarchar(4000),#P204 nvarchar(4000),#P203 nvarchar(4000),#P202 nvarchar(4000),#P201 nvarchar(4000),#P200 nvarchar(4000),#P199 nvarchar(4000),#P198 nvarchar(4000),#P197 nvarchar(4000),#P196 nvarchar(4000),#P195 nvarchar(4000),#P194 nvarchar(4000),#P193 nvarchar(4000),#P192 nvarchar(4000),#P191 nvarchar(4000),#P190 nvarchar(4000),#P189 nvarchar(4000),#P188 nvarchar(4000),#P187 nvarchar(4000),#P186 nvarchar(4000),#P185 nvarchar(4000),#P184 nvarchar(4000),#P183 nvarchar(4000),#P182 nvarchar(4000),#P181 nvarchar(4000),#P180 nvarchar(4000),#P179 nvarchar(4000),#P178 nvarchar(4000),#P177 nvarchar(4000),#P176 nvarchar(4000),#P175 nvarchar(4000),#P174 nvarchar(4000),#P173 nvarchar(4000),#P172 nvarchar(4000),#P171 nvarchar(4000),#P170 nvarchar(4000),#P169 nvarchar(4000),#P168 nvarchar(4000),#P167 nvarchar(4000),#P166 nvarchar(4000),#P165 nvarchar(4000),#P164 nvarchar(4000),#P163 nvarchar(4000),#P162 nvarchar(4000),#P161 nvarchar(4000),#P160 nvarchar(4000),#P159 nvarchar(4000),#P158 nvarchar(4000),#P157 nvarchar(4000),#P156 nvarchar(4000),#P155 nvarchar(4000),#P154 nvarchar(4000),#P153 nvarchar(4000),#P152 nvarchar(4000),#P151 nvarchar(4000),#P150 nvarchar(4000),#P149 nvarchar(4000),#P148 nvarchar(4000),#P147 nvarchar(4000),#P146 nvarchar(4000),#P145 nvarchar(4000),#P144 nvarchar(4000),#P143 nvarchar(4000),#P142 nvarchar(4000),#P141 nvarchar(4000),#P140 nvarchar(4000),#P139 nvarchar(4000),#P138 nvarchar(4000),#P137 nvarchar(4000),#P136 nvarchar(4000),#P135 nvarchar(4000),#P134 nvarchar(4000),#P133 nvarchar(4000),#P132 nvarchar(4000),#P131 nvarchar(4000),#P130 nvarchar(4000),#P129 nvarchar(4000),#P128 nvarchar(4000),#P127 nvarchar(4000),#P126 nvarchar(4000),#P125 nvarchar(4000),#P124 nvarchar(4000),#P123 nvarchar(4000),#P122 nvarchar(4000),#P121 nvarchar(4000),#P120 nvarchar(4000),#P119 nvarchar(4000),#P118 nvarchar(4000),#P117 nvarchar(4000),#P116 nvarchar(4000),#P115 nvarchar(4000),#P114 nvarchar(4000),#P113 nvarchar(4000),#P112 nvarchar(4000),#P111 nvarchar(4000),#P110 nvarchar(4000),#P109 nvarchar(4000),#P108 nvarchar(4000),#P107 nvarchar(4000),#P106 nvarchar(4000),#P105 nvarchar(4000),#P104 nvarchar(4000),#P103 nvarchar(4000),#P102 nvarchar(4000),#P101 nvarchar(4000),#P100 nvarchar(4000),#P99 nvarchar(4000),#P98 nvarchar(4000),#P97 nvarchar(4000),#P96 nvarchar(4000),#P95 nvarchar(4000),#P94 nvarchar(4000),#P93 nvarchar(4000),#P92 nvarchar(4000),#P91 nvarchar(4000),#P90 nvarchar(4000),#P89 nvarchar(4000),#P88 nvarchar(4000),#P87 nvarchar(4000),#P86 nvarchar(4000),#P85 nvarchar(4000),#P84 nvarchar(4000),#P83 nvarchar(4000),#P82 nvarchar(4000),#P81 nvarchar(4000),#P80 nvarchar(4000),#P79 nvarchar(4000),#P78 nvarchar(4000),#P77 nvarchar(4000),#P76 nvarchar(4000),#P75 nvarchar(4000),#P74 nvarchar(4000),#P73 nvarchar(4000),#P72 nvarchar(4000),#P71 nvarchar(4000),#P70 nvarchar(4000),#P69 nvarchar(4000),#P68 nvarchar(4000),#P67 nvarchar(4000),#P66 nvarchar(4000),#P65 nvarchar(4000),#P64 nvarchar(4000),#P63 nvarchar(4000),#P62 nvarchar(4000),#P61 nvarchar(4000),#P60 nvarchar(4000),#P59 nvarchar(4000),#P58 nvarchar(4000),#P57 nvarchar(4000),#P56 nvarchar(4000),#P55 nvarchar(4000),#P54 nvarchar(4000),#P53 nvarchar(4000),#P52 nvarchar(4000),#P51 nvarchar(4000),#P50 nvarchar(4000),#P49 nvarchar(4000),#P48 nvarchar(4000),#P47 nvarchar(4000),#P46 nvarchar(4000),#P45 nvarchar(4000),#P44 nvarchar(4000),#P43 nvarchar(4000),#P42 nvarchar(4000),#P41 nvarchar(4000),#P40 nvarchar(4000),#P39 nvarchar(4000),#P38 nvarchar(4000),#P37 nvarchar(4000),#P36 nvarchar(4000),#P35 nvarchar(4000),#P34 nvarchar(4000),#P33 nvarchar(4000),#P32 nvarchar(4000),#P31 nvarchar(4000),#P30 nvarchar(4000),#P29 nvarchar(4000),#P28 nvarchar(4000),#P27 nvarchar(4000),#P26 nvarchar(4000),#P25 nvarchar(4000),#P24 nvarchar(4000),#P23 nvarchar(4000),#P22 nvarchar(4000),#P21 nvarchar(4000),#P20 nvarchar(4000),#P19 nvarchar(4000),#P18 nvarchar(4000),#P17 nvarchar(4000),#P16 nvarchar(4000),#P15 nvarchar(4000),#P14 nvarchar(4000),#P13 nvarchar(4000),#P12 nvarchar(4000),#P11 nvarchar(4000),#P10 nvarchar(4000),#P9 nvarchar(4000),#P8 nvarchar(4000),#P7 nvarchar(4000),#P6 nvarchar(4000),#P5 nvarchar(4000),#P4 nvarchar(4000),#P3 nvarchar(4000),#P2 nvarchar(4000),#P1 nvarchar(4000),#P0 nvarchar(4000))select p.id as productId, p.external_id as externalId from products p where p.id in (#P0, #P1, #P2, #P3, #P4, #P5, #P6, #P7, #P8, #P9, #P10, #P11, #P12, #P13, #P14, #P15, #P16, #P17, #P18, #P19, #P20, #P21, #P22, #P23, #P24, #P25, #P26, #P27, #P28, #P29, #P30, #P31, #P32, #P33, #P34, #P35, #P36, #P37, #P38, #P39, #P40, #P41, #P42, #P43, #P44, #P45, #P46, #P47, #P48, #P49, #P50, #P51, #P52, #P53, #P54, #P55, #P56, #P57, #P58, #P59, #P60, #P61, #P62, #P63, #P64, #P65, #P66, #P67, #P68, #P69, #P70, #P71, #P72, #P73, #P74, #P75, #P76, #P77, #P78, #P79, #P80, #P81, #P82, #P83, #P84, #P85, #P86, #P87, #P88, #P89, #P90, #P91, #P92, #P93, #P94, #P95, #P96, #P97, #P98, #P99, #P100, #P101, #P102, #P103, #P104, #P105, #P106, #P107, #P108, #P109, #P110, #P111, #P112, #P113, #P114, #P115, #P116, #P117, #P118, #P119, #P120, #P121, #P122, #P123, #P124, #P125, #P126, #P127, #P128, #P129, #P130, #P131, #P132, #P133, #P134, #P135, #P136, #P137, #P138, #P139, #P140, #P141, #P142, #P143, #P144, #P145, #P146, #P147, #P148, #P149, #P150, #P151, #P152, #P153, #P154, #P155, #P156, #P157, #P158, #P159, #P160, #P161, #P162, #P163, #P164, #P165, #P166, #P167, #P168, #P169, #P170, #P171, #P172, #P173, #P174, #P175, #P176, #P177, #P178, #P179, #P180, #P181, #P182, #P183, #P184, #P185, #P186, #P187, #P188, #P189, #P190, #P191, #P192, #P193, #P194, #P195, #P196, #P197, #P198, #P199, #P200, #P201, #P202, #P203, #P204, #P205, #P206, #P207, #P208, #P209, #P210, #P211, #P212, #P213, #P214, #P215, #P216, #P217, #P218, #P219, #P220, #P221, #P222, #P223, #P224, #P225, #P226, #P227, #P228, #P229, #P230, #P231, #P232, #P233, #P234, #P235, #P236, #P237, #P238, #P239, #P240, #P241, #P242, #P243, #P244, #P245, #P246, #P247, #P248, #P249, #P250, #P251, #P252, #P253, #P254, #P255, #P256, #P257, #P258, #P259, #P260, #P261, #P262, #P263, #P264, #P265, #P266, #P267, #P268, #P269, #P270, #P271, #P272, #P273, #P274, #P275, #P276, #P277, #P278, #P279, #P280, #P281, #P282, #P283, #P284, #P285, #P286, #P287, #P288, #P289, #P290, #P291, #P292, #P293, #P294, #P295, #P296, #P297, #P298, #P299, #P300, #P301, #P302, #P303, #P304, #P305, #P306, #P307, #P308, #P309, #P310, #P311, #P312, #P313, #P314, #P315, #P316, #P317, #P318, #P319, #P320, #P321, #P322, #P323, #P324, #P325, #P326, #P327, #P328, #P329, #P330, #P331, #P332, #P333, #P334, #P335, #P336, #P337, #P338, #P339, #P340, #P341, #P342, #P343, #P344, #P345, #P346, #P347, #P348, #P349, #P350, #P351, #P352, #P353, #P354, #P355, #P356, #P357, #P358, #P359, #P360, #P361, #P362, #P363, #P364, #P365, #P366, #P367, #P368, #P369, #P370, #P371, #P372, #P373, #P374, #P375, #P376, #P377, #P378, #P379, #P380, #P381, #P382, #P383, #P384, #P385, #P386, #P387, #P388, #P389, #P390, #P391, #P392, #P393, #P394, #P395, #P396, #P397, #P398, #P399, #P400, #P401, #P402, #P403, #P404, #P405, #P406, #P407, #P408, #P409, #P410, #P411, #P412, #P413, #P414, #P415, #P416, #P417, #P418, #P419, #P420, #P421, #P422, #P423, #P424, #P425, #P426, #P427, #P428, #P429, #P430, #P431, #P432, #P433, #P434, #P435, #P436, #P437, #P438, #P439, #P440, #P441, #P442, #P443, #P444, #P445, #P446, #P447, #P448, #P449, #P450, #P451, #P452, #P453, #P454, #P455, #P456, #P457, #P458, #P459, #P460, #P461, #P462, #P463, #P464, #P465, #P466, #P467, #P468, #P469, #P470, #P471, #P472, #P473, #P474, #P475, #P476, #P477, #P478, #P479, #P480, #P481, #P482, #P483, #P484, #P485, #P486, #P487, #P488, #P489, #P490, #P491, #P492, #P493, #P494, #P495, #P496, #P497, #P498, #P499, #P500, #P501, #P502, #P503, #P504, #P505, #P506, #P507, #P508, #P509, #P510, #P511, #P512, #P513, #P514, #P515, #P516, #P517, #P518, #P519, #P520, #P521, #P522, #P523, #P524, #P525, #P526, #P527, #P528, #P529, #P530, #P531, #P532, #P533, #P534, #P535, #P536, #P537, #P538, #P539, #P540, #P541, #P542, #P543, #P544, #P545, #P546, #P547, #P548, #P549, #P550, #P551, #P552, #P553, #P554, #P555, #P556, #P557, #P558, #P559, #P560, #P561, #P562, #P563, #P564, #P565, #P566, #P567, #P568, #P569, #P570, #P571, #P572, #P573, #P574, #P575, #P576, #P577, #P578, #P579, #P580, #P581, #P582, #P583, #P584, #P585, #P586, #P587, #P588, #P589, #P590, #P591, #P592, #P593, #P594, #P595, #P596, #P597, #P598, #P599, #P600, #P601, #P602, #P603, #P604, #P605, #P606, #P607, #P608, #P609, #P610, #P611, #P612, #P613, #P614, #P615, #P616, #P617, #P618, #P619, #P620, #P621, #P622, #P623, #P624, #P625, #P626, #P627, #P628, #P629, #P630, #P631, #P632, #P633, #P634, #P635, #P636, #P637, #P638, #P639, #P640, #P641, #P642, #P643, #P644, #P645, #P646, #P647, #P648, #P649, #P650, #P651, #P652, #P653, #P654, #P655, #P656, #P657, #P658, #P659, #P660, #P661, #P662, #P663, #P664, #P665, #P666, #P667, #P668, #P669, #P670, #P671, #P672, #P673, #P674, #P675, #P676, #P677, #P678, #P679, #P680, #P681, #P682, #P683, #P684, #P685, #P686, #P687, #P688, #P689, #P690, #P691, #P692, #P693, #P694, #P695, #P696, #P697, #P698, #P699, #P700, #P701, #P702, #P703, #P704, #P705, #P706, #P707, #P708, #P709, #P710, #P711, #P712, #P713, #P714, #P715, #P716, #P717, #P718, #P719, #P720, #P721, #P722, #P723, #P724, #P725, #P726, #P727, #P728, #P729, #P730, #P731, #P732, #P733, #P734, #P735, #P736, #P737, #P738, #P739, #P740, #P741, #P742, #P743, #P744, #P745, #P746, #P747, #P748, #P749)
I was trying to contact Azure's support and they recommended to add a FORCESEEK hint to the query - Do you know how to add this query hint to the springdata repository?
Thank you.
The problem in this case is that the parameters are handles as Unicode (nvarchar) and my indexes where Ascii [char(36)]. Due to that reason it seems the searches are not executed using the indexes which make is very small. It seems like a normal behavior since Java strings are Unicode and there is no real way for the DB to down convert Unicode to Ascii.
After modifying my id columns (PK and FK) from char(36) to nchar(36) the parameterized queries run as expected and running a search with IN clause of 2,000 ids takes about 400msec instead of almost 10 minutes.

How to import XML to SQL Server when base nodes repeat

I am importing generated XML files into a SQL Server database. It's been working fine, but now I need additional information from a node that repeats, and I can't figure it out. I am using VBScript with SQLXMLBulkLoad.SQLXMLBulkload.4.0 to import the files. There are many <file> nodes in each file, and here a simplified as an example:
<?xml version="1.0" encoding="UTF-8"?>
<samp_catalog version='1.0'>
<file>
<name>123.wav</name>
<xyz>
<a_samp>
<a_one>1</a_one>
<a_two>2</a_two>
<a_three>3</a_three>
</a_samp>
<b_samp version='2.0'>
<b_four>sample</b_four>
<b_five>sample</b_five>
<b_six>sample</b_six>
</b_samp>
<c_samp version='1.0'>
<c_entry>
<total_size>0</total_size>
<sig>sample</sig>
<type>SAMPLE</type>
<data_size>256</data_size>
<data encoding='string'></data>
</c_entry>
</c_samp>
<c_samp version='1.0'>
<c_entry>
<total_size>0</total_size>
<sig>sample</sig>
<type>PHONE</type>
<data_size>256</data_size>
<data encoding='string'>15555551212</data>
</c_entry>
</c_samp>
<c_samp version='1.0'>
<c_entry>
<total_size>0</total_size>
<sig>sample</sig>
<type>OTHER_SAMPLE</type>
<data_size>256</data_size>
<data encoding='string'></data>
</c_entry>
</c_samp>
</xyz>
</file>
</samp_catalog>
Without the repeating c_samp node, the schema was easy:
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema" xmlns:sql = "urn:schemas-microsoft-com:mapping-schema">
<xsd:element name = "samp_catalog" sql:is-constant = "1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "file" sql:relation = "files" maxOccurs = "unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "name" type = "xsd:string" sql:field = "name" />
<xsd:element name = "xyz" sql:is-constant = "1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "a_samp" sql:is-constant = "1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "a_one" type = "xsd:integer" sql:field = "a_one" />
<xsd:element name = "a_two" type = "xsd:integer" sql:field = "a_two" />
<xsd:element name = "a_three" type = "xsd:integer" sql:field = "a_three" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name = "b_samp" sql:is-constant = "1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "b_four" type = "xsd:string" sql:field = "b_four" />
<xsd:element name = "b_five" type = "xsd:string" sql:field = "b_five" />
<xsd:element name = "b_six" type = "xsd:string" sql:field = "b_six" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The important one is the second data element (of type PHONE), but importing them all is acceptable as well. I have tried defining all 3 c_samps in the schema and defining a sql:field for only the specified data element and defining it for all elements, however the import always results in NULL for everything under a c_samp element.
How can I get the repeating nodes to import into the database?
I would write a stored procedure in SQL-Server and pass over the XML as-is. Use SQL Server's abilities to read the XML without a schema.
The following code will extract all information from the XML given.
Just do something like
CREATE dbo.ImportMyXML(#xml XML)
AS
BEGIN
--Code here
END
GO
Within the BEGIN END you write this query (out-commented the INSERT INTO to test the query's result:
--INSERT INTO YourTargetTable(col1,col2,col3,...)
SELECT #xml.value(N'(/samp_catalog/file/name/text())[1]',N'nvarchar(max)') AS name
,a_samp.value(N'(*/text())[1]',N'nvarchar(max)') AS a_one
,a_samp.value(N'(*/text())[2]',N'nvarchar(max)') AS a_two
,a_samp.value(N'(*/text())[3]',N'nvarchar(max)') AS a_three
,b_samp.value(N'#version',N'nvarchar(max)') AS b_version
,b_samp.value(N'(*/text())[1]',N'nvarchar(max)') AS b_one
,b_samp.value(N'(*/text())[2]',N'nvarchar(max)') AS b_two
,b_samp.value(N'(*/text())[3]',N'nvarchar(max)') AS b_three
,NodesWith_c_entry.value(N'(../#version)[1]',N'nvarchar(max)') AS c_version
,NodesWith_c_entry.value(N'(total_size/text())[1]','int') AS c_total_size
,NodesWith_c_entry.value(N'(sig/text())[1]','nvarchar(max)') AS c_sig
,NodesWith_c_entry.value(N'(type/text())[1]','nvarchar(max)') AS c_type
,NodesWith_c_entry.value(N'(data_size/text())[1]','int') AS c_data_size
,NodesWith_c_entry.value(N'(data/#encoding)[1]','nvarchar(max)') AS c_data_encoding
,NodesWith_c_entry.value(N'(data/text())[1]','nvarchar(max)') AS c_data_text
FROM #xml.nodes(N'samp_catalog/file/xyz') AS A(xyz)
OUTER APPLY xyz.nodes('a_samp') AS B(a_samp)
OUTER APPLY xyz.nodes('b_samp') AS C(b_samp)
OUTER APPLY xyZ.nodes('*/c_entry') AS D(NodesWith_c_entry);
With the XML given the result is this:
+---------+-------+-------+---------+-----------+--------+--------+---------+-----------+--------------+--------+--------------+-------------+-----------------+-------------+
| name | a_one | a_two | a_three | b_version | b_one | b_two | b_three | c_version | c_total_size | c_sig | c_type | c_data_size | c_data_encoding | c_data_text |
+---------+-------+-------+---------+-----------+--------+--------+---------+-----------+--------------+--------+--------------+-------------+-----------------+-------------+
| 123.wav | 1 | 2 | 3 | 2.0 | sample | sample | sample | 1.0 | 0 | sample | SAMPLE | 256 | string | NULL |
+---------+-------+-------+---------+-----------+--------+--------+---------+-----------+--------------+--------+--------------+-------------+-----------------+-------------+
| 123.wav | 1 | 2 | 3 | 2.0 | sample | sample | sample | 1.0 | 0 | sample | PHONE | 256 | string | 15555551212 |
+---------+-------+-------+---------+-----------+--------+--------+---------+-----------+--------------+--------+--------------+-------------+-----------------+-------------+
| 123.wav | 1 | 2 | 3 | 2.0 | sample | sample | sample | 1.0 | 0 | sample | OTHER_SAMPLE | 256 | string | NULL |
+---------+-------+-------+---------+-----------+--------+--------+---------+-----------+--------------+--------+--------------+-------------+-----------------+-------------+
So far, I haven't found an answer for what I was looking to do, but here is a work around I'm using for now. In VBScript, after loading the file with Microsoft.XMLDOM I loop through the file and find all type nodes whose text value is PHONE and then create a new element and append it to b_samp with its sibling data's text value.
VBScript:
Set nodes = xmlFile.selectNodes("//type")
For Each node in nodes
If node.text = "PHONE" Then
Set phoneNode = xmlFile.CreateElement("phone")
For Each child in node.ParentNode.ChildNodes
If child.nodeName = "data" Then phoneNode.text = child.text
Next
For Each child in node.ParentNode.ParentNode.ParentNode.ChildNodes
If child.nodeName = "b_samp" Then child.appendChild phoneNode
Next
End If
Next
Then save the file with a new name to leave the original intact, and import it with a new schema adding this line right after the b_six element:
<xsd:element name = "phone" type = "xsd:string" sql:field = "phone" />

read Xml into ms sql

I want to read the xml pasted below into sql. I tried several ways from the web to parse this xml without success.
I tried the following script:
CREATE TABLE [dbo].[Ejemplo2](
[RutEmisor][nvarchar](12) null,
[RutEnvia][nvarchar](12) null,
[RutReceptor][nvarchar](12) null,
[FchResol][nvarchar](12) null,
[NroResol][nvarchar](12) null
)
GO
DECLARE #messagebody XML
SELECT #messagebody = BulkColumn
FROM OPENROWSET(BULK 'C:\Ejemplo.xml', SINGLE_CLOB) AS X
INSERT INTO [dbo].[Ejemplo2]
select a.value(N'(./RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
a.value(N'(./RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
a.value(N'(./RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
a.value(N'(./FchResol)[1]', N'nvarchar(12)') as [FchResol],
a.value(N'(./NroResol)[1]', N'nvarchar(12)') as [NroResol]
from #messagebody.nodes('/EnvioDTE/SetDTE/Caratula') as r(a);
Select * from dbo.Ejemplo2
But I get 0 rows
I tried with more simple examples and always return the data, but in this case I can't figure whats is wrong.
Any help on this will be appreciated.
The xml follows
<?xml version="1.0" encoding="ISO-8859-1"?>
<EnvioDTE xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
<SetDTE ID="SetDoc">
<TmstFirmaEnv>2003-10-13T09:33:22</TmstFirmaEnv>
<Caratula version="1.0">
<RutEmisor>97975000-5</RutEmisor>
<RutEnvia>7880442-4</RutEnvia>
<RutReceptor>60803000-K</RutReceptor>
<FchResol>2003-09-02</FchResol>
<NroResol>0</NroResol>
<SubTotDTE>
<TpoDTE>33</TpoDTE>
<NroDTE>1</NroDTE>
</SubTotDTE>
</Caratula>
<DTE version="1.0">
<Documento ID="F60T33">
<Encabezado>
<IdDoc>
<TipoDTE>33</TipoDTE>
<Folio>60</Folio>
<FchEmis>2003-10-13</FchEmis>
</IdDoc>
<Emisor>
<RUTEmisor>97975000-5</RUTEmisor>
<RznSoc>RUT DE PRUEBA</RznSoc>
<GiroEmis>Insumos de Computacion</GiroEmis>
<Acteco>31341</Acteco>
<CdgSIISucur>1234</CdgSIISucur>
<DirOrigen>Teatinos 120, Piso 4</DirOrigen>
<CmnaOrigen>Santiago</CmnaOrigen>
<CiudadOrigen>Santiago</CiudadOrigen>
</Emisor>
<Receptor>
<RUTRecep>77777777-7</RUTRecep>
<RznSocRecep>EMPRESA LTDA</RznSocRecep>
<GiroRecep>COMPUTACION</GiroRecep>
<DirRecep>SAN DIEGO 2222</DirRecep>
<CmnaRecep>LA FLORIDA</CmnaRecep>
<CiudadRecep>SANTIAGO</CiudadRecep>
</Receptor>
<Totales>
<MntNeto>100000</MntNeto>
<TasaIVA>19</TasaIVA>
<IVA>19000</IVA>
<MntTotal>119000</MntTotal>
</Totales>
</Encabezado>
<Detalle>
<NroLinDet>1</NroLinDet>
<CdgItem>
<TpoCodigo>INT1</TpoCodigo>
<VlrCodigo>011</VlrCodigo>
</CdgItem>
<NmbItem>Parlantes Multimedia 180W.</NmbItem>
<DscItem/>
<QtyItem>20</QtyItem>
<PrcItem>4500</PrcItem>
<MontoItem>90000</MontoItem>
</Detalle>
<Detalle>
<NroLinDet>2</NroLinDet>
<CdgItem>
<TpoCodigo>INT1</TpoCodigo>
<VlrCodigo>0231</VlrCodigo>
</CdgItem>
<NmbItem>Mouse Inalambrico PS/2</NmbItem>
<DscItem/>
<QtyItem>1</QtyItem>
<PrcItem>5000</PrcItem>
<MontoItem>5000</MontoItem>
</Detalle>
<Detalle>
<NroLinDet>3</NroLinDet>
<CdgItem>
<TpoCodigo>INT1</TpoCodigo>
<VlrCodigo>1515</VlrCodigo>
</CdgItem>
<NmbItem>Caja de Diskettes 10 Unidades</NmbItem>
<DscItem/>
<QtyItem>5</QtyItem>
<PrcItem>1000</PrcItem>
<MontoItem>5000</MontoItem>
</Detalle>
<TED version="1.0">
<DD>
<RE>97975000-5</RE>
<TD>33</TD>
<F>60</F>
<FE>2003-10-13</FE>
<RR>77777777-7</RR>
<RSR>EMPRESA LTDA</RSR>
<MNT>119000</MNT>
<IT1>Parlantes Multimedia 180W.</IT1>
<CAF version="1.0">
<DA>
<RE>97975000-5</RE>
<RS>RUT DE PRUEBA</RS>
<TD>33</TD>
<RNG>
<D>1</D>
<H>200</H>
</RNG>
<FA>2003-09-04</FA>
<RSAPK>
<M>0a4O6Kbx8Qj3K4iWSP4w7KneZYeJ+g/prihYtIEolKt3cykSxl1zO8vSXu397QhTmsX7SBEudTUx++2zDXBhZw==</M>
<E>Aw==</E>
</RSAPK>
<IDK>100</IDK>
</DA>
<FRMA algoritmo="SHA1withRSA">g1AQX0sy8NJugX52k2hTJEZAE9Cuul6pqYBdFxj1N17umW7zG/hAavCALKByHzdYAfZ3LhGTXCai5zNxOo4lDQ==</FRMA>
</CAF>
<TSTED>2003-10-13T09:33:20</TSTED>
</DD>
<FRMT algoritmo="SHA1withRSA">GbmDcS9e/jVC2LsLIe1iRV12Bf6lxsILtbQiCkh6mbjckFCJ7fj/kakFTS06Jo8i
S4HXvJj3oYZuey53Krniew==</FRMT>
</TED>
<TmstFirma>2003-10-13T09:33:20</TmstFirma>
</Documento>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#F60T33">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>hlmQtu/AyjUjTDhM3852wvRCr8w=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>JG1Ig0pvSIH85kIKGRZUjkyX6CNaY08Y94j4UegTgDe8+wl61GzqjdR1rfOK9BGn93AMOo6aiAgolW0k/XklNVtM/ZzpNNS3d/fYVa1q509mAMSXbelxSM3bjoa7H6Wzd/mV1PpQ8zK5gw7mgMMP4IKxHyS92G81GEguSmzcQmA=</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>
tNEknkb1kHiD1OOAWlLKkcH/UP5UGa6V6MYso++JB+vYMg2OXFROAF7G8BNFFPQx
iuS/7y1azZljN2xq+bW3bAou1bW2ij7fxSXWTJYFZMAyndbLyGHM1e3nVmwpgEpx
BHhZzPvwLb55st1wceuKjs2Ontb13J33sUb7bbJMWh0=
</Modulus>
<Exponent>
AQAB
</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>MIIEgjCCA+ugAwIBAgIEAQAApzANBgkqhkiG9w0BAQUFADCBtTELMAkGA1UEBhMC
Q0wxHTAbBgNVBAgUFFJlZ2lvbiBNZXRyb3BvbGl0YW5hMREwDwYDVQQHFAhTYW50
aWFnbzEUMBIGA1UEChQLRS1DRVJUQ0hJTEUxIDAeBgNVBAsUF0F1dG9yaWRhZCBD
ZXJ0aWZpY2Fkb3JhMRcwFQYDVQQDFA5FLUNFUlRDSElMRSBDQTEjMCEGCSqGSIb3
DQEJARYUZW1haWxAZS1jZXJ0Y2hpbGUuY2wwHhcNMDMxMDAxMTg1ODE1WhcNMDQw
OTMwMDAwMDAwWjCBuDELMAkGA1UEBhMCQ0wxFjAUBgNVBAgUDU1ldHJvcG9saXRh
bmExETAPBgNVBAcUCFNhbnRpYWdvMScwJQYDVQQKFB5TZXJ2aWNpbyBkZSBJbXB1
ZXN0b3MgSW50ZXJub3MxDzANBgNVBAsUBlBpc28gNDEjMCEGA1UEAxQaV2lsaWJh
bGRvIEdvbnphbGV6IENhYnJlcmExHzAdBgkqhkiG9w0BCQEWEHdnb256YWxlekBz
aWkuY2wwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALxZlVh1xr9sKQIBDF/6
Va+lsHQSG5AAmCWvtNTIOXN3E9EQCy7pOPHrDg6EusvoHyesZSKJbc0TnIFXZp78
q7mbdHijzKqvMmyvwbdP7KK8LQfwf84W4v9O8MJeUHlbJGlo5nFACrPAeTtONbHa
ReyzeMDv2EganNEDJc9c+UNfAgMBAAGjggGYMIIBlDAjBgNVHREEHDAaoBgGCCsG
AQQBwQEBoAwWCjA3ODgwNDQyLTQwCQYDVR0TBAIwADA8BgNVHR8ENTAzMDGgL6At
hitodHRwOi8vY3JsLmUtY2VydGNoaWxlLmNsL2UtY2VydGNoaWxlY2EuY3JsMCMG
A1UdEgQcMBqgGAYIKwYBBAHBAQKgDBYKOTY5MjgxODAtNTAfBgNVHSMEGDAWgBTg
KP3S4GBPs0brGsz1CJEHcjodCDCB0AYDVR0gBIHIMIHFMIHCBggrBgEEAcNSBTCB
tTAvBggrBgEFBQcCARYjaHR0cDovL3d3dy5lLWNlcnRjaGlsZS5jbC8yMDAwL0NQ
Uy8wgYEGCCsGAQUFBwICMHUac0VsIHRpdHVsYXIgaGEgc2lkbyB2YWxpZG8gZW4g
Zm9ybWEgcHJlc2VuY2lhbCwgcXVlZGFuZG8gZWwgQ2VydGlmaWNhZG8gcGFyYSB1
c28gdHJpYnV0YXJpbywgcGFnb3MsIGNvbWVyY2lvIHkgb3Ryb3MwCwYDVR0PBAQD
AgTwMA0GCSqGSIb3DQEBBQUAA4GBABMfCyJF0mNXcov8iEWvjGFyyPTsXwvsYbbk
OJ41wjaGOFMCInb4WY0ngM8BsDV22bGMs8oLyX7rVy16bGA8Z7WDUtYhoOM7mqXw
/Hrpqjh3JgAf8zqdzBdH/q6mAbdvq/yb04JHKWPC7fMFuBoeyVWAnhmuMZfReWQi
MUEHGGIW</X509Certificate>
</X509Data>
</KeyInfo>
</Signature></DTE>
</SetDTE><Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#SetDoc">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>4OTWXyRl5fw3htjTyZXQtYEsC3E=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>sBnr8Yq14vVAcrN/pKLD/BrqUFczKMW3y1t3JOrdsxhhq6IxvS13SgyMXbIN/T9ciRaFgNabs3pi732XhcpeiSmD1ktzbRctEbSIszYkFJY49k0eB+TVzq3eVaQr4INrymfuOnWj78BZcwKuXvDy4iAcx6/TBbAAkPFwMP9ql2s=</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>
tNEknkb1kHiD1OOAWlLKkcH/UP5UGa6V6MYso++JB+vYMg2OXFROAF7G8BNFFPQx
iuS/7y1azZljN2xq+bW3bAou1bW2ij7fxSXWTJYFZMAyndbLyGHM1e3nVmwpgEpx
BHhZzPvwLb55st1wceuKjs2Ontb13J33sUb7bbJMWh0=
</Modulus>
<Exponent>
AQAB
</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>MIIEgjCCA+ugAwIBAgIEAQAApzANBgkqhkiG9w0BAQUFADCBtTELMAkGA1UEBhMC
Q0wxHTAbBgNVBAgUFFJlZ2lvbiBNZXRyb3BvbGl0YW5hMREwDwYDVQQHFAhTYW50
aWFnbzEUMBIGA1UEChQLRS1DRVJUQ0hJTEUxIDAeBgNVBAsUF0F1dG9yaWRhZCBD
ZXJ0aWZpY2Fkb3JhMRcwFQYDVQQDFA5FLUNFUlRDSElMRSBDQTEjMCEGCSqGSIb3
DQEJARYUZW1haWxAZS1jZXJ0Y2hpbGUuY2wwHhcNMDMxMDAxMTg1ODE1WhcNMDQw
OTMwMDAwMDAwWjCBuDELMAkGA1UEBhMCQ0wxFjAUBgNVBAgUDU1ldHJvcG9saXRh
bmExETAPBgNVBAcUCFNhbnRpYWdvMScwJQYDVQQKFB5TZXJ2aWNpbyBkZSBJbXB1
ZXN0b3MgSW50ZXJub3MxDzANBgNVBAsUBlBpc28gNDEjMCEGA1UEAxQaV2lsaWJh
bGRvIEdvbnphbGV6IENhYnJlcmExHzAdBgkqhkiG9w0BCQEWEHdnb256YWxlekBz
aWkuY2wwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALxZlVh1xr9sKQIBDF/6
Va+lsHQSG5AAmCWvtNTIOXN3E9EQCy7pOPHrDg6EusvoHyesZSKJbc0TnIFXZp78
q7mbdHijzKqvMmyvwbdP7KK8LQfwf84W4v9O8MJeUHlbJGlo5nFACrPAeTtONbHa
ReyzeMDv2EganNEDJc9c+UNfAgMBAAGjggGYMIIBlDAjBgNVHREEHDAaoBgGCCsG
AQQBwQEBoAwWCjA3ODgwNDQyLTQwCQYDVR0TBAIwADA8BgNVHR8ENTAzMDGgL6At
hitodHRwOi8vY3JsLmUtY2VydGNoaWxlLmNsL2UtY2VydGNoaWxlY2EuY3JsMCMG
A1UdEgQcMBqgGAYIKwYBBAHBAQKgDBYKOTY5MjgxODAtNTAfBgNVHSMEGDAWgBTg
KP3S4GBPs0brGsz1CJEHcjodCDCB0AYDVR0gBIHIMIHFMIHCBggrBgEEAcNSBTCB
tTAvBggrBgEFBQcCARYjaHR0cDovL3d3dy5lLWNlcnRjaGlsZS5jbC8yMDAwL0NQ
Uy8wgYEGCCsGAQUFBwICMHUac0VsIHRpdHVsYXIgaGEgc2lkbyB2YWxpZG8gZW4g
Zm9ybWEgcHJlc2VuY2lhbCwgcXVlZGFuZG8gZWwgQ2VydGlmaWNhZG8gcGFyYSB1
c28gdHJpYnV0YXJpbywgcGFnb3MsIGNvbWVyY2lvIHkgb3Ryb3MwCwYDVR0PBAQD
AgTwMA0GCSqGSIb3DQEBBQUAA4GBABMfCyJF0mNXcov8iEWvjGFyyPTsXwvsYbbk
OJ41wjaGOFMCInb4WY0ngM8BsDV22bGMs8oLyX7rVy16bGA8Z7WDUtYhoOM7mqXw
/Hrpqjh3JgAf8zqdzBdH/q6mAbdvq/yb04JHKWPC7fMFuBoeyVWAnhmuMZfReWQi
MUEHGGIW</X509Certificate>
</X509Data>
</KeyInfo>
</Signature></EnvioDTE>
This Works. You add "*:" for the nodes.
DECLARE #messagebody XML
SELECT #messagebody = BulkColumn
FROM OPENROWSET(BULK 'C:\Ejemplo.xml', SINGLE_CLOB) AS X
PRINT CONVERT(VARCHAR(MAX),#messagebody)
INSERT INTO [dbo].[Ejemplo24]
select a.value(N'(./*:RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
a.value(N'(./*:RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
a.value(N'(./*:RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
a.value(N'(./*:FchResol)[1]', N'nvarchar(12)') as [FchResol],
a.value(N'(./*:NroResol)[1]', N'nvarchar(12)') as [NroResol]
from #messagebody.nodes('/*:EnvioDTE/*:SetDTE/*:Caratula') as r(a);
Your query is great, but you must specify the default namespace xmlns
Try like this:
WITH XMLNAMESPACES(DEFAULT 'http://www.sii.cl/SiiDte')
INSERT INTO [dbo].[Ejemplo2]
select a.value(N'(./RutEmisor)[1]', N'nvarchar(12)') as [RutEmisor],
a.value(N'(./RutEnvia)[1]', N'nvarchar(12)') as [RutEnvia],
a.value(N'(./RutReceptor)[1]', N'nvarchar(12)') as [RutReceptor],
a.value(N'(./FchResol)[1]', N'nvarchar(12)') as [FchResol],
a.value(N'(./NroResol)[1]', N'nvarchar(12)') as [NroResol]
from #messagebody.nodes('/EnvioDTE/SetDTE/Caratula') as r(a);

Resources