经典存储过程(01)--树状信息更新

1. DictComp表的字段情况如下:




2.存储过程内容
/*------------------------------------------------------------------------------------------------------------------------------
本过程用于"树状信息更新完成后台更新Path"
传入参数
 @pCompId
   树节点ID
传出参数
 @pResult
   =1 表示确认成功
   =-1 则表示已经确认
   =-2 表示确认时出错
 @pOutStr  返回提示信息
CreateDate :2005-08-15 16:26:20 by WUJINSHU
LastUpdate :2005-08-15 17:49:20 by WUJINSHU

delete from dictcomp where compid=0
------------------------------------------------------------------------------------------------------------------------------*/
CREATE Procedure ProUpdateCompPath
(
 ---- 输入参数 ---- 
 @pCompId  int
) AS

---- 关闭计数器 ----
--SET NOCOUNT ON
Declare
 @pResult  int  ,
 @pOutStr  varchar(50) 

Declare
 @ErrorInt   int    ,
 @ErrorStr   varchar(200) ,

 @CompId   int    ,
 @CompName   varchar(100) ,
 @ParentId   int    ,
 @HasChild   int    ,
 @IdPath   varchar(500) ,
 @NamePath   varchar(1000) ,
 @LevelIndex   int    ,

 @ParentIdPath  varchar(500) ,
 @ParentNamePath  varchar(1000) ,
 @my_lakes_cursor Cursor
 
--Select @pCompId = 0
 
--定义游标
Declare C_DataCursor
Cursor LOCAL For
 Select  
  CompId   ,
  CompName  ,
  ParentId  ,
  HasChild  ,
  IdPath   ,
  NamePath  ,
  LevelIndex
 From
  DictComp A
 Where  
 --  A.CompId = @pCompId
 -- Or A.ParentId = @pCompId
  A.ParentId = @pCompId
 Order By
  A.ParentId  
For Read Only

Select @ErrorInt=0

if @pCompId=0
begin
 Update
  DictComp
 Set
  IdPath  = Cast(CompId As Varchar(20)) ,
  NamePath = CompName      ,
  LevelIndex = 0
 Where
  ParentId = 0
end
else begin
 if Exists(Select CompId From DictComp Where ParentId=@pCompId And CompId<>0)
  Set @HasChild = 1
 else
  Set @HasChild = 0

 Select
  @ParentIdPath  = IsNull(B.IdPath ,'') ,
  @ParentNamePath = IsNull(B.NamePath,'')  ,
  @LevelIndex  =  IsNull(B.LevelIndex,0) ,
  @ParentId  = A.ParentId
 From
  DictComp A Left Join DictComp B On A.ParentId=B.CompId
 Where
  A.CompId  = @pCompId

 if @ParentId=0
 begin
  Set @LevelIndex = @LevelIndex + 1
 end
 else begin
  Set @LevelIndex = @LevelIndex + 1
 end

 Update
  DictComp
 Set
  IdPath  = @ParentIdPath + '/' + Cast(CompId As Varchar(20)) ,
  NamePath = @ParentNamePath + '/' + CompName,
  HasChild = @HasChild,
  LevelIndex = @LevelIndex
 Where
  CompId  = @pCompId
end

--打开游标
Open C_DataCursor


--获取数据
Fetch C_DataCursor
 Into 
  @CompId  ,
  @CompName  ,
  @ParentId  ,
  @HasChild  ,
  @IdPath  ,
  @NamePath  ,
  @LevelIndex

While (@@Fetch_Status=0)
begin
 --Print @CompId
 --Print @CompName
 --Print @ParentId
 --Print '------------------------------------------'
 
 if Exists(Select CompId From DictComp Where ParentId=@CompId And CompId<>0)
  Set @HasChild = 1
 else
  Set @HasChild = 0

 Update
  DictComp
 Set
  HasChild = @HasChild
 Where
  CompId  = @CompId

 Select
  @ParentIdPath  = IsNull(B.IdPath ,'') ,
  @ParentNamePath = IsNull(B.NamePath,'')
 From
  DictComp A Left Join DictComp B On A.ParentId=B.CompId
 Where
  A.CompId  = @CompId
 
 if @ParentId=0
 begin
  Set @LevelIndex = @LevelIndex + 1
 end
 else begin
  Set @LevelIndex = @LevelIndex + 1
 end

 Update
  DictComp
 Set
  IdPath  = @ParentIdPath + '/' + Cast(CompId As Varchar(20)) ,
  NamePath = @ParentNamePath + '/' + CompName,
  LevelIndex = @LevelIndex
 Where
  ParentId = @CompId
 
 --EXECUTE ProUpdateCompPath @CompId, @pResult output, @pOutStr output
 EXECUTE ProUpdateCompPath @CompId

 --获取数据
 Fetch C_DataCursor
  Into 
   @CompId  ,
   @CompName  ,
   @ParentId  ,
   @HasChild  ,
   @IdPath  ,
   @NamePath  ,
   @LevelIndex
end

--关闭游标
Close   C_DataCursor

--释放游标
DealLocate  C_DataCursor

--判断错误代码,1表示错误,0表示没有发生错误
if @ErrorInt=1
begin
 Select @pResult  =-2
 Select @pOutStr  =@ErrorStr
end
else begin
 --判断是否发生错误
 If @@Error = 0
 begin
  Select @pResult =1
  Select @pOutStr ='成功'
 end
 else begin
  Select @pResult =-2
  Select @pOutStr ='失败'
 end
end

GRANT EXEC ON ProUpdateCompPath TO PUBLIC
GO

posted on 2007-04-03 12:50  天涯人  阅读(648)  评论(1编辑  收藏  举报

导航