过程 超过最大存储过程、函数、触发器或视图嵌套级别(限制 32)

发布于 2024-10-18 15:27:58 字数 2037 浏览 2 评论 0原文

存储过程在以下位置失败,谢谢您的帮助。

--Insert MSOrg Information    

DECLARE @PersonnelNumber int,    
 @MSOrg   varchar(255)    

DECLARE csr CURSOR FAST_FORWARD FOR    
 SELECT PersonnelNumber FROM Person    

OPEN csr    

FETCH NEXT FROM csr     
 INTO @PersonnelNumber    

WHILE @@FETCH_STATUS = 0    
BEGIN    

 EXEC GetMSOrg @PersonnelNumber, @MSOrg out    

 INSERT INTO PersonSubject (    
   PersonnelNumber    
   ,SubjectID    
   ,SubjectValue    
   ,Created  
   ,Updated  
  )    
  SELECT @PersonnelNumber    
   ,SubjectID    
   ,@MSOrg    
   ,getDate()  
   ,getDate()  
  FROM Subject    
  WHERE DisplayName = 'MS Org'    

 FETCH NEXT FROM csr     
  INTO @PersonnelNumber    
END    

CLOSE csr    
DEALLOCATE csr    

下面是存储的 prc 定义 GetMSOrg,在第三个条件下失败

CREATE  PROCEDURE [dbo].[GetMSOrg]
(
    @PersonnelNumber    int
    ,@OrgTerm       varchar(200)    out
)
AS
DECLARE @MDRTermID          int
        ,@ReportsToPersonnelNbr int
--Check to see if we have reached the top of the chart
SELECT  @ReportsToPersonnelNbr = ReportsToPersonnelNbr
FROM        ReportsTo
WHERE   PersonnelNumber = @PersonnelNumber
IF (@ReportsToPersonnelNbr IS NULL) --Reached the Top of the Org Ladder
BEGIN
    SET @OrgTerm = 'Non-standard rollup'
END
ELSE IF (@PersonnelNumber IN (SELECT PersonnelNumber FROM OrgTermMap))
BEGIN
    SELECT  @OrgTerm = s.Term
    FROM    OrgTermMap tm
        JOIN Taxonomy..StaticHierarchy s ON tm.OrgTermID = s.TermID
    WHERE   tm.PersonnelNumber = @PersonnelNumber
END
ELSE
BEGIN
    SELECT  @MDRTermID = tm.OrgTermID
    FROM        ReportsTo r
        JOIN OrgTermMap tm ON r.ReportsToPersonnelNbr = tm.PersonnelNumber
    WHERE   r.PersonnelNumber = @PersonnelNumber
    IF (@MDRTermID IS NULL)
    BEGIN
        EXEC GetMSOrg @ReportsToPersonnelNbr, @OrgTerm out
    END
    ELSE
    BEGIN
        SELECT  @OrgTerm = Term
        FROM    Taxonomy..StaticHierarchy
        WHERE VocabID = 118
            AND TermID = @MDRTermID
    END
END

GO

The stored proc is failing at below location,Thanks, for all your help.

--Insert MSOrg Information    

DECLARE @PersonnelNumber int,    
 @MSOrg   varchar(255)    

DECLARE csr CURSOR FAST_FORWARD FOR    
 SELECT PersonnelNumber FROM Person    

OPEN csr    

FETCH NEXT FROM csr     
 INTO @PersonnelNumber    

WHILE @@FETCH_STATUS = 0    
BEGIN    

 EXEC GetMSOrg @PersonnelNumber, @MSOrg out    

 INSERT INTO PersonSubject (    
   PersonnelNumber    
   ,SubjectID    
   ,SubjectValue    
   ,Created  
   ,Updated  
  )    
  SELECT @PersonnelNumber    
   ,SubjectID    
   ,@MSOrg    
   ,getDate()  
   ,getDate()  
  FROM Subject    
  WHERE DisplayName = 'MS Org'    

 FETCH NEXT FROM csr     
  INTO @PersonnelNumber    
END    

CLOSE csr    
DEALLOCATE csr    

Below is the stored prc defination GetMSOrg and fails at third condition

CREATE  PROCEDURE [dbo].[GetMSOrg]
(
    @PersonnelNumber    int
    ,@OrgTerm       varchar(200)    out
)
AS
DECLARE @MDRTermID          int
        ,@ReportsToPersonnelNbr int
--Check to see if we have reached the top of the chart
SELECT  @ReportsToPersonnelNbr = ReportsToPersonnelNbr
FROM        ReportsTo
WHERE   PersonnelNumber = @PersonnelNumber
IF (@ReportsToPersonnelNbr IS NULL) --Reached the Top of the Org Ladder
BEGIN
    SET @OrgTerm = 'Non-standard rollup'
END
ELSE IF (@PersonnelNumber IN (SELECT PersonnelNumber FROM OrgTermMap))
BEGIN
    SELECT  @OrgTerm = s.Term
    FROM    OrgTermMap tm
        JOIN Taxonomy..StaticHierarchy s ON tm.OrgTermID = s.TermID
    WHERE   tm.PersonnelNumber = @PersonnelNumber
END
ELSE
BEGIN
    SELECT  @MDRTermID = tm.OrgTermID
    FROM        ReportsTo r
        JOIN OrgTermMap tm ON r.ReportsToPersonnelNbr = tm.PersonnelNumber
    WHERE   r.PersonnelNumber = @PersonnelNumber
    IF (@MDRTermID IS NULL)
    BEGIN
        EXEC GetMSOrg @ReportsToPersonnelNbr, @OrgTerm out
    END
    ELSE
    BEGIN
        SELECT  @OrgTerm = Term
        FROM    Taxonomy..StaticHierarchy
        WHERE VocabID = 118
            AND TermID = @MDRTermID
    END
END

GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜深人未静 2024-10-25 15:27:58

每次调用 EXEC GetMSOrg 都会沿着查询继续进行,

SELECT  @ReportsToPersonnelNbr = ReportsToPersonnelNbr
FROM        ReportsTo
WHERE   PersonnelNumber = @PersonnelNumber

并且当 PersonnelNumber 不再向任何人报告时,有一个退出子句。

因此,唯一合乎逻辑的结论是,你有一个周期性的人->报告对象->..->人。这甚至可以在单个记录上。

此查询将在报告到记录中查找循环:

declare @PersonnelNumber int
set @PersonnelNumber = 10

;with CTE as (
select 1 Level,
       convert(varchar(max),@PersonnelNumber) Seed,
       '>>' + convert(varchar(max),@PersonnelNumber) + '>>' Path
union all
SELECT Level+1,
       convert(varchar(max),ReportsToPersonnelNbr),
       Path + convert(varchar(max),ReportsToPersonnelNbr) + '>>'
FROM   ReportsTo
join CTE on CTE.Seed = ReportsTo.PersonnelNumber
where  Level < 40
)
select *
from CTE
where Len(Replace(Path, '>' + convert(varchar(max),Seed) + '>', ''))
  = LEN(Path) - Len('>' + convert(varchar(max),Seed) + '>') * 2

Each call to EXEC GetMSOrg keeps progressing along the query

SELECT  @ReportsToPersonnelNbr = ReportsToPersonnelNbr
FROM        ReportsTo
WHERE   PersonnelNumber = @PersonnelNumber

And there is an exit clause when PersonnelNumber no longer reports to anyone.

Therefore the only logical conclusion is that you have a cyclical person->reportsto->..->person. This could even be on a single record.

This query will find loops within the reportsto records:

declare @PersonnelNumber int
set @PersonnelNumber = 10

;with CTE as (
select 1 Level,
       convert(varchar(max),@PersonnelNumber) Seed,
       '>>' + convert(varchar(max),@PersonnelNumber) + '>>' Path
union all
SELECT Level+1,
       convert(varchar(max),ReportsToPersonnelNbr),
       Path + convert(varchar(max),ReportsToPersonnelNbr) + '>>'
FROM   ReportsTo
join CTE on CTE.Seed = ReportsTo.PersonnelNumber
where  Level < 40
)
select *
from CTE
where Len(Replace(Path, '>' + convert(varchar(max),Seed) + '>', ''))
  = LEN(Path) - Len('>' + convert(varchar(max),Seed) + '>') * 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文