I have an XML column UserDetails in a SQL Server database which has value like this:
<fleet>
<Setting name="city"/>
<UserSettings name="details">
<UserSettings name="A1">
<setting type="1" name="email"/>
<setting value="user"/>
</UserSettings>
<UserSettings name="A2">
<setting type="2" name="phone"/>
<setting value="technician"/>
</UserSettings>
</UserSettings>
</fleet>
I would to read from the db and add a new setting in
<UserSettings name="A2"> .
Can someone point me to what I should be doing?
You might use sql:variable() to introduce a value you've created externally. And you can use Xml.exist() to find XMLs which haven't got this new entry before:
DECLARE #myTbl TABLE(Descr VARCHAR(100),TheXml XML);
INSERT INTO #myTbl VALUES
(
'Setting "A3" doesn''t exist','<fleet>
<Setting name="city" />
<UserSettings name="details">
<UserSettings name="A1">
<setting type="1" name="email" />
<setting value="user" />
</UserSettings>
<UserSettings name="A2">
<setting type="2" name="phone" />
<setting value="technician" />
</UserSettings>
</UserSettings>
</fleet>'
)
,(
'Setting "A3" exists already','<fleet>
<Setting name="city" />
<UserSettings name="details">
<UserSettings name="A1">
<setting type="1" name="email" />
<setting value="user" />
</UserSettings>
<UserSettings name="A2">
<setting type="2" name="phone" />
<setting value="technician" />
</UserSettings>
<UserSettings name="A3">
<setting type="3" name="A3 setting" />
<setting value="this exists already" />
</UserSettings>
</UserSettings>
</fleet>'
);
DECLARE #NewSetting XML=
N'<UserSettings name="A3">
<setting type="3" name="new A3" />
<setting value="new data" />
</UserSettings>';
UPDATE #myTbl
SET TheXml.modify('
insert sql:variable("#NewSetting")
as last into (/fleet/UserSettings)[1]')
WHERE TheXml.exist(N'/fleet/UserSettings[#name="details"]/UserSettings[#name="A3"]')=0;
SELECT * FROM #myTbl
SQL SERVER provides methods to modify XML data, and a language to do it (XML DML).
You need to use the modify method with an insert xml dml statement.
Here is the complete example:
Sample data:
DECLARE #myDoc xml;
SET #myDoc =
'<fleet>
<Setting name="city" />
<UserSettings name="details">
<UserSettings name="A1">
<setting type="1" name="email" />
<setting value="user" />
</UserSettings>
<UserSettings name="A2">
<setting type="2" name="phone" />
<setting value="technician" />
</UserSettings>
</UserSettings>
</fleet>';
The statement:
SET #myDoc.modify('
insert <setting value="NEW SETTINGS" />
as first into (/fleet/UserSettings/UserSettings[#name="A2"])[1] ') ;
SELECT #myDoc;
Result:
<fleet>
<Setting name="city" />
<UserSettings name="details">
<UserSettings name="A1">
<setting type="1" name="email" />
<setting value="user" />
</UserSettings>
<UserSettings name="A2">
<setting value="NEW SETTINGS" />
<setting type="2" name="phone" />
<setting value="technician" />
</UserSettings>
</UserSettings>
</fleet>
Related
I am making an api and I want to read from the web.config an integer value. I tried to do it like this:
<applicationSettings>
<HHH.Properties.Settings>
<setting name="loggerPath" serializeAs="String">
<value>Logs</value>
</setting>
<setting name="querytime" serializeAs="String">
<value>30</value>
</setting>
<setting name="windowstime" serializeAs="String">
<value>86400</value>
</setting>
<setting name="alarmarmingtime" serializeAs="String">
<value>300</value>
</setting>
</HHH.Properties.Settings>
</applicationSettings>
The value I want to read is the windowstime but it throws me an error.
In the serializeAs property I cannot choose "integer" for example because it is an enumerated one and it does not have this option. How should I do it?
I already resolved it. I used this:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="windowstime" value="86400"/>
</appSettings>
Hi i just want to know how to post xml data to Http post method in Objective-C by dynamically instead of static string Appending
here my sample xml Data :
<xml-packet version="2.5" ctr="1" timestamp="1345193301000">
<device id="0" name="hh" ipaddr="0.0.0.1" type="Hello" status="NORMAL"
capability="ios">
<devcice name="5s" type="2" status="NORMAL">
<sensor name="sample" type="" status="NORMAL">
<event timestamp="1345193300000" type="ITEM">
<position agid="3" name="Manager" lastsynctime="1344433367000" action="" />
<position agid="6" name="LOC3" lastsynctime="1343818867000" action="" />
<position agid="8" name="LOC5" lastsynctime="1344433367000" action="" />
<position agid="4" name="LOC1" lastsynctime="1344504045000" action="" />
<position agid="5" name="LOC2" lastsynctime="1343386683000" action="" />
<position agid="7" name="LOC4" lastsynctime="1344238182000" action="" />
</event>
<event timestamp="1345192938000" type="ITEM">
<position agid="1" name="CEO" lastsynctime="1344594202000" action="update">
<item id="ac001520101217000a012222" result="" status="NEW" lastmodifiedtime="" />
<item id="e20019838306016127500770" result="" status="NEW" lastmodifiedtime="" />
<item id="e20019838309014424301e64" result="" status="NEW" lastmodifiedtime="" />
<item id="2005790000000000000005fc" result="" status="MISSING" lastmodifiedtime="1345212728000" />
<item id="00000000012102007400bd39" result="" status="MISSING" lastmodifiedtime="1344594171000" />
</position>
</event>
</sensor>
</device>
</xml-packet>
Can you all help me for this problem?
I want to get value from following XML in sql stored procedure. I don't get vlaue if 'xsi:type="ActiveDirectoryItem"' is in tag 'anyType', and 'ActiveDirectoryItems' tag is also with URLs. How can i do to get only values?
<?xml version="1.0" encoding="utf-8" ?>
<ActiveDirectoryItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Items>
<anyType xsi:type="ActiveDirectoryItem">
<FirstName />
<MiddleInitial />
<LastName />
<DisplayName>Migrate-group</DisplayName>
<UserPrincipalName />
<PostalAddress />
<ResidentialAddress />
<Title />
<HomePhone />
<OfficePhone />
<Mobile />
<Fax />
<Email>Migrate-group#gmail.com</Email>
<Url />
<AccountName>Migrate-group</AccountName>
<DistinguishedName />
<IsAccountActive>false</IsAccountActive>
<ManagedBy />
<Manager />
<CompareType>0</CompareType>
<Description />
<Department />
<Company />
<Type />
</anyType>
</Items>
<GlobalCatalog />
</ActiveDirectoryItems>
The format i want to get is as the following:
DisplayName Email Account Name
Migrate-group Migrate-group#gmail.com Migrate-group
you can use the value keyword
Example:
DECLARE #MyXml XML = '<Root><SomeData>100</SomeData></Root>'
DECLARE #Something INT
SELECT #Something = #MyXml.value('(//Root/SomeData)[1]', 'INT')
I've inherited a report spec subsystem that needs to be tweaked. The mission is to add a date column to the tReports table, and populate it with the CreateDate that is (supposed to be) contained in the XML Code Spec. The problem is that some of the older reports don't have the CREATEDATE attribute, or as in one example below, the XML is valid but poorly formed, and the CREATEDATE cannot be retrieved using the xQuery that works for most of the other reports. Since I don't have an explicit creation date included in the spec, I'm using interpolation to estimate a reasonable date. One factor of the interpolation is to look at the date strings contained in the report spec--some will be useful, others not.
There are too many reports (over 1,200) to visually skim each report spec for date strings. These date strings can appear in any location in the report spec, and there are a very large number of combinations of elements and attributes that can contain a date string.
The ideal solution would be a listing of the reportID's and the date string ready to use in an UPDATE, but because date format varies (m/d/yy, mm/dd/yy, m/dd/yy ...) I'd be grateful to get a vew spurious characters surrounding the date string that I could clean up later.
All the date strings will be from 2000 or later, so the search string I'v been using is '/20' which has provided good results.
I've looked at many sites that discuss this kind of issue, and found only one solution by Mikael Eriksson that is something like what I'm describing, but I can't make it work after several hours of playing with it. How to extract multiple strings from single rows in SQL Server
Is there a way, without using a cursor nor WHILE loop, to extract these embedded dates?
-- Some representative data: (I'm using SQL Server 2008 R2)
CREATE TABLE #ReportSpecs (ReportID INT, ReportSpec VARCHAR(MAX))
INSERT INTO #ReportSpecs
( ReportID, ReportSpec )
VALUES
(136,
'<ReportID>136</ReportID>
<EmpIDCreator>23816</EmpIDCreator>
<EmpName>Blanc, Melvin J</EmpName>
<ReportType>0</ReportType>
<ReportName>PSST Sys Spec</ReportName>
<ReportData>
<REPORT>
<COLUMNS>
<Column Name="JobNumber" Position="1" />
<Column Name="TaskType" Position="2" />
<Column Name="Assignees" Position="3" />
<Column NAME="JobDueDate" Position="4" />
<Column Name="ReferenceNumber" Position="5" />
<Column Name="Deliverable" Position="6" />
<Column Name="Priority" Position="7" />
</COLUMNS>
<FILTERS>
<FILTER NAME="TYPE" VALUE="To_Me" />
<FILTER NAME="Status" VALUE="All" />
<FILTER NAME="DateOptions" VALUE="DateRange" From="8/16/2002" To="8/23/2002" />
<FILTER NAME="FromDate" VALUE="8/16/2002" />
<FILTER NAME="ToDate" VALUE="8/23/2002" />
<FILTER NAME="Role" VALUE="All" />
</FILTERS>
<parameters>
<PARAMETER NAME="#Cascading" TYPE="integer" VALUE="0" />
<PARAMETER NAME="#EmpID" SYSTEM="true" TYPE="integer" VALUE="#Request.EmployeeIDAlias#" />
<PARAMETER NAME="#FromOrgs" TYPE="varchar(250)" VALUE="" />
<PARAMETER NAME="#ToOrgs" TYPE="varchar(250)" VALUE="" />
</parameters>
<NAME>PSST Sys Spec</NAME>
<OWNER>
<ID>23816</ID>
</OWNER>
<source id="8" useinternalid="True" />
</REPORT>
</ReportData>'),
(311,
'<ReportID>311</ReportID>
<EmpIDCreator>7162</EmpIDCreator>
<EmpName>Potter, Harry J</EmpName>
<ReportType>0</ReportType>
<ReportName>CPVC Synch Test</ReportName>
<ReportData>
<REPORT>
<COLUMNS>
<Column Name="JobNumber" Position="1" />
<Column Name="TaskType" Position="2" />
<Column Name="Subject" Position="3" />
<Column Name="CurrentAssignee" Position="4" />
<Column NAME="JobDueDate" Position="5" />
<Column Name="Deliverable" Position="6" />
<Column Name="Category" Position="7" />
<Column Name="Priority" Position="8" />
</COLUMNS>
<FILTERS>
<FILTER NAME="TYPE" VALUE="By_Orgs_6098,By_Orgs_6123" />
<FILTER NAME="Status" VALUE="Open" />
<FILTER NAME="DateOptions" VALUE="DateRange" From="3/25/2002" To="4/4/2002" />
<FILTER NAME="ReviewFromDate" VALUE="3/25/2002" />
<FILTER NAME="ReviewToDate" VALUE="4/4/2002" />
<FILTER NAME="Role" VALUE="All" />
</FILTERS>
<parameters>
<PARAMETER NAME="#Act" TYPE="integer" VALUE="0" />
<PARAMETER NAME="#MgrID" SYSTEM="true" TYPE="integer" VALUE="#Request.EmployeeIDAlias#" />
<PARAMETER NAME="#MgrIDActing" TYPE="integer" VALUE="" />
<PARAMETER NAME="#FromDept" TYPE="varchar(250)" VALUE="" />
<PARAMETER NAME="#FromEmp" TYPE="varchar(250)" VALUE="" />
<PARAMETER NAME="#ToDept" TYPE="varchar(250)" VALUE="" />
</parameters>
<NAME>CPVC Synch Test</NAME>
<OWNER>
<ID>7162</ID>
</OWNER>
<source id="17" useinternalid="True" />
</REPORT>
</ReportData>'),
(1131,
'<ReportID>1131</ReportID>
<EmpIDCreator>13185</EmpIDCreator>
<EmpName>Reed, Alan</EmpName>
<ReportType>0</ReportType>
<ReportName>
''"><script>alert(''hello'')</script>
</ReportName>
<ReportData>
<Report NAME="''">
<script>alert(''hello'')</script>" CREATEDATE="12/7/2009">
<DESCRIPTION>sfasf</DESCRIPTION>
<OWNER ID="13185"/>
<SOURCE ID="1" USEINTERNALID="TRUE"/>
<COLUMNS>
<COLUMN NAME="JobNumber" POSITION="1" SORTORDER="asc"/>
</COLUMNS>
<FILTERS>
<FILTER NAME="TYPE" VALUE="By_Me,To_Me" />
<FILTER NAME="ASGSTATUS" VALUE="Open" />
<FILTER NAME="DATEOPTIONS" VALUE="All" />
<FILTER NAME="STATUS" VALUE="Open" />
<FILTER NAME="ASGDATEOPTIONS" VALUE="All" />
<FILTER NAME="ROLE" VALUE="All" />
</FILTERS>
<PARAMETERS>
<PARAMETER NAME="#Me" TYPE="integer" VALUE="3" />
<PARAMETER NAME="#FromCost" TYPE="varchar(250)" VALUE=""/>
<PARAMETER NAME="#ToCost" TYPE="varchar(250)" VALUE="" />
</PARAMETERS>
<ADVANCEDSORT SortByA="JobNumber" SortOrderA="asc" SortByB="" SortOrderB="" SortByC="" SortOrderC="" />
</Report>
</ReportData>');
/*
Desired Output (A DISTINCT list would be better, but just getting this output would be GREAT.)
ReportID DateString
-------- ----------
136 8/16/2002
136 8/23/2002
136 8/16/2002
136 8/23/2002
311 3/25/2002
311 4/4/2002
311 3/25/2002
311 4/4/2002
1131 12/7/2009
*/
DROP TABLE #ReportSpecs
Thanks for your time.
select R.ReportID,
D.V as DateString
from #ReportSpecs as R
cross apply (select cast(R.ReportSpec as xml)) as X(R)
cross apply X.R.nodes('//#*, //*/text()') as T(X)
cross apply (select T.X.value('.', 'varchar(max)')) as D(V)
where charindex('/20', D.V) > 0
Result:
ReportID DateString
----------- --------------------------
136 8/16/2002
136 8/23/2002
136 8/16/2002
136 8/23/2002
311 3/25/2002
311 4/4/2002
311 3/25/2002
311 4/4/2002
1131 " CREATEDATE="12/7/2009">
I'am trying to execute sp_xml_preparedocument and geting error "Only one top level element is allowed in an XML document"
my T-SQL commands:
DECLARE #aa XML
DECLARE #idoc int
SET #aa =(select * from db_name for xml auto, xmldata)
#aa now is
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" name="Schema7">
<ElementType name="Person" content="empty" model="closed">
<AttributeType name="preson_id" dt:type="i4" />
<AttributeType name="Name" dt:type="string" />
<AttributeType name="Surname" dt:type="string" />
<AttributeType name="guid" dt:type="uuid" />
<AttributeType name="version" dt:type="bin.base64" />
<attribute type="preson_id" />
<attribute type="Name" />
<attribute type="Surname" />
<attribute type="guid" />
<attribute type="version" />
</ElementType>
</Schema>
<Person xmlns="x-schema:#Schema7" preson_id="1" Name="Иван" Surname="Иванов" guid="2E739E87-3CA4-4ED8-ADD0-8B59957668B8" version="AAAAAAAAB9E=" />
<Person xmlns="x-schema:#Schema7" preson_id="2" Name="Николай" Surname="Николаев" guid="BDC41C59-D70F-4B70-954E-4918B9516AF8" version="AAAAAAAAB9I=" />
<Person xmlns="x-schema:#Schema7" preson_id="3" Name="Максим" Surname="Максимов" guid="740E57F3-56BA-48B8-92AF-978D7B1D2712" version="AAAAAAAAB9M=" />
EXEC sp_xml_preparedocument #idoc OUTPUT, #aa
The XML parse error 0xc00ce555 occurred on line number 1, near the XML text ""
Msg 6602, Level 16, State 2, Procedure sp_xml_preparedocument, Line 1
The error description is 'Only one top level element is allowed in an XML document
I'am new in this, and i need help)))
An one more question - How parse timestamp type?
if i use
SET #aa =(select * from db_name for xml elements, root('root'), type)
sp_xml_preparedocument works fine, OPENXML returns all values of my db_table, but timestamp values looks not the same as were..
Sorry for my bad English
SELECT #aa returns
<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes" name="Schema7">
<ElementType name="Person" content="empty" model="closed">
<AttributeType name="preson_id" dt:type="i4" />
<AttributeType name="Name" dt:type="string" />
<AttributeType name="Surname" dt:type="string" />
<AttributeType name="guid" dt:type="uuid" />
<AttributeType name="version" dt:type="bin.base64" />
<attribute type="preson_id" />
<attribute type="Name" />
<attribute type="Surname" />
<attribute type="guid" />
<attribute type="version" />
</ElementType>
</Schema>
<Person xmlns="x-schema:#Schema7" preson_id="1" Name="Иван" Surname="Иванов" guid="2E739E87-3CA4-4ED8-ADD0-8B59957668B8" version="AAAAAAAAB9E=" />
<Person xmlns="x-schema:#Schema7" preson_id="2" Name="Николай" Surname="Николаев" guid="BDC41C59-D70F-4B70-954E-4918B9516AF8" version="AAAAAAAAB9I=" />
<Person xmlns="x-schema:#Schema7" preson_id="3" Name="Максим" Surname="Максимов" guid="740E57F3-56BA-48B8-92AF-978D7B1D2712" version="AAAAAAAAB9M=" />
An XML document must have one root element only - see W3C specification.
Thus in you case if Schema is the root element you cannot add the Person elements at the end
Make sure that there is no duplicate entries for the record that you are trying to get