Question : show a message and want  to see all the threads to that message.

got another question.

if I click on show a message then I want to see all the threads to that message.

Need to build a stored procedure.

ALTER PROCEDURE [dbo].[prc_Messages_Response_Select_All]
      

AS
BEGIN
      SET NOCOUNT ON
      
    SELECT
            a.[MessageID],
            a.[ProfileID],
            a.[FromProfileID],
            a.[FromUsername],
            b.[UserName] AS SentToUserName,
            a.[Subject],
            a.[Body],
            convert(varchar, a.[MsgDate], 107) AS zdate,
            a.[MsgDate],
            a.[MsgRead],
            a.[MsgReplied],
            a.[MsgNew],
            a.[ReplyOfMessageID],
            a.[InitialMessageID]
    FROM
            [tbl_Messages] a
      INNER JOIN
            [tbl_Profile] b
            ON
                  a.[ProfileID]=b.[ProfileID]
    WHERE
            a.[FromProfileID] =@FromProfileID
    ORDER BY
            a.[MsgDate] DESC
END
GO
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
not sure how to do this?

USE [omegalove]
GO

/****** Object:  Table [dbo].[tbl_Messages]    Script Date: 08/21/2010 15:49:16 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tbl_Messages](
	[MessageID] [int] IDENTITY(1,1) NOT NULL,
	[ProfileID] [int] NOT NULL,
	[FromProfileID] [int] NULL,
	[FromUsername] [nvarchar](50) NULL,
	[Subject] [nvarchar](50) NULL,
	[Body] [nvarchar](max) NULL,
	[MsgDate] [datetime] NULL,
	[MsgRead] [int] NULL,
	[MsgReplied] [int] NULL,
	[MsgNew] [int] NULL,
	[ReplyOfMessageID] [int] NULL,
	[InitialMessageID] [int] NULL,
 CONSTRAINT [PK_tbl_Messages] PRIMARY KEY CLUSTERED 
(
	[MessageID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tbl_Messages] ADD  CONSTRAINT [DF_tbl_Messages_MsgDate]  DEFAULT (getdate()) FOR [MsgDate]
GO

ALTER TABLE [dbo].[tbl_Messages] ADD  CONSTRAINT [DF_tbl_Messages_MsgRead]  DEFAULT ((0)) FOR [MsgRead]
GO

ALTER TABLE [dbo].[tbl_Messages] ADD  CONSTRAINT [DF_tbl_Messages_MsgReplied]  DEFAULT ((0)) FOR [MsgReplied]
GO

ALTER TABLE [dbo].[tbl_Messages] ADD  CONSTRAINT [DF_Messages_MsgNew]  DEFAULT ((0)) FOR [MsgNew]
GO

ALTER TABLE [dbo].[tbl_Messages] ADD  CONSTRAINT [DF_tbl_Messages_ReplyOfMessageID]  DEFAULT ((0)) FOR [ReplyOfMessageID]
GO

ALTER TABLE [dbo].[tbl_Messages] ADD  CONSTRAINT [DF_tbl_Messages_InitialMessageID]  DEFAULT ((0)) FOR [InitialMessageID]
GO

Answer : show a message and want  to see all the threads to that message.

Maybe something like this will help. It uses a recursive common table expression approach. The principal is that the first query is your anchor query which in your case is your the initial section using @FromProfileID. Then using CTE recursion (UNION ALL), you join in those messages that are either replies or linked to initial  message ID. If your secondary level messages always has a reply of message ID you probably don't need the second check in the ON clause.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
;WITH msgs
AS
(
    -- get the first message
    SELECT  0 as MessageLevel,
            a.[MessageID],
            a.[ProfileID],
            a.[FromProfileID],
            a.[FromUsername],
            b.[UserName] AS SentToUserName,
            a.[Subject],
            a.[Body],
            convert(varchar, a.[MsgDate], 107) AS zdate,
            a.[MsgDate],
            a.[MsgRead],
            a.[MsgReplied],
            a.[MsgNew],
            a.[ReplyOfMessageID],
            a.[InitialMessageID]
    FROM
            [tbl_Messages] a
      INNER JOIN
            [tbl_Profile] b
            ON
                  a.[ProfileID] = b.[ProfileID]
    WHERE
            a.[FromProfileID] = @FromProfileID
    
    -- recursive CTE portion        
    UNION ALL 
    
    SELECT  m.MessageLevel+1,
            a.[MessageID],
            a.[ProfileID],
            a.[FromProfileID],
            a.[FromUsername],
            b.[UserName] AS SentToUserName,
            a.[Subject],
            a.[Body],
            convert(varchar, a.[MsgDate], 107) AS zdate,
            a.[MsgDate],
            a.[MsgRead],
            a.[MsgReplied],
            a.[MsgNew],
            a.[ReplyOfMessageID],
            a.[InitialMessageID]
    FROM
            [tbl_Messages] a
      INNER JOIN
            [tbl_Profile] b
            ON
                  a.[ProfileID] = b.[ProfileID]
      INNER JOIN 
            msgs m
            ON
                  a.[ReplyOfMessageID] = m.[MessageID]
                  OR a.[InitialMessageID] = m.[MessageID] -- may not need this
) 
SELECT *
FROM msgs
ORDER BY MessageLevel, [MsgDate] DESC
Random Solutions  
 
programming4us programming4us