Problem: delete the post and the whole sub tree. (20 minutes)
Condition:
Supposing we have a web forum system, in which there are numbers of topics. Every topic has one and only one root post. Every post (either root post or intermediate post) has numbers of children posts, unless it\'s a leaf post. In short, you could regard a forum as a forest: A topic is a tree; a post is a tree node.
Posts are stored in the tbl_Posts table in SQL Server 2000. Definition of tbl_Posts is:
CREATE TABLE [dbo].[tbl_Posts] (
[PostID] [int] NOT NULL ,
[ParentID] [int] NOT NULL ,
[TopicID] [int] NOT NULL ,
[Su**ect] [nvarchar] (50) NOT NULL ,
[Body] [nvarchar] (1000) NOT NULL
)
ALTER TABLE [dbo].[tbl_Posts] WITH NOCHECK ADD
CONSTRAINT [PK_tbl_Posts] PRIMARY KEY CLUSTERED
(
[PostID]
)
CREATE INDEX [IX_tbl_Posts] ON [dbo].[tbl_Posts]([ParentID])
ALTER TABLE [dbo].[tbl_Posts] ADD
CONSTRAINT [FK_tbl_Posts_tbl_Posts_1] FOREIGN KEY
(
[TopicID]
) REFERENCES [dbo].[tbl_Posts] (
[PostID]
),
CONSTRAINT [FK_tbl_Posts_tbl_Posts_2] FOREIGN KEY
(
[ParentID]
) REFERENCES [dbo].[tbl_Posts] (
[PostID]
)
Posts are sorted by PostID in the table. PostID is unique. All the posts in the same topic have the same TopicID, which equal to the root post\'s PostID. Root post\'s ParentID is equal to it\'s PostID. There is no limitation to depth of post tree.
The date in tbl_Posts might be:
PostID ParentID TopicID
1 1 1
2 1 1
3 3 3
4 1 1
5 1 1
6 6 6
7 4 1
8 4 1
9 1 1
10 3 3
11 4 1
12 3 3
13 12 3
14 6 6
15 6 6
16 9 1
17 8 1
Question:
Please write a SQL stored procedure \"DeletePost\", which takes a PostID as input parameter and can delete all the posts (include itself) under the post PostID. Note:
using temp table, table variable or any recursive algorithm won\'t score. Cursor is not encouraged.
For example, \"exec DeletePost 4\" will change the forum as:

-------------------------------------------------------------------------------------------