Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
pmni
Starting Member
3 Posts |
Posted - 2011-12-31 : 10:51:16
|
Hi. I'm developing a information center that needed an message center. In interface I want to display the messages like a conversation Outlook style.It's quite simple, basic, and its based on message and reply.I'm using SQL Server 2008, and have this table:MessageId int PKDate datetimeFrom uniqueidentifier FKTo uniqueidentifier FKBody varcharSubject varcharParentId int nullHierarchy hierarchyid nullI'm using Entity Framewok to insert the messages, and use a trigger to update the hierarchyId.The trigger is this:ALTER TRIGGER [dbo].[trg_UpdateHierarchy] ON [dbo].[Messages] AFTER INSERTAS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for trigger here DECLARE @messageId int DECLARE @ParentId int SELECT @messageId = inserted.MessageId, @ParentId = inserted.ParentId FROM inserted IF(@ParentId IS NULL) BEGIN UPDATE [Messages] SET Hierarchy = hierarchyid::GetRoot() WHERE [Messages].MessageId = @messageId RETURN END ELSE BEGIN DECLARE @parent hierarchyid SELECT @parent = Hierarchy FROM [Messages] WHERE [Messages].MessageId = @ParentId DECLARE @lastHierarchy hierarchyid SELECT @lastHierarchy = MAX(Hierarchy) FROM [Messages] WHERE Hierarchy.GetAncestor(1) = @parent UPDATE [Messages] SET Hierarchy = @parent.GetDescendant(@lastHierarchy, NULL) WHERE [Messages].MessageId = @messageId ENDEND If I have messageId = 1 is the start of a new thread, and messageId = 2 has parentId = 1, and messageId = 3 has parentId = 3.When read the records I have this:messageId = 1, hierarchy = /messageId = 2, hierarchy = /1/messageId = 3, hierarchy = /1/1/ What I'm doing wrong? Thanks in advance |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-01 : 11:51:55
|
From your code, that seems like what would happen - i.e., each new entry would be child of the previous entry. If that is not what you are looking for, can you show your desired output? quote: If I have messageId = 1 is the start of a new thread, and messageId = 2 has parentId = 1, and messageId = 3 has parentId = 3.
Is there a typo in there? Is messageId 3 its own parent? |
 |
|
|
pmni
Starting Member
3 Posts |
Posted - 2012-01-02 : 04:22:05
|
| Yes it's an error, messageId = 3 has parent messageId = 2.My desired outpu is:messageId = 1, hierarchy = /messageId = 2, hierarchy = /1/messageId = 3, hierarchy = /1/2/Thanks for your reply. |
 |
|
|
pmni
Starting Member
3 Posts |
Posted - 2012-01-02 : 07:15:51
|
| I'm discover that my idea about hierarchyid is wrong. Hierarchyid don't maintain the referencial integraty, but helps on searchs!Thanks any way! |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-02 : 08:15:09
|
| HierarchyId is not meant for maintaining referential integrity, although you could apply a uniqueness constraint on a hierarchyid column or even make it the primary key. The purpose of hierarchyid really is to facilitate hierarchies such as organizational charts. You can insert or remove members at will, and even move whole subtrees etc. |
 |
|
|
|
|
|
|
|