Question : Need to setup this stored procedure.

Create PROCEDURE [dbo].[prc_Messages_Response_Select_All]
      @FromProfileID INT
;WITH msgs
AS
BEGIN
(
    -- 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

END
Msg 102, Level 15, State 1, Procedure prc_Messages_Response_Select_All, Line 3
Incorrect syntax near ';'.
Msg 156, Level 15, State 1, Procedure prc_Messages_Response_Select_All, Line 5
Incorrect syntax near the keyword 'BEGIN'.

Answer : Need to setup this stored procedure.

You have to remember that the base structure of the stored procedure is:

create procedure proc_name
   @parameter1[, @parameter2[, @parameterN]]
as
[begin]
-- procedure body here
[end]

To help keep things straight, I would always start with that and I would include the optional begin and end just so it is clear where your procedure body starts.

The WITH statement in the query I gave you is a common table expression and it takes the form:

WITH cte_name AS ( {select query})

Previous statements have to be terminated before the WITH, so you will often see this as ;WITH to ensure that other statements are closed first.

Your error above is due to you confusing the AS of WITH to the AS of procedure and so inserted BEGIN in the wrong place. Try the corrected code below.

Hope that helps!
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:
62:
63:
64:
65:
66:
67:
68:
69:
70:
CREATE PROCEDURE [dbo].[prc_Messages_Response_Select_All]
      @FromProfileID INT
AS
BEGIN

SET NOCOUNT ON;

;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
;

END
Random Solutions  
 
programming4us programming4us