日期:2014-05-18 浏览次数:20590 次
IF OBJECT_ID('TreeNode') IS NOT NULL DROP TABLE TreeNode GO CREATE TABLE TREENODE ( ID INT NOT NULL UNIQUE ,VAL INT ,LID INT ,RID INT ) /* 10 / \ 6 14 / \ / \ 4 8 12 16 */ INSERT INTO TREENODE SELECT 10,10,6,14 UNION ALL SELECT 6,6,4,8 UNION ALL SELECT 14,14,12,16 UNION ALL SELECT 4,4,NULL,NULL UNION ALL SELECT 8,8,NULL,NULL UNION ALL SELECT 12,12,NULL,NULL UNION ALL SELECT 16,16,NULL,NULL SELECT * FROM TREENODE /* ID VAL LID RID ----------- ----------- ----------- ----------- 10 10 6 14 6 6 4 8 14 14 12 16 4 4 NULL NULL 8 8 NULL NULL 12 12 NULL NULL 16 16 NULL NULL */ UPDATE T1 SET LID=(SELECT TOP 1 ID FROM TREENODE T2 WHERE T2.VAL<T1.VAL ORDER BY VAL DESC) ,RID=(SELECT TOP 1 ID FROM TREENODE T2 WHERE T2.VAL>T1.VAL ORDER BY VAL ASC) FROM TREENODE T1 SELECT * FROM TREENODE ORDER BY ID /* ID VAL LID RID ----------- ----------- ----------- ----------- 4 4 NULL 6 6 6 4 8 8 8 6 10 10 10 8 12 12 12 10 14 14 14 12 16 16 16 14 NULL */
IF OBJECT_ID('MYSTACK') IS NOT NULL DROP TABLE MYSTACK GO CREATE TABLE MYSTACK( ID INT IDENTITY(1,1) ,VAL INT ,MINVAL INT ,PRIMARY KEY( ID DESC ) ) GO IF OBJECT_ID('MYPUSH') IS NOT NULL DROP PROCEDURE MYPUSH GO CREATE PROCEDURE MYPUSH(@VAL INT) AS BEGIN DECLARE @MINVAL INT SELECT TOP 1 @MINVAL=VAL FROM MYSTACK INSERT INTO MYSTACK(VAL,MINVAL) SELECT @VAL,CASE WHEN @VAL<@MINVAL OR @MINVAL IS NULL THEN @VAL ELSE @MINVAL END END GO IF OBJECT_ID('MYPOP') IS NOT NULL DROP PROCEDURE MYPOP GO CREATE PROCEDURE MYPOP AS BEGIN DECLARE @NOWID INT,@NOWVAL INT SELECT TOP 1 @NOWID=ID,@NOWVAL=VAL FROM MYSTACK DELETE FROM MYSTACK WHERE ID=@NOWID SELECT @NOWVAL END GO IF OBJECT_ID('MYMIN') IS NOT NULL DROP PROCEDURE MYMIN GO CREATE PROCEDURE MYMIN AS BEGIN SELECT TOP 1 MINVAL FROM MYSTACK END GO MYPUSH 7 GO MYPUSH 4 GO MYPUSH 5 GO SELECT * FROM MYSTACK GO MYMIN GO MYPOP GO MYPOP GO MYMIN GO
------解决方案--------------------
xue xi
------解决方案--------------------
顶楼上····
------解决方案--------------------
学习.
------解决方案--------------------
学习了!