Showing posts with label hierarchy. Show all posts
Showing posts with label hierarchy. Show all posts

Friday, March 9, 2012

for xml hierarchy

Hi

I have a table that looks like this

declare @.VC table ( V varchar(100), VC int, depth tinyint)
insert into @.VC values ( 'TN', 1, 1)
insert into @.VC values ( 'TN', 2, 2);

and I have for xml query

select
V as @.value,
(
select VC as '@.value' from @.VC pe where pe.V = n.V
for xml path ('Value'), root('Values'), type
) as ME
from ( select distinct VC from @.VC ) n
for xml path ('Value'), root('Values')

that gives me something like this

<Values>
<Value value="TN">
<ME>
<Values>
<Value value="1" />
<Value value="2" />
</Values>
</ME>
</Value>
</Values>

However I need to reorder the xml to look like this according to depth

<Values>
<Value value="TN">
<ME>
<Values depth = 1>
<Value value="1" />
</Values>
</ME>
<ME>
<Values depth =2>
<Value value="2" />
</Values>
</ME>
</Value>
</Values>

the problem I am having is that depth 2 is not below depth 1 node, is on the same depth with the depth attribute diff.

Is there some way to write the for xml to do this

thanks

P


select
V as "@.value",
(
select pe.depth as "Values/@.depth",
pe.VC as "Values/Value/@.value"
from @.VC pe where pe.V = n.V
for xml path('ME'),type
)
from ( select distinct V from @.VC ) n
for xml path ('Value'), root('Values')

Wednesday, March 7, 2012

FOR XML AUTO not producing the proper parent-child hierarchy

I have a query that uses two views that should produce a simple parent-child
XML document however it repeats every
parent (topic) record for each child record. Here's the query:
select topic.id,
topic.name,
topic.sename,
topic.description,
topic.parentsubjectname,
topic.parentcategroyname,
0 WL,
item.id,
item.name,
item.sename,
item.about,
item.avgrating
from dbo.pico_topic_info topic
join dbo.pico_item_info item on topic.id = item.ParentTopicID
where topic.id = 344
order by topic.id, item.id
for xml auto, elements
I expect a document that looks like this:
<topic>
<id>344</id>
<name>Some Topic</name>
..
<item>
<id>123123</id>
<name>item 1 </name>
..
</item>
<item>
<id>543453</id>
<name>item 2</name>
..
</item>
</topic>
However I get a topic element with every item element:
<topic>
<id>344</id>
<name>Some Topic</name>
..
<item>
<id>123123</id>
<name>item 1 </name>
..
</item>
</topic>
<topic>
<id>344</id>
<name>Some Topic</name>
..
<item>
<id>543453</id>
<name>item 2</name>
..
</item>
</topic>
Any ideas why? I've not had this problem before.
--BuddyWell, I figured it out. The description column is an ntext column which app
arently doesn't play well with other columns
when the FOR XML clause is used.
--Buddy
Buddy Ackerman wrote:
> I have a query that uses two views that should produce a simple
> parent-child XML document however it repeats every parent (topic) record
> for each child record. Here's the query:
>
> select topic.id,
> topic.name,
> topic.sename,
> topic.description,
> topic.parentsubjectname,
> topic.parentcategroyname,
> 0 WL,
> item.id,
> item.name,
> item.sename,
> item.about,
> item.avgrating
> from dbo.pico_topic_info topic
> join dbo.pico_item_info item on topic.id = item.ParentTopicID
> where topic.id = 344
> order by topic.id, item.id
> for xml auto, elements
>
> I expect a document that looks like this:
> <topic>
> <id>344</id>
> <name>Some Topic</name>
> ...
> <item>
> <id>123123</id>
> <name>item 1 </name>
> ...
> </item>
> <item>
> <id>543453</id>
> <name>item 2</name>
> ...
> </item>
> </topic>
> However I get a topic element with every item element:
>
>
> <topic>
> <id>344</id>
> <name>Some Topic</name>
> ...
> <item>
> <id>123123</id>
> <name>item 1 </name>
> ...
> </item>
> </topic>
> <topic>
> <id>344</id>
> <name>Some Topic</name>
> ...
> <item>
> <id>543453</id>
> <name>item 2</name>
> ...
> </item>
> </topic>
>
> Any ideas why? I've not had this problem before.
>
> --Buddy

FOR XML AUTO not producing the proper parent-child hierarchy

I have a query that uses two views that should produce a simple parent-child XML document however it repeats every
parent (topic) record for each child record. Here's the query:
select topic.id,
topic.name,
topic.sename,
topic.description,
topic.parentsubjectname,
topic.parentcategroyname,
0 WL,
item.id,
item.name,
item.sename,
item.about,
item.avgrating
from dbo.pico_topic_info topic
join dbo.pico_item_info item on topic.id = item.ParentTopicID
where topic.id = 344
order by topic.id, item.id
for xml auto, elements
I expect a document that looks like this:
<topic>
<id>344</id>
<name>Some Topic</name>
...
<item>
<id>123123</id>
<name>item 1 </name>
...
</item>
<item>
<id>543453</id>
<name>item 2</name>
...
</item>
</topic>
However I get a topic element with every item element:
<topic>
<id>344</id>
<name>Some Topic</name>
...
<item>
<id>123123</id>
<name>item 1 </name>
...
</item>
</topic>
<topic>
<id>344</id>
<name>Some Topic</name>
...
<item>
<id>543453</id>
<name>item 2</name>
...
</item>
</topic>
Any ideas why? I've not had this problem before.
--Buddy
Well, I figured it out. The description column is an ntext column which apparently doesn't play well with other columns
when the FOR XML clause is used.
--Buddy
Buddy Ackerman wrote:
> I have a query that uses two views that should produce a simple
> parent-child XML document however it repeats every parent (topic) record
> for each child record. Here's the query:
>
> select topic.id,
> topic.name,
> topic.sename,
> topic.description,
> topic.parentsubjectname,
> topic.parentcategroyname,
> 0 WL,
> item.id,
> item.name,
> item.sename,
> item.about,
> item.avgrating
> from dbo.pico_topic_info topic
> join dbo.pico_item_info item on topic.id = item.ParentTopicID
> where topic.id = 344
> order by topic.id, item.id
> for xml auto, elements
>
> I expect a document that looks like this:
> <topic>
> <id>344</id>
> <name>Some Topic</name>
> ...
> <item>
> <id>123123</id>
> <name>item 1 </name>
> ...
> </item>
> <item>
> <id>543453</id>
> <name>item 2</name>
> ...
> </item>
> </topic>
> However I get a topic element with every item element:
>
>
> <topic>
> <id>344</id>
> <name>Some Topic</name>
> ...
> <item>
> <id>123123</id>
> <name>item 1 </name>
> ...
> </item>
> </topic>
> <topic>
> <id>344</id>
> <name>Some Topic</name>
> ...
> <item>
> <id>543453</id>
> <name>item 2</name>
> ...
> </item>
> </topic>
>
> Any ideas why? I've not had this problem before.
>
> --Buddy

Friday, February 24, 2012

for each record updated

Currently in my database I have Widgets. These Widgest can be linked
together through keys in a Link table. Think in terms of hierarchy. A
parent Widget has child Widgets linked to it. These child Widgets when
linked take on properties(field values) of the parent Widgets. I have a
trigger that fires when a property(field) is changed in the parent
Widget that changes that property(field) in all the child Widgets. I
want to add to that trigger so that the trigger will also insert a new
record into a log table for each record updated. Here is some example
code:
SELECT @.widget_id = widget_id, @.status = status
FROM inserted
BEGIN
update widget
set status = @.status, updated_date = @.current_date
where widget_id in (select linked_id
from link
where topic = 'LINK'
and log_type = 'CLONE'
and id = @.widget_id)
and defect_status != 'Complete'
END
For each of these children widgets updated I want to insert a record
into a log table with the widget_id and the linked_id as fields. Anyone
have any ideas. I hope this is somewhat clear.
JakeHello Jake !
To direct you to the right solution some DDL would be very nice,
because we don=B4t know your table structure and how the data is stored.
http://www.aspfaq.com/5006
After posting we will be pleased to help you :-D
HTH, Jens Suessmeyer.|||> SELECT @.widget_id = widget_id, @.status = status
> FROM inserted
Triggers are in their essence set-based. You just made yours lose that
wonderful ability.
You don't need variables to propagate changes from one table to another:
update <destination_table>
set <dest_column_1> = <src_column_1>
..
from inserted
<join> <destination_table>
on <destination_table>.<common_key> =
inserted.<common_key>
ML