求救!!!存储过程出错了

今天上午写了一个存储过程,总是报:ELSE附近有语法错误,如下红色字体区域,ELSE与IF的区别只是WHERE里改变了一个参数,各位高手指点一下吧...
CREATE PROCEDURE [TermGetDepartmentList]
   @CompanyID char(36)='',
   @BigDepartID char(36)='',
   @DepartID char(36)=''
AS
DECLARE @T TABLE([ID] char(36),DisplayName varchar(96),[Level] int,Sort varchar(500))
DECLARE @Level int
DECLARE @DepID char(36)
SET @DepID=''
SET @Level=0
IF @CompanyID!=''
    SET @DepID=@CompanyID
    SET @Level=1
IF @BigdepartID!=''
    SET @DepID=@BigDepartID
    SET @Level=2
IF @DepartID!=''
    SET @DepID=@DepartID
    SET @Level=3
IF (@DepID='')
        INSERT INTO @T
        SELECT [ID],DisplayName,@Level,[ID] FROM [部门] WHERE ParentID = [ID]
        WHILE @@ROWCOUNT>0
        BEGIN
         SET @Level = @Level + 1
         INSERT INTO @T
         SELECT A.[ID],A.DisplayName,@Level,(B.Sort + A.[ID]) AS SORT FROM [部门] A INNER JOIN @T B ON A.ParentID = B.[ID]
         WHERE A.[ID] != A.ParentID AND B.[Level] = @Level - 1
        END

ELSE
        INSERT INTO @T
        SELECT [ID],DisplayName,@Level,[ID] FROM [部门] WHERE ParentID
=@DepID
        WHILE @@ROWCOUNT>0
        BEGIN
  SET @Level = @Level + 1
  INSERT INTO @T
  SELECT A.[ID],A.DisplayName,@Level,(B.Sort + A.[ID]) AS SORT FROM [部门] A INNER JOIN @T B ON A.ParentID = B.[ID]
  WHERE A.[ID] != A.ParentID AND B.[Level] = @Level - 1
        END

SELECT [ID],DisplayName FROM @T ORDER BY Sort
GO

转载于:https://www.cnblogs.com/Bigkangaroo/articles/813620.html

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值