登陆论坛 找回密码 立刻获得20pp! 地图 帮助
宽带山生活
版主:saliven 麦克 银河 

<< [1] >>  [只看楼主]  [加入关注] 点击:492 回复:18 已被1人关注

主题:微软面试题:SQL查询问题,哪位高手有兴趣?

咖啡猫
612 0

来自:上海
注册:2001-10-01
发帖:146+551 
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:


点击查看全图

点击查看全图



-------------------------------------------------------------------------------------------
http://www.lazybones.biz

发表于:2005-10-16 20:44:59
推荐:进口全瓷牙1999元全搞掂! ...楼主...

SdenliWong
153 0

来自:上海
注册:2003-03-26
发帖:47+1593 
downloading



-------------------------------------------------------------------------------------------
[url=http://www.1110.net/down.asp?UserID=426123]全国各类考试复习“视频教学下载网”[/url]

发表于:2005-10-16 20:52:28

细毛副警长
703 0

来自:上海
注册:2001-09-23
发帖:150+4547 
数据结构downloading



-------------------------------------------------------------------------------------------
和谐副警长

发表于:2005-10-16 20:55:40
推荐:迎新年牙齿美容大行动 ...第2楼...

功夫海参
534 0

来自:上海
注册:2002-07-11
发帖:4761+48640 
downloading



-------------------------------------------------------------------------------------------
I Like To Move It Move It~~~
提倡上海方言,光大海派文化
www.iseesh.com
爱看上海

发表于:2005-10-16 20:57:02
推荐:kds红日官网启动啦!!! ...第3楼...

81 0

来自:保密
注册:2005-01-04
发帖:13+177 
貌似用一个递归就OK了downloading



-------------------------------------------------------------------------------------------
我是开水,我找方便面。
你在哪里?再找不到你我就要冷特勒...
变成冰水了。

发表于:2005-10-16 21:06:56

咖啡猫
612 0

来自:上海
注册:2001-10-01
发帖:146+552 
using temp table, table variable or any recursive algorithm won't score. Cursor is not encouraged.downloading



-------------------------------------------------------------------------------------------
http://www.lazybones.biz

发表于:2005-10-16 21:10:00
推荐:KDS红日球迷会2010招募公告 ...第5楼...

喜福成
427 0

来自:上海
注册:2002-10-27
发帖:177+11406 
downloadingdownloadingdownloading



-------------------------------------------------------------------------------------------
这个家伙很懒,什么也没留下......

发表于:2005-10-16 21:10:34

41 0

来自:上海
注册:2005-07-13
发帖:20+126 
从来不做MIS应用, 太低级了



-------------------------------------------------------------------------------------------
这个家伙很懒,什么也没留下......

发表于:2005-10-16 21:11:23

和谐三得利
1131 15

来自:台湾
注册:2001-02-10
发帖:1278+11414 
downloadingIt's so easy, Let him know that SQL Server 2005 will support recursive t-sql seelct statement: WITHdownloading



-------------------------------------------------------------------------------------------
那么不如,让我们用力的拥抱,真心的祝福,然后转过身去,寻找自己的归宿。不管是哪一种朋友,有怎样的结局

发表于:2005-10-16 21:12:24

DWBI
870 0

来自:上海
注册:2001-08-26
发帖:595+10703 
不懂,偶来学习di



-------------------------------------------------------------------------------------------
Data Warehouse and Business Intelligence
--load the whole world into one warehouse
and mine it

发表于:2005-10-16 21:13:56

SdenliWong
153 0

来自:上海
注册:2003-03-26
发帖:47+1597 
哈刚白刚



-------------------------------------------------------------------------------------------
[url=http://www.1110.net/down.asp?UserID=426123]全国各类考试复习“视频教学下载网”[/url]

发表于:2005-10-16 21:14:04

咖啡猫
612 0

来自:上海
注册:2001-10-01
发帖:146+553 
8楼赖节皮downloading



-------------------------------------------------------------------------------------------
http://www.lazybones.biz

发表于:2005-10-16 21:16:14
推荐:长期更新,从懂事到现在 ...第11楼...

胜利日
408 0

来自:保密
注册:2004-06-16
发帖:172+4463 
偶4年前的写得一个论坛就是这样的结构,
微软的题目真火星downloading



-------------------------------------------------------------------------------------------
不自由,吾宁死

发表于:2005-10-16 21:19:07

咖啡猫
612 0

来自:上海
注册:2001-10-01
发帖:146+554 
这种tree在table中的表示很常见的,这个问题考的是如何不用递归、临时表来查询



-------------------------------------------------------------------------------------------
http://www.lazybones.biz

发表于:2005-10-16 21:21:40
推荐:科帕奇SUV俱乐部版主招募 ...第13楼...

和谐三得利
1131 15

来自:台湾
注册:2001-02-10
发帖:1279+11419 
13楼正街,等我有空来想一个答案好了~~downloading



-------------------------------------------------------------------------------------------
那么不如,让我们用力的拥抱,真心的祝福,然后转过身去,寻找自己的归宿。不管是哪一种朋友,有怎样的结局

发表于:2005-10-16 21:36:52

Baggio
790 0

来自:上海
注册:2001-07-06
发帖:93+1678 
downloading



-------------------------------------------------------------------------------------------
我想说很多,
但是现在的我什么也不想说了……

发表于:2005-10-16 21:44:36
推荐:S*H*E你是卖艺还是卖色! ...第15楼...

天上的白云
57 11

来自:上海
注册:2003-08-30
发帖:117+1385 
downloading



-------------------------------------------------------------------------------------------
&amp;nbsp;我不是一个随便的人,但我随便起来就不是人

发表于:2006-08-20 23:28:06
推荐:打造KDS最美贴,刹那芳华 ...第16楼...

暴利TT小熊
118 35

来自:保密
注册:2005-12-24
发帖:84+1753 
downloadingdownloading高手么兴趣



-------------------------------------------------------------------------------------------
No reason,no result, just now~.....

发表于:2006-08-20 23:23:33

回复主题 [ 返回宽带山KDS ]
主题: 微软面试题:SQL查询问题,哪位高手有兴趣?
手机看宽带山

关于我们 - 联系我们 - 加入我们 - 营销服务 - 友情链接 - 频道合作- PChome Widget
Copyright © 1996-2009 PChome.net All rights reserved. 电脑之家 版权所有