0

我需要编写一个程序来检查在数据出错后是否有员工出现在其经理中。我创建了该过程,但出现与 nr 相关的错误(关键字 IF 附近的语法不正确)。为什么会出现,我该如何纠正。我是使用 SQL Server 的初学者,因此欢迎任何帮助。

这是程序:

CREATE PROCEDURE IsManager( @Id INT)
 as
    WITH Managers
    AS (
        SELECT id, id_manager 1 AS LEVELC
        FROM [DBO].[employees]
        WHERE id = @Id
        UNION ALL
        SELECT P.id, P.id_manager H.LEVELC+1
        FROM  [DBO].[employees] E
        inner join Ancestors H
            ON E.id=H.id_manager
   )    
    SELECT COUNT(*) AS nr FROM (SELECT * FROM Managers where Managers.id=@Id
    IF nr=0
    PRINT 'Error '
    ELSE
    PRINT 'Your database has no errors'
4

1 回答 1

2

试试看

CREATE PROCEDURE IsManager( @Id INT)
 as
    declare @nr int

    WITH Managers(id, id_manager, levelc)
    AS (
        SELECT id, id_manager, 1 AS LEVELC
        FROM [DBO].[employees]
        WHERE id = @Id
        UNION ALL
        SELECT P.id, P.id_manager, H.LEVELC+1
        FROM  [DBO].[employees] E
        inner join Ancestors H
            ON E.id=H.id_manager
   )    

  SELECT @nr = COUNT(*) FROM Managers where Managers.id=@Id
  IF @nr=0
    PRINT 'Error '
  ELSE
    PRINT 'Your database has no errors'
于 2013-10-27T21:20:39.723 回答