CREATEPROCEDURE[add]@keyidint,@messagevarchar(50)OUTPUT―――keyid为复兴的贴子id号,假如是新贴则为0,@message为堕落信息
AS
IF(@keyid=0)
INSERTINTOforum(rootid,deep,ordernum,……)values(0,0,0,……)
ELSE
BEGIN
DECLARE@rootidint,@idint,@deepint,@begnumfloat,@endnumfloat,@ordernumfloat
SELECT@rootid=0,@id=0,@deep=0,@begnum=0,@endnum=0,@ordernum=0
SELECT@rootid=rootid,@id=id,@begnum=ordernum,@deep=deepfromforumwhereid=@keyid
IF(@id=0)
BEGIN
SELECT@message=要复兴的贴子已被删除!
return
END
ELSE
BEGIN
IF(@rootid=0)SELECT@rootid=@id――复兴的是根贴,取其id为新加贴的rootid
SELECT@endnum=ordernumwhererootid=@rootidandordernum>@begnumorderbyordernum
IF(@endnum=0)
SELECT@ordernum=@begnum+65536――复兴的是最初一贴,能够在此限定@ordernum的局限以防溢出
ELSE
**BEGIN
**IF@endnum-@begnum>1――精度仍能分辩。此处的1为精度标志,合适于基数字段为int,假如基数字段为numeric字段,请酌情选娶(呸呸呸,错别字来了),目标是使基数精渡过小时限定深度增添,制止显现时的杂乱
**SELECT@ordernum=(@begnum+@endnum)/2,@deep=@deep+1――关头,取排序基数中值
**ELSE
**SELECT@ordernum=@begnum――限定深度不克不及再增添,此贴与复兴贴平行显现,假如存在parentid字段,则要取parentid和复兴贴的parentid一样
**END
**INSERTintoforum(rootid,deep,ordernum,……)values(@rootid,@deep,@ordernum,……)
END
END
Select@message=乐成
return
剪枝存储历程改成:
CREATEPROCEDURE[del]@keyidint,@messagevarchar(50)OUTPUT―――keyid为要删除的贴子id号,假如是新贴则为0,@message为堕落信息
AS
DECLARE@rootidint,@idint,@deepint,@begnumfloat,@endnumfloat
SELECT@rootid=0,@deep=0,@begnum=0,@endnum=0,@id=0
SELECT@id=id,@begnum=ordernum,@rootid=rootid,@deep=deepfromforumwhereid=@keyid
IF(@id=0)
BEGIN
SELECT@message=该贴子不存在!"
return
END
ELSE
BEGIN
SELECT@endnum=ordernumfromforumwhererootid=@rootidanddeep<=@deepandordernum>@begnumorderbyordernum
IF(@endnum=0)――要删除的是最初一个子枝或是根贴
DELETEFROMforumwhereordernum>=@begnumand(rootid=@rootidorid=@rootid)
ELSE
**BEGIN
**IF@begnum=@endnum
**DELETEFROMforumwhereid=@idand(rootid=@rootidorid=@rootid)――已受精度限定的枝,只删以后贴
**ELSE
**DELETEFROMforumwhereordernum>=@begnumandordernum<@endnumand(rootid=@rootidorid=@rootid)
**END
END