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">
Related
I am trying to convert data from a xml field into a table. I have found multiple solutions on this website, but none of them seem to give me exactly what I need.
The xml looks like this:
<table>
<id>{100}</id>
<rows>
<row>
<columns>
<column name="cur" value="EUR" type="System.String" />
<column name="item" value="Item a" type="System.String" />
<column name="price" value="2.5" type="System.Decimal" />
<column name="qty" value="20" type="System.Int32" />
<column name="Key" value="1" type="System.Int32" />
</columns>
</row>
<row>
<columns>
<column name="cur" value="EUR" type="System.String" />
<column name="item" value="Item b" type="System.String" />
<column name="price" value="30" type="System.Decimal" />
<column name="qty" value="30" type="System.Int32" />
<column name="Key" value="2" type="System.Int32" />
</columns>
</row>
</rows>
<key>Key</key>
</table>
I would like the result to look like this:
id key cur item price qty
100 1 EUR Item a 2.5 20
100 2 EUR Item b 30 30
I hope someone can help me.
Try it like this:
DECLARE #x XML=
'<table>
<id>{100}</id>
<rows>
<row>
<columns>
<column name="cur" value="EUR" type="System.String" />
<column name="item" value="Item a" type="System.String" />
<column name="price" value="2.5" type="System.Decimal" />
<column name="qty" value="20" type="System.Int32" />
<column name="Key" value="1" type="System.Int32" />
</columns>
</row>
<row>
<columns>
<column name="cur" value="EUR" type="System.String" />
<column name="item" value="Item b" type="System.String" />
<column name="price" value="30" type="System.Decimal" />
<column name="qty" value="30" type="System.Int32" />
<column name="Key" value="2" type="System.Int32" />
</columns>
</row>
</rows>
<key>Key</key>
</table>';
SELECT #x.value('(/table/id)[1]','varchar(max)') AS id --are the curly brackets there on purpose?
,Rw.value('(columns/column[#name="Key"]/#value)[1]','int') AS [key]
,Rw.value('(columns/column[#name="cur"]/#value)[1]','varchar(max)') AS cur
,Rw.value('(columns/column[#name="item"]/#value)[1]','varchar(max)') AS item
,Rw.value('(columns/column[#name="price"]/#value)[1]','decimal(10,4)') AS price
,Rw.value('(columns/column[#name="qty"]/#value)[1]','int') AS qty
FROM #x.nodes('/table/rows/row') AS A(Rw)
I have a pretty simple Propel question. I'm storing leads in a database. Those leads have one or more community interests. The tables I'm using are named, 'lead', 'community', and a 'lead_communities' join table. What's the best way to delete all of a lead's community interests?
Here are some more details. The Propel schema looks like this:
<table name="community">
<column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" />
<column name="name" type="VARCHAR" size="255" />
<column name="address" type="VARCHAR" size="255" />
etc...
<table name="lead_communities">
<column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true"/>
<column name="lead_id" type="INTEGER" />
<column name="community_id" type="INTEGER" />
<column name="created_date" type="DATE" size="4" />
<foreign-key foreignTable="community" refPhpName="Lead_Communities">
<reference local="community_id" foreign="id"/>
</foreign-key>
<foreign-key foreignTable="lead" refPhpName="Lead_Communities">
<reference local="lead_id" foreign="id"/>
</foreign-key>
</table>
<table name="lead">
<column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true"/>
<column name="salutation" type="VARCHAR" size="20" />
<column name="first_name" type="VARCHAR" size="255" defaultValue="" />
<column name="last_name" type="VARCHAR" size="255" defaultValue="" />
<column name="email" type="VARCHAR" size="255" defaultValue="" />
etc..
Here's how I select the lead from the database:
$lead = LeadQuery::create()->filterByEmail($enauk)->findOne();
So, what I hope to do is something like:
$lead->deleteLeadCommunities();
Well, the easiest way I can think of without any other context is simply doing a query on the join table with a delete() call:
$numDeleted = LeadCommunitiesQuery::create()
->filterByLead($lead)
->delete();
Just to be clear, there is no generated method like what you want (deleteLeadCommunities), but you could always write it for convenience, and replace $lead in my example code with $this. So inside Lead.php:
public function deleteLeadCommunities() {
return LeadCommunitiesQuery::create()
->filterByLead($this)
->delete();
}
How to select file names from this kind of XML in a SQL Server column?
file name is in attribute VALUE only if high level tag is object with name File
like this
<Object NAME="File">
<Parameter ID="1" NAME="Name" VALUE="\\mysvr\fiels\readme1.txt" />
</Object>
This sub-hierarchy can be placed on any level of xml (see example below)
XML column can contain 0-N file names, I need list like this:
id, filename
--- ------------------------
1 \\mysvr\fiels\readme1.txt
1 \\mysvr\fiels\readme2.txt
2 \\mysvr\fiels\readme3.txt
2 \\mysvr\fiels\readme4.txt
Example of XML contents:
declare #t1 table
( id int,
x XML
)
insert into #t1 (id, x)
select 1,N'<root name="name" id="12">
<class1>
<Object NAME="File">
<Parameter ID="1" NAME="Name" VALUE="\\mysvr\fiels\readme1.txt" />
</Object>
</class1>
<class1>
<subclass1>
<Object NAME="File">
<Parameter ID="10" NAME="Name" VALUE="\\mysvr\fiels\readme2.txt" />
</Object>
<Object NAME="bitmap">
<Parameter ID="11" NAME="my1" VALUE="bmp" />
</Object>
</subclass1>
</class1>
</root>'
union
select 2,N'<root name="name" id="12">
<class1>
<Object NAME="File">
<Parameter ID="13" NAME="Name" VALUE="\\mysvr\fiels\readme3.txt" />
</Object>
<Object NAME="Font">
<Parameter ID="22" NAME="Tahoma" VALUE="11" />
</Object>
</class1>
<class1>
<subclass1>
<Object NAME="File">
<Parameter ID="14" NAME="Name" VALUE="\\mysvr\fiels\readme4.txt" />
</Object>
</subclass1>
</class1>
</root>'
Try this:
SELECT
id,
Filename = ObjPrm.value('#VALUE', 'varchar(100)')
FROM #t1
CROSS APPLY x.nodes('//Object[#NAME="File"]/Parameter') AS Tbl(ObjPrm)
Gives me an output of:
To do this you can do the following.
Define a string of the specific details you want to display.
You want to display id and Name so your string will look something like this.
String holder = "#id# -- #VALUE#"
Then you search through that whole string for the values you have between the two '#' signs.
When you find them, just do a search for the value inside the Quotation marks, and display them.
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
I've two tables, Profile and ProfileCategory
ProfileId INT IX
UserId UNIQUEIDENTIFIER PK (For one-to-one mapping with aspnet_membership)
CompanyName
Description
ProfileCategory
CategoryId
ProfileId
When I generate the code with PLINGO I get following errors
Operator '==' cannot be applied to operands of type 'int?' and 'System.Guid'
Operator '==' cannot be applied to operands of type 'int?' and 'System.Guid'
I dived into the generated code and the following..
[System.Diagnostics.DebuggerNonUserCode]
[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]
private void OnProfileList1Remove(Profile entity)
{
SendPropertyChanging(null);
var profileCategory = ProfileCategoryList.FirstOrDefault(c => c.CategoryId == CategoryId
&& c.ProfileId == entity.UserId);
ProfileCategoryList.Remove( profileCategory);
SendPropertyChanged(null);
}
So the generated code seems to want to compare the index and the primary key
I can manually alter the code but on regeneration it will be altered back.
Does anyone know why this is happening?
Thanks
Here is the profile snipet from the dbml
<Table Name="dbo.Profile" Member="Profile">
<Type Name="Profile">
<Column Name="UserId" Storage="_userId" Type="System.Guid" DbType="uniqueidentifier NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
<Column Name="ProfileId" Storage="_profileId" Type="System.Int32" DbType="int NOT NULL IDENTITY" CanBeNull="false" />
<Column Name="CompanyName" Storage="_companyName" Type="System.String" DbType="nvarchar(250) NOT NULL" CanBeNull="false" />
<Column Name="Description" Storage="_description" Type="System.String" DbType="varchar(MAX)" CanBeNull="true" />
<Column Name="Services" Storage="_services" Type="System.Xml.Linq.XElement" DbType="xml" CanBeNull="true" UpdateCheck="Never" />
<Column Name="ContactDetails" Storage="_contactDetails" Type="System.Xml.Linq.XElement" DbType="xml" CanBeNull="true" UpdateCheck="Never" />
<Column Name="Attributes" Storage="_attributes" Type="System.Xml.Linq.XElement" DbType="xml" CanBeNull="true" UpdateCheck="Never" />
<Column Name="StateId" Storage="_stateId" Type="System.Int32" DbType="int NOT NULL" CanBeNull="false" />
<Column Name="Views" Storage="_views" Type="System.Int32" DbType="int NOT NULL" CanBeNull="false" />
<Association Name="User_Profile" Member="User" Storage="_user" ThisKey="UserId" Type="User" IsForeignKey="true" DeleteRule="CASCADE" />
<Association Name="Profile_ProfileAddress" Member="ProfileAddressList" Storage="_profileAddressList" OtherKey="UserId" Type="ProfileAddress" DeleteOnNull="false" />
<Association Name="Profile_Review" Member="ReviewList" Storage="_reviewList" ThisKey="ProfileId" OtherKey="ProfileId" Type="Review" DeleteOnNull="false" />
<Association Name="Profile_ProfileCategory" Member="ProfileCategoryList" Storage="_profileCategoryList" ThisKey="ProfileId" OtherKey="ProfileId" Type="ProfileCategory" />
</Type>
</Table>
<Table Name="dbo.ProfileCategory" Member="ProfileCategory">
<Type Name="ProfileCategory">
<Column Name="ProfileCategoryId" Storage="_profileCategoryId" Type="System.Int32" DbType="int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
<Column Name="ProfileId" Storage="_profileId" Type="System.Int32" DbType="int" CanBeNull="true" />
<Column Name="CategoryId" Storage="_categoryId" Type="System.Int32" DbType="int NOT NULL" CanBeNull="false" />
<Association Name="Category_ProfileCategory" Member="Category" Storage="_category" ThisKey="CategoryId" Type="Category" IsForeignKey="true" />
<Association Name="Profile_ProfileCategory" Member="Profile" Storage="_profile" ThisKey="ProfileId" OtherKey="ProfileId" Type="Profile" IsForeignKey="true" DeleteRule="CASCADE" />
</Type>
</Table>
Have you checked to make sure this wasn't already fixed in a nightly build of PLINQO? Could you please post the schema (or send to CodeSmith support) for these two tables.
Thanks
-Blake Niemyjski