Propel - Filter with many-to-many relationship - many-to-many

I try to use Propel and don't succed to filter with many-to-many relationship.
Here, find schema.xml:
<?xml version="1.0" encoding="UTF-8"?>
<database defaultIdMethod="native" name="flash">
<table name="demand">
<column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true" />
<column name="label" type="VARCHAR" size="90" required="true" />
<column name="description" type="LONGVARCHAR" required="true" />
</table>
<table name="customer">
<column name="id" type="SMALLINT" primaryKey="true" autoIncrement="true" required="true" />
<column name="name" type="VARCHAR" size="32" required="true" />
<column name="service_id" type="SMALLINT" required="true" />
<foreign-key name="fk_customer_service" foreignTable="service">
<reference local="service_id" foreign="id"/>
</foreign-key>
</table>
<table name="service">
<column name="id" type="SMALLINT" primaryKey="true" autoIncrement="true" required="true" />
<column name="label" type="VARCHAR" size="4" />
</table>
<table name="demand_customer" isCrossRef="true">
<column name="demand_id" type="INTEGER" primaryKey="true" required="true" />
<column name="customer_id" type="SMALLINT" primaryKey="true" required="true" />
<foreign-key name="fk_demand_customer_demand" foreignTable="demand" onDelete="CASCADE">
<reference local="demand_id" foreign="id"/>
</foreign-key>
<foreign-key name="fk_demand_customer_customer" foreignTable="customer" onDelete="CASCADE">
<reference local="customer_id" foreign="id"/>
</foreign-key>
</table>
</database>
I would like to retrieve all demands with service of customers attached is equal to 3 (for example). With data below, I would just have demand id number 2.
Could you help me ?
Data examples :
demand
-------------------------------
id | label | description
-------------------------------
1 test1 desc1
2 test2 desc2
customer
------------------------------
id | name | service_id
------------------------------
1 name1 1
2 name2 3
3 name2 2
service
-------------
id | label
-------------
1 service1
2 service2
3 service3
demand_customer
---------------------------
demand_id | customer_id
---------------------------
1 1
2 1
2 2

I found how to do this :
$demands = DemandQuery::create()
->useDemandCustomerQuery()
->useCustomerQuery()
->filterByServiceId(getSessionValue('service'))
->endUse()
->endUse()
->find();

Related

How to import XML to MySQL with parent and child elements?

Using the following query I can read all <vehicle> elements from the XML and save it into database but I want that each vehicle also store its parent <timestep> time so that I know which time step it belongs to.
query
LOAD XML LOCAL INFILE 'vehicle.xml'
INTO TABLE vehicles
ROWS IDENTIFIED BY '<vehicle>'
I tired creating a table timesteps where I added a time column and all the other columns too but it gets the last vehicle from each timestep only!, while I want to get all the children.
Part of my XML
<?xml version="1.0" encoding="UTF-8"?>
<fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
<timestep time="0.00">
<vehicle id="0" x="3654.27" y="3699.20" angle="327.39" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="21059650#7_0" slope="0.00"/>
</timestep>
<timestep time="1.00">
<vehicle id="0" x="3653.49" y="3700.41" angle="327.39" type="DEFAULT_VEHTYPE" speed="1.44" pos="6.54" lane="21059650#7_0" slope="0.00"/>
<vehicle id="1" x="2592.95" y="3497.59" angle="60.95" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="22408082_0" slope="0.00"/>
</timestep>
<timestep time="2.00">
<vehicle id="0" x="3651.81" y="3703.04" angle="327.39" type="DEFAULT_VEHTYPE" speed="3.12" pos="9.66" lane="21059650#7_0" slope="0.00"/>
<vehicle id="1" x="2595.17" y="3498.82" angle="60.95" type="DEFAULT_VEHTYPE" speed="2.54" pos="7.64" lane="22408082_0" slope="0.00"/>
<vehicle id="2" x="4551.99" y="4411.11" angle="328.95" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="23657587_0" slope="0.00"/>
</timestep>
<timestep time="3.00">
<vehicle id="0" x="3649.14" y="3707.21" angle="327.39" type="DEFAULT_VEHTYPE" speed="4.95" pos="14.61" lane="21059650#7_0" slope="0.00"/>
<vehicle id="1" x="2599.61" y="3501.29" angle="60.95" type="DEFAULT_VEHTYPE" speed="5.08" pos="12.73" lane="22408082_0" slope="0.00"/>
<vehicle id="2" x="4550.98" y="4412.79" angle="328.95" type="DEFAULT_VEHTYPE" speed="1.96" pos="7.06" lane="23657587_0" slope="0.00"/>
<vehicle id="3" x="4460.55" y="5390.34" angle="58.38" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="-22975228#1_0" slope="0.00"/>
</timestep>
<timestep time="4.00">
<vehicle id="0" x="3645.25" y="3713.30" angle="327.39" type="DEFAULT_VEHTYPE" speed="7.23" pos="21.83" lane="21059650#7_0" slope="0.00"/>
<vehicle id="1" x="2605.71" y="3504.68" angle="60.95" type="DEFAULT_VEHTYPE" speed="6.98" pos="19.70" lane="22408082_0" slope="0.00"/>
<vehicle id="2" x="4549.12" y="4415.87" angle="328.95" type="DEFAULT_VEHTYPE" speed="3.59" pos="10.66" lane="23657587_0" slope="0.00"/>
<vehicle id="3" x="4461.94" y="5391.20" angle="58.38" type="DEFAULT_VEHTYPE" speed="1.63" pos="6.73" lane="-22975228#1_0" slope="0.00"/>
<vehicle id="4" x="6607.38" y="6648.96" angle="238.83" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="22983344_0" slope="0.00"/>
</timestep>
<timestep time="5.00">
<vehicle id="0" x="3640.37" y="3720.81" angle="326.68" type="DEFAULT_VEHTYPE" speed="8.95" pos="30.78" lane="21059650#7_0" slope="0.00"/>
<vehicle id="1" x="2613.55" y="3509.03" angle="60.95" type="DEFAULT_VEHTYPE" speed="8.97" pos="28.67" lane="22408082_0" slope="0.00"/>
<vehicle id="2" x="4545.93" y="4421.17" angle="328.95" type="DEFAULT_VEHTYPE" speed="6.19" pos="16.84" lane="23657587_0" slope="0.00"/>
<vehicle id="3" x="4465.05" y="5393.11" angle="58.38" type="DEFAULT_VEHTYPE" speed="3.64" pos="10.37" lane="-22975228#1_0" slope="0.00"/>
<vehicle id="4" x="6605.78" y="6648.00" angle="238.83" type="DEFAULT_VEHTYPE" speed="1.86" pos="6.96" lane="22983344_0" slope="0.00"/>
<vehicle id="5" x="6028.95" y="3131.17" angle="40.09" type="DEFAULT_VEHTYPE" speed="0.00" pos="5.10" lane="264312282#1_0" slope="0.00"/>
</timestep>
</fcd-export>
You have to have a column named time in your vehicles table.
The official Documentation describes your case:
Using a ROWS IDENTIFIED BY '<tagname>' clause, it is possible to
import data from the same XML file into database tables with different
definitions. For this example, suppose that you have a file named
address.xml which contains the following XML:
<?xml version="1.0"?>
<list>
<person person_id="1">
<fname>Robert</fname>
<lname>Jones</lname>
<address address_id="1" street="Mill Creek Road" zip="45365" city="Sidney"/>
<address address_id="2" street="Main Street" zip="28681" city="Taylorsville"/>
</person>
<person person_id="2">
<fname>Mary</fname>
<lname>Smith</lname>
<address address_id="3" street="River Road" zip="80239" city="Denver"/>
<!-- <address address_id="4" street="North Street" zip="37920" city="Knoxville"/> -->
</person>
</list>
Now create an address table in the test database using the following
CREATE TABLE statement:
CREATE TABLE address (
address_id INT NOT NULL PRIMARY KEY,
person_id INT NULL,
street VARCHAR(40) NULL,
zip INT NULL,
city VARCHAR(40) NULL,
created TIMESTAMP
);
...
To import the data from the elements into the address table,
use the LOAD XML statement shown here:
mysql> LOAD XML LOCAL INFILE 'address.xml'
-> INTO TABLE address
-> ROWS IDENTIFIED BY '<address>';
Query OK, 3 rows affected (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
You can see that the data was imported using a SELECT statement such
as this one:
mysql> SELECT * FROM address;
+------------+-----------+-----------------+-------+--------------+---------------------+
| address_id | person_id | street | zip | city | created |
+------------+-----------+-----------------+-------+--------------+---------------------+
| 1 | 1 | Mill Creek Road | 45365 | Sidney | 2007-07-24 17:37:37 |
| 2 | 1 | Main Street | 28681 | Taylorsville | 2007-07-24 17:37:37 |
| 3 | 2 | River Road | 80239 | Denver | 2007-07-24 17:37:37 |
+------------+-----------+-----------------+-------+--------------+---------------------+
3 rows in set (0.00 sec)
The data from the <address> element that is enclosed in XML comments
is not imported. However, since there is a person_id column in the
address table, the value of the person_id attribute from the parent
<person> element for each <address> is imported into the address
table.

Where is magento register user details stored

I am running Magento 1.7.2. and i want to know where is magento register user details stored. actually i need, if we add a new field in registration form where the value of field was stored in database table.
Following is list of tables where user detail is stored.
| customer_address_entity |
| customer_address_entity_datetime |
| customer_address_entity_decimal |
| customer_address_entity_int |
| customer_address_entity_text |
| customer_address_entity_varchar |
| customer_entity |
| customer_entity_datetime |
| customer_entity_decimal |
| customer_entity_int |
| customer_entity_text |
| customer_entity_varchar |
And attributes are stored in following table
customer_eav_attribute
The Best way of doing this by creating customer Attribute.
You can do this by following below steps :
/app/code/local/Your/Customattribute/sql/your_customattribute_setup/install-0.1.0.php
<?php
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute("customer", "customattribute", array(
"type" => "varchar",
"backend" => "",
"label" => "Custom Attribute",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => "Custom Attribute"
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "customattribute");
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'customattribute',
'999' //sort_order
);
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
//$used_in_forms[]="customer_account_edit";
//$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->endSetup();
/app/code/local/Your/Customattribute/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Your_Customattribute>
<version>0.1.0</version>
</Your_Customattribute>
</modules>
<global>
<resources>
<Your_Customattribute_setup>
<setup>
<module>Your_Customattribute</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</Your_Customattribute_setup>
<Your_Customattribute_write>
<connection>
<use>core_write</use>
</connection>
</Your_Customattribute_write>
<Your_Customattribute_read>
<connection>
<use>core_read</use>
</connection>
</Your_Customattribute_read>
</resources>
</global>
</config>
app/etc/modules/Your_Customattribute.xml
<?xml version="1.0"?>
<config>
<modules>
<Your_Customattribute>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Your_Customattribute>
</modules>
</config>
Then to retrieve or edit you use:
$customer = Mage::getModel('customer/customer')->load($custid);
$customer->getCustomattribute();
$customer->setCustomattribute($yourjson);

Variable Mappings in SSIS ForEach Loop

I'm populating an ado net dataset in SSIS 2008 with a stored procedure. The resultset contains 21 columns. When using a For Each loop in SSIS, it appears that if I want the first two columns, then the 20th column I can't use index 0,1, 19. It appears that I have to use create mappings for 2-18 if i want to use index 19. Is there a way to only use the fields you want?
Thanks In Advance.
Works fine, I'm not sure what you're running into.
I created a basic package with an Execute SQL Task feeding into a Foreach Loop Container.
My source query is ugly but it satisfies my laziness
SELECT
*
FROM
(
SELECT TOP 21
CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS int) AS rn
FROM
sys.all_columns AS AC
) D
PIVOT
(
max(rn)
FOR rn IN ([1]
, [2]
, [3]
, [4]
, [5]
, [6]
, [7]
, [8]
, [9]
, [10]
, [11]
, [12]
, [13]
, [14]
, [15]
, [16]
, [17]
, [18]
, [19]
, [20]
, [21]
)
) P;
I push that full resultset into an SSIS Variable called rsResults of type Object.
Within my Foreach Loop Container, I map variables Col00, Col01 and Col19 into ordinal positions 0, 1 and 19
Biml all the things
If you have the free addon to Visual Studio, BIDS Helper you can recreate my solution with a few clicks. Biml is the business intelligence markup language, think of it as the operating system for BI. Here we'll use some XML to describe the SSIS package we want.
Add new .biml file to your SSIS project
Fix the third line there to point to a valid database
Right click on the biml file and select "Generate SSIS Package"
Profit
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Connections>
<!-- UPDATE ME -->
<OleDbConnection Name="CM_OLE" ConnectionString="Data Source=localhost\dev2014;Initial Catalog=tempdb;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;" />
</Connections>
<Packages>
<Package ConstraintMode="Linear" Name="so_29220275">
<Variables>
<Variable DataType="Object" Name="rsResults" />
<Variable DataType="Int32" Name="Col00">-1</Variable>
<Variable DataType="Int32" Name="Col01">-1</Variable>
<Variable DataType="Int32" Name="Col19">-1</Variable>
<Variable DataType="String" Name="QuerySource"><![CDATA[SELECT
*
FROM
(
SELECT TOP 21
CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS int) AS rn
FROM
sys.all_columns AS AC
) D
PIVOT
(
max(rn)
FOR rn IN ([1]
, [2]
, [3]
, [4]
, [5]
, [6]
, [7]
, [8]
, [9]
, [10]
, [11]
, [12]
, [13]
, [14]
, [15]
, [16]
, [17]
, [18]
, [19]
, [20]
, [21]
)
) P;
]]></Variable>
</Variables>
<Tasks>
<ExecuteSQL ConnectionName="CM_OLE" Name="SQL - gen data" ResultSet="Full">
<VariableInput VariableName="User.QuerySource" />
<Results>
<Result VariableName="User.rsResults" Name="0"></Result>
</Results>
</ExecuteSQL>
<ForEachAdoLoop SourceVariableName="User.rsResults" ConstraintMode="Linear" Name="FELC 3 of 20">
<VariableMappings>
<VariableMapping VariableName="User.Col00" Name="0" />
<VariableMapping VariableName="User.Col01" Name="1" />
<VariableMapping VariableName="User.Col19" Name="19" />
</VariableMappings>
<Tasks>
<ExecuteSQL ConnectionName="CM_OLE" Name="SQL - do nothing">
<DirectInput>SELECT 1;</DirectInput>
</ExecuteSQL>
</Tasks>
</ForEachAdoLoop>
</Tasks>
</Package>
</Packages>
</Biml>

SQL Server 2008 OPENXML Insert

I have an XML document that is pretty much structured like an Excel spreadsheet based on the schema. There are two worksheets in this file. How can I insert all the data from the second sheet into my SQL table? I basically want a straight import, but OPENXML keeps telling me 0 rows affected. Here is a sample of the XML doc:
<Worksheet ss:Name="Product Level Data">
<x:WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>1</PaperSizeIndex>
<Scale>100</Scale>
<FitWidth>1</FitWidth>
<FitHeight>1</FitHeight>
<HorizontalResolution>300</HorizontalResolution>
<VerticalResolution>300</VerticalResolution>
</Print>
<Zoom>100</Zoom>
<x:PageSetup>
<x:Header ss:StyleID="systemtitle" Data="The SAS System"
/>
</x:PageSetup>
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>1</SplitHorizontal>
<TopRowBottomPane>1</TopRowBottomPane>
<ActivePane>2</ActivePane>
<Panes>
<Pane>
<Number>3</Number>
</Pane>
<Pane>
<Number>2</Number>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</x:WorksheetOptions>
<AutoFilter x:Range="R1C1:R545C10" xmlns="urn:schemas-microsoft-com:office:excel"></AutoFilter><Table ss:StyleID="_body">
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="270"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="90"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="108"/>
<ss:Column ss:AutoFitWidth="1" ss:Width="108"/>
<Row ss:AutoFitHeight="1">
<Cell ss:StyleID="header__c" ss:Index="1"><Data ss:Type="String">ID</Data></Cell>
<Cell ss:StyleID="header__c" ss:Index="2"><Data ss:Type="String">Company Name</Data></Cell>
<Cell ss:StyleID="header__l" ss:Index="3"><Data ss:Type="String">Item</Data></Cell>
<Cell ss:StyleID="header__c" ss:Index="4"><Data ss:Type="String">Type</Data></Cell>
<Cell ss:StyleID="header__r1" ss:Index="5"><Data ss:Type="String">Total Time</Data></Cell>
<Cell ss:StyleID="header__r1" ss:Index="6"><Data ss:Type="String">Total Amount</Data></Cell>
<Cell ss:StyleID="header__r1" ss:Index="7"><Data ss:Type="String">Count Product</Data></Cell>
<Cell ss:StyleID="header__r" ss:Index="8"><Data ss:Type="String">Percentage Time</Data></Cell>
<Cell ss:StyleID="header__r" ss:Index="9"><Data ss:Type="String">Product Time</Data></Cell>
<Cell ss:StyleID="header__r" ss:Index="10"><Data ss:Type="String">Invalid Product Time</Data></Cell>
</Row>
<Row ss:AutoFitHeight="1">
<Cell ss:StyleID="data__l" ss:Index="1"><Data ss:Type="String">DF</Data></Cell>
<Cell ss:StyleID="data__l" ss:Index="2"><Data ss:Type="String">Dan's Fruit Company</Data></Cell>
<Cell ss:StyleID="data__l" ss:Index="3"><Data ss:Type="String">Apple</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="4"><Data ss:Type="String">Fruit</Data></Cell>
<Cell ss:StyleID="data__r1" ss:Index="5"><Data ss:Type="Number">2034004</Data></Cell>
<Cell ss:StyleID="data__r1" ss:Index="6"><Data ss:Type="Number">23423</Data></Cell>
<Cell ss:StyleID="data__r1" ss:Index="7"><Data ss:Type="Number">15789</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="8"><Data ss:Type="Number">100.0</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="9"><Data ss:Type="Number">0.000</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="10"><Data ss:Type="Number">0.000</Data></Cell>
</Row>
Here is the code I am trying. This is the first time I've tried to use OPENXML:
DECLARE #XMLDocPointer INT
, #strXML VARCHAR(10000)
SET #strXML = <large code block up there>
EXEC sp_xml_preparedocument #XMLDocPointer OUTPUT, #strXML
BEGIN TRANSACTION
INSERT INTO xml_staging_table
(
id
, company_name
, item
, type
, total_time
, total_amount
, count_product
, percent_time
, product_time
, invalid_product_time
)
SELECT id
, company_name
, item
, type
, total_time
, total_amount
, count_product
, percentage_time
, product_time
, invalid_product_time
FROM OPENXML(#XMLDocPointer,'/WORKSHEET/TABLE/ROW/CELL', 4) --What is the correct syntax here?
WITH
(
id VARCHAR(10) 'id/.'
, company_name VARCHAR(500) 'company_name/.'
, item VARCHAR(10) 'item/.'
, type VARCHAR(50) 'type/.'
, total_time BIGINT 'total_time/.'
, total_amount BIGINT 'total_amount/.'
, count_product BIGINT 'count_product/.'
, percentage_time DECIMAL(5,2) 'percentage_time/.'
, product_time BIGINT 'product_time/.'
, invalid_product_time BIGINT 'invalid_product_time/.'
)
COMMIT
EXEC sp_xml_removedocument #XMLDocPointer
Not sure how to do it with OPENQUERY - this is deprecated, you should use the XQuery support in SQL Server 2005 and newer directly.
Also: your XML is incomplete in that it doesn't show the definition of the ss: xml namespace anywhere - but ignoring that - you should be able to get your data with a SQL statement something like this:
-- reduced huge XML to the actual relevant parts...
-- defined "dummy" XML namespace for ss: prefix
DECLARE #input XML = '<Worksheet ss:Name="Product Level Data" xmlns:ss="urn:test">
<Table ss:StyleID="_body">
<Row ss:AutoFitHeight="1">
<Cell ss:StyleID="data__l" ss:Index="1"><Data ss:Type="String">DF</Data></Cell>
<Cell ss:StyleID="data__l" ss:Index="2"><Data ss:Type="String">Dan''s Fruit Company</Data></Cell>
<Cell ss:StyleID="data__l" ss:Index="3"><Data ss:Type="String">Apple</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="4"><Data ss:Type="String">Fruit</Data></Cell>
<Cell ss:StyleID="data__r1" ss:Index="5"><Data ss:Type="Number">2034004</Data></Cell>
<Cell ss:StyleID="data__r1" ss:Index="6"><Data ss:Type="Number">23423</Data></Cell>
<Cell ss:StyleID="data__r1" ss:Index="7"><Data ss:Type="Number">15789</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="8"><Data ss:Type="Number">100.0</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="9"><Data ss:Type="Number">0.000</Data></Cell>
<Cell ss:StyleID="data__r" ss:Index="10"><Data ss:Type="Number">0.000</Data></Cell>
</Row>
</Table>
</Worksheet>'
SELECT
XmlCell.value('(.)[1]', 'varchar(50)')
FROM
#Input.nodes('/Worksheet/Table/Row/Cell/Data') AS Nodes(XmlCell)
This gives me an output of:
DF
Dan's Fruit Company
Apple
Fruit
2034004
23423
15789
100.0
0.000
0.000
Update: to handle the "full" input XML, and just extracting the cells that have a StyleID that is something like data...., you can use this T-SQL statement - again, it has to know the definition of the ss: XML namespace - I just "tweaked" it here to something I choose - replace this with your actual XML namespace for the ss: prefix:
;WITH XMLNAMESPACES('urn:test' AS ss),
XmlParsedData AS
(
SELECT
CellStyle = XmlCell.value('#ss:StyleID', 'varchar(50)'),
CellIndex = XmlCell.value('#ss:Index', 'int'),
CellValue = XmlCell.value('(Data)[1]', 'varchar(50)')
FROM
#Input.nodes('/Worksheet/Table/Row/Cell') AS Nodes(XmlCell)
)
SELECT *
FROM XmlParsedData
WHERE
CelLStyle LIKE 'data%'
This will give you an output like:
CellStyle CellIndex CellValue
data__l 1 DF
data__l 2 Dan's Fruit Company
data__l 3 Apple
data__r 4 Fruit
data__r1 5 2034004
data__r1 6 23423
data__r1 7 15789
data__r 8 100.0
data__r 9 0.000
data__r 10 0.000

How to read xml in t-sql?

Could anyone tell me how I could select values from the following xml:
I would like to get rows as follows:
Col1: key, Col2: key, Col3: value
1 1 value
1 2 value2
1 3 value3
2 4 value4
2 5 value5
2 6 value6
Here is the xml:
<root>
<item>
<key>1</key>
<value>
<params>
<item>
<key>1</key>
<value>value</value>
</item>
<item>
<key>2</key>
<value>value2</value>
</item>
<item>
<key>3</key>
<value>value3</value>
</item>
</params>
</value>
</item>
<item>
<key>2</key>
<value>
<params>
<item>
<key>4</key>
<value>value4</value>
</item>
<item>
<key>5</key>
<value>value5</value>
</item>
<item>
<key>6</key>
<value>value6</value>
</item>
</params>
</value>
</item>
</root>
Assuming you have this XML in a T-SQL variable - then you could use this snippet of code:
DECLARE #input XML = '...(your XML here).....'
SELECT
Key1 = Item.value('(key)[1]', 'int'),
Key2 = Item2.value('(key)[1]', 'int'),
ItemValue = Item2.value('(value)[1]', 'varchar(50)')
FROM
#input.nodes('/root/item') AS T(Item)
CROSS APPLY
item.nodes('value/params/item') AS T2(Item2)
This gives me an output of:
Key1 Key2 ItemValue
1 1 value
1 2 value2
1 3 value3
2 4 value4
2 5 value5
2 6 value6
The approach is the following:
grab the list of <item> nodes under <root> as your first "list of XML nodes" with the first .nodes() XQuery method, and extract the value of the <key> XML element in that XML fragment into Key1
grab the "nested" list of XML nodes inside that XML fragment, using the value/params/item XPath, to get the child rows - and extract the values from <key> and <value> from those nested child XML fragments into Key2 and ItemValue