I am beginner to the C#, I need to insert my XML file to SQL table pro-grammatically XML file have five nodes called root, name, subject, sno and queries, I will listed below
<root>
<name>
MyName
</name>
<subject>
newsubject
</subject>
<queries>
<sno>
1
</sno>
<query>
This is the query one
</query>
</queries>
<queries>
<sno>
2
</sno>
<query>
This is the query two
</query>
</queries>
<queries>
<sno>
3
</sno>
<query>
This is the query three
</query>
</queries>
</root>
I need to add this value to sql server table name member_info
my table design is
name --> varchar(50)
subject-->varchar(75)
sno-->int
queries-->varchar(150)
I tried some basic stuffs for finding the XML file on the specific folder, I don`t have idea how to implement by reading and inserting XML file, Please be gentle I am just beginner.
You have a few options:
You can read the XML nodes before inserting into the database, and
insert normalized table column values.
You can shred the XML in a stored procedure, by passing the XML
string as an input parameter. You can read up on how to shred the XML utilizing the nodes() method, here.
Related
I have to create an XML file with the below format.
office_tel and mobile are fields of a table.
Any help would be valuable.
<fields>
<office_tel>
<value>1234</value>
</office_tel>
<mobile>
<value>99999</value>
</mobile>
</fields>
Try this for SQL SERVER. Ref FOR XML (SQL Server)
SELECT
office_tel as "office_tel/value",
mobile as "mobile/value"
FROM TableName
FOR XML PATH('fields')
I'm really new to SQL and I'm trying to replace a certain XML record with a brand new updated XML.
So i have a table that contains a list with records that all have a barcode ID, lets say barcode 123 has a column named XML that is filled with an XML A. I want to replace XML A with XML B that is saved on my hard drive.
Can anyone help me with this? It seems i can only find ways to directly replace data inside the xml itself, not replacing the entire XML.
I am using SQL server management studio 2012.
Thanks in advance.
Why not use an update query
update table_name set xml='all content of your xml file here' where BarcodeID=123;
If I have following xml:
<Main>
<Group>
<Insert />
</Group>
<Group>
<Insert />
</Group>
...
</Main>
I know that if I use openxml(#xml, 'Main/Group/Insert',1), I will parse all <Insert>.
Is there a way I can read only one group node, parse its Insert node and then read the next group node and parse its Insert node?
The OpenXML or newer XML datatype and XPath treat the XML as a table, extracting and treating the results as multiple rows in one go.
What you are asking is equivalent to, for a normal SQL Server table, can you select one record, then read the next record? Of course - such as using cursors or WHILE loop -, but why?
Consider what you want to do with it, and you will soon come around to treating it properly as a dataset and getting the parsed results as records, and applying SET logic to the values you retrieve from it.
I've a XML string like that stored on my DB:
<?xml version="1.0" encoding="utf-8"?>
<AddressMaintenance>
<Label Name="lblAddress11">Address Line 1</Label><TextBox Name="txtAddress11">Zuellig Korea</TextBox>
</AddressMaintenance>
do you know if there is a way to extract the value Zuelling Korea using XMLQuery or SQL? I can't create a temporary table because it's a validate environment so I can only read that value. I know is possible using reg exp, but, if possible I try to use XML.
thanks,
Andrea
If this is stored in an XMLTYPE on a table you can use the Oracle XML functions to extract it using XPath:
SELECT
extractvalue(xmlcol, '/*/TextBox[#Name=''txtAddress11'']') txtaddress
FROM yourtable
Adapt the XPath to suit your needs.
See ExtractValue documentation or research other Oracle XML functions.
I should probably note that 11g and later, extractvalue is deprecated and you should use XMLQuery
whats the best way to handle a complex param in a stored proc?
let me explain a little about the param so its clear what i am trying to do here...
I have a project management app - so i have things like a release, a project and milestones.
A project has a single release and a release has its milestones.
i am adding reporting and need to generate a report that allows users to select any number of releases and any milestones of a release. this will result in a report that shows all the projects that are part of the release and their data for the milestones selected.
since each release has multiple milestones (milestone table) to keep the associations of what milestone was selected in what release i was thinking to have coma separated list like this to pass the UI data to SQL.
release1 | m2, m3, m4
release2 | m2, m7
release3 | m5
as a varchar or maybe xml...
whats the best practice for sending in something like this that has relational data built into the param? am i way off and over thinking the problem?
please tell me there is a simple solution i am not seeing...
XML would be the best way and simplest with least amount of code... see a sample below.
As starter sample:
DECLARE #Param XML
SET #Param = '
<chriteria>
<release id="1">
<milestone id="1" />
<milestone id="2" />
<milestone id="3" />
</release>
<release id="2">
<milestone id="1" />
<milestone id="2" />
</release>
</chriteria>
'
SELECT c.value('../#id', 'INT') AS ReleaseId, c.value('#id', 'INT') AS MilestoneId
FROM #Param.nodes('/chriteria/release/milestone') AS T(c)
SQL Server 2008 has Table Valued Parameters to solve just this problem. For SQL Server 2005, you will have to either pass in a string to parse in the Stored Proc, or have your caller and Stored Proc use some shared (or temporary) table to hold the data to process.