我需要编写一个程序,检查数据错误后,员工是否出现在其经理之间。我创建了该过程但是我有一个与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'
答案 0 :(得分: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'