Assign values from same table

Posted by Reddy S R on Stack Overflow See other posts from Stack Overflow or by Reddy S R
Published on 2012-03-29T02:35:36Z Indexed on 2012/03/29 11:30 UTC
Read the original article Hit count: 221

Filed under:

I have a database table with parent child relationships between different rows.
1 parent can have any number of children.
Children do not have children.
I want to copy 'Message' from 'Parent Category' to child categories.

CategoryID   Name           Value       Message      ParentID     DeptId
1            Books          9           Specials                  1
2            Music          7                                     1
3            Paperback      25                       1            1
4            PDFs           26                       1            2
5            CDs            35                       2            1

If that was sample data, Paperback should have Specials as it's Message after the query is run.
I have gotten the child rows (the query runs very slow, don't know why), but how do I get the data and assign it to appropriate child rows?

--@DeptId = 1
select * from Categories
where ParentID in(
select CategoryID from Categories 
where DeptID = @DeptId
)

I would like to see a solution that would not use cursors.

Thanks

© Stack Overflow or respective owner

Related posts about sql