How do I update my SQL database using XML? - sql

I have the following XML file:
<?xml version="1.0" encoding="utf-8"?>
<Patients>
<Patient EMail="LeBron#James.com">
<FirstName>LeBron</FirstName>
<LastName>James</LastName>
</Patient>
<Patient EMail="Kobe#Bryant.com">
<FirstName>Kobe</FirstName>
<LastName>Bryant</LastName>
</Patient>
<Patient EMail="Allen#Iverson.com">
<FirstName>Allen</FirstName>
<LastName>Iverson</LastName>
</Patient>
</Patients>
I want to store it in a SQL database which I have done successfully/
I then added some more data to the same XML File:
<?xml version="1.0" encoding="utf-8"?>
<Patients>
<Patient EMail="LeBron#James.com">
<FirstName>LeBron</FirstName>
<LastName>James</LastName>
</Patient>
<Patient EMail="Kobe#Bryant.com">
<FirstName>Kobe</FirstName>
<LastName>Bryant</LastName>
</Patient>
<Patient EMail="Allen#Iverson.com">
<FirstName>Allen</FirstName>
<LastName>Iverson</LastName>
</Patient>
<!-- New data starts here -->
<Patient EMail="trtr#Iverson.com">
<FirstName>tr</FirstName>
<LastName>rson</LastName>
</Patient>
<Patient EMail="wewn#Iverson.com">
<FirstName>Awerwren</FirstName>
<LastName>Iveww</LastName>
</Patient>
</Patients>
But it does not update in SQL database at runtime.....
What am I doing wrong?

Based on what you've posted, I'm guessing that perhaps there's a primary or unique key on the Patient table's FirstName and LastName columns, and the second time you try to insert data into the database this constraint is causing the inserts to fail.
Just a guess. Post more information and perhaps we can narrow it down a bit for you.
Share and enjoy.

Delete the previous database. Create a new database with the new XML.

Related

SQL query for Dynamic nodes in XML [duplicate]

This question already has an answer here:
SQL Data as XML Element
(1 answer)
Closed 5 years ago.
Our table is like
StudentNo Name Subject Mark Grade
1 John English 41 A
1 John Hindi 42 B
We want an XML format from this table as follows.
<Student>
<Name>John</Name>
<Subject>
<English>
<Mark>41</Mark>
<Grade>A</Grade>
</English>
<Hindi>
<Mark>42</Mark>
<Grade>B</Grade>
</Hindi>
</Subject>
<Student>
Here the subject name nodes should be generated dynamically.
This is very similar to SQL Data as XML Element - so much so that I think it might be a duplicate - but I want to explain a bit more for your context why this isn't the best idea. In my answer to that question, I show a really hacky way that you could do this, but it's not the best idea.
Your XML will be nearly impossible to create a schema for. Any consumer of that XML will never be able to be sure what values might appear as elements. Rather than try to create dynamic elements, you should probably use attributes of some sort. You could even use xsi:type to create an abstract type in your XML of sorts (although in my example I'm just using a plain old attribute - you could pick whatever attribute will make the most sense for your consumers). The Query for that XML would be:
declare #subjects TABLE(studentno int, name varchar(10), subjecT varchar(10), mark int, grade char(1))
INSERT #subjects
VALUES
(1, 'John','English', 41,'A'),
(1, 'John','Hindi', 42,'B')
select
s.Name
,(SELECT
s2.Subject as '#type'
,s2.Mark
,s2.Grade
FROM #subjects s2
WHERE s2.studentno = s.studentno
FOR XML PATH('Subject'), ROOT('Subjects'), TYPE)
from #subjects s
GROUP BY s.name, s.studentno
FOR XML PATH('Student')
produces:
<Student>
<Name>John</Name>
<Subjects>
<Subject type="English">
<Mark>41</Mark>
<Grade>A</Grade>
</Subject>
<Subject type="Hindi">
<Mark>42</Mark>
<Grade>B</Grade>
</Subject>
</Subjects>
</Student>
This XML will be possible to make sense of by consumers, where they can, for example, iterate the subjects without knowing what subjects might be there (and without needing to resort to assuming that every direct child of Subjects is in fact a subject and not some other type of node that got added in a new version of the schema).
If you really need that output, I'd prefer to use XSLT to transform the output above to your format, e.g.:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="Subject">
<xsl:element name="{#type}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="Subjects">
<xsl:element name="Subject">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:transform>
gets you
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<Name>John</Name>
<Subject>
<English>
<Mark>41</Mark>
<Grade>A</Grade>
</English>
<Hindi>
<Mark>42</Mark>
<Grade>B</Grade>
</Hindi>
</Subject>
</Student>
Note you can't do this completely with SQL Server though - you'd have to resort to building the XML string and casting it as XML, as in my other answer.

Is it possible to create multiple data source objects under the same database executing a single xmla script?

I want to create multiple data source objects under the same database executing the single XMLA script only once.I have tried the below script but it did not work.If I define only a single node, the script executes successfully.But when I add the another same node it gives error. I am newer to this.Please guide.
<Create xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<ParentObject>
<DatabaseID>Test Database</DatabaseID>
</ParentObject>
<ObjectDefinition>
<DataSource xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RelationalDataSource">
<ID>Test Datasource1</ID>
<Name>Test Datasource1</Name>
<Description>A test datasource1.</Description>
<ConnectionString>Provider=SQLNCLI11.1;Data Source=servername;User ID=user;Password=pass;Initial Catalog=SqlDb</ConnectionString>
<ImpersonationInfo>
<ImpersonationMode>ImpersonateServiceAccount</ImpersonationMode>
</ImpersonationInfo>
<Timeout>PT0S</Timeout>
</DataSource>
<DataSource xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="RelationalDataSource">
<ID>Test Datasource2</ID>
<Name>Test Datasource2</Name>
<Description>A test datasource2.</Description>
<ConnectionString>Provider=SQLNCLI11.1;Data Source=servername;User ID=user;Password=pass;Initial Catalog=SqlDb</ConnectionString>
<ImpersonationInfo>
<ImpersonationMode>ImpersonateServiceAccount</ImpersonationMode>
</ImpersonationInfo>
<Timeout>PT0S</Timeout>
</DataSource>
</ObjectDefinition>
</Create>
Is there a batch element wrapper you can use?

reading xml file with namespace

we need to read a xml file in sql server but we are having problems because the xml have a namespace, I have tried several solutions but I can't resolve the problem.
the xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<Status:orders xmlns:Status="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com Status.xsd">
<order>
<Header>
<Name>500039</Name>
<Letter>A</Letter>
</Header>
</order>
</Status:orders>
can you help how to retrieve the values for the Name and letter tags
thanks in advance.
Your friend is called WITH XMLNAMESPACES...
Try it like this
DECLARE #xml XML=
'<?xml version="1.0" encoding="UTF-8"?>
<Status:orders xmlns:Status="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com Status.xsd">
<order>
<Header>
<Name>500039</Name>
<Letter>A</Letter>
</Header>
</order>
</Status:orders>';
WITH XMLNAMESPACES('http://www.test.com' AS Status)
SELECT #xml.value('(/Status:orders/order/Header/Name)[1]','int')
,#xml.value('(/Status:orders/order/Header/Letter)[1]','varchar(max)');
An alternative was, to use the asterisk.
SELECT #xml.value('(/*:orders/order/Header/Name)[1]','int')
,#xml.value('(/*:orders/order/Header/Letter)[1]','varchar(max)');
Another alternative was this:
SELECT #xml.value('(//Name)[1]','int')
,#xml.value('(//Letter)[1]','varchar(max)');
But in general it is good advice, to be as specific as possible...

infinspan for SQL server

I need a template infinispan for sql server.
Or a tutorial that explains each and every tag, a sample that points h2 database, or tutorial that explains each and every tag a sample that points h2 database.
<?xml version="1.0" encoding="UTF-8"?>
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:6.0 http://www.infinispan.org/schemas/infinispan-config-6.0.xsd
urn:infinispan:config:jdbc:6.0 http://www.infinispan.org/schemas/infinispan-cachestore-jdbc-config-6.0.xsd"
xmlns="urn:infinispan:config:6.0">
<namedCache name="persisted_repository">
<persistence passivation="false">
<stringKeyedJdbcStore xmlns="urn:infinispan:config:jdbc:6.0"
fetchPersistentState="false"
ignoreModifications="false"
purgeOnStartup="false">
<connectionPool
connectionUrl="jdbc:h2:file:target/content/db;DB_CLOSE_DELAY=-1"
driverClass="org.h2.Driver"
username="sa"/>
<stringKeyedTable
prefix="ISPN_STRING_TABLE"
createOnStart="true"
dropOnExit="false">
<idColumn name="ID_COLUMN" type="VARCHAR(255)"/>
<dataColumn name="DATA_COLUMN" type="BINARY"/>
<timestampColumn name="TIMESTAMP_COLUMN" type="BIGINT"/>
</stringKeyedTable>
</stringKeyedJdbcStore>
</persistence>
<transaction
transactionManagerLookupClass="org.infinispan.transaction.lookup.DummyTransa`c`tionManagerLookup"
transactionMode="TRANSACTIONAL"
lockingMode="OPTIMISTIC" />
</namedCache>
</infinispan>
If you want to connect to MySQL, the only thing you have to change is the connectionPool:
<connectionPool
connectionUrl="jdbc:mysql://mysql.example.com:3306/my_db"
driverClass="com.mysql.jdbc.Driver"
username="db_user"
password="db_pwd" />

Unitils dataset and modified dates

Any ideas how this can be done with Unitils dbunit?
Date relative to current in the DBUnit dataset
The problem is that the [create_date]-placeholder is not recognized in #Dataset.
A simple solution might be to just use placeholders in your xml dataset, eg.
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<user userName="jdoe" name="doe" firstname="john" lastLogin="{YESTERDAY}" />
<user userName="jdoe" name="doe" firstname="jane" lastLogin="{A_WEEK_AGO}" />
</dataset>
and do some post-processing(replace the placeholders with the calculated values) before you run your tests. When you are using Maven, you could then first execute the post-processing, (fill in the values in the xml-template-dataset, copy the filled-in-xml-dataset to the correct folder), before any tests are executed.