我有一個包含 8 條關于員工的記錄的資料庫,它保存了Ssn
每個員工的 id 和Super_ssn
參考其經理的 id。下面是創建表的代碼:
Create Table t_employee
(
Ssn int not null,
Super_ssn int,
FirstName varchar(50),
LastName varchar(50),
NationalCode varchar(50),
_role varchar(50),
Primary key(Ssn),
Foreign Key(Super_ssn) references t_employee(Ssn)
);
這個資料庫形成了一種樹,條件是每個員工只能訪問其下屬的資訊,但如果他的欄位等于HRM,那么他可以訪問所有員工的資訊。
我必須寫一個只有一個引數的procedure
/ ,然后回傳他所有下屬的資訊。function
@employeeId
我已經撰寫了以下程式,但它顯示
結尾附近的語法不正確
Create Procedure returnAllChildren (@employeeId int)
as
Begin
Declare @empRole nvarchar(50)
Set @empRole = (Select _role From t_employee where Ssn = @employeeId)
if @empRole = 'HRM'
Begin
Select * From t_employee
End
Else
Begin
With accessed_employees (Ssn, FirstName, LastName, Super_ssn, NationalCode, _role, _level) as
(
Select emp.Ssn,
emp.FirstName,
emp.LastName,
emp.Super_ssn,
emp.NationalCode,
emp._role,
0 as _level
From t_employee AS emp
Where emp.Ssn = @employeeId
Union ALL
Select _emp.Ssn,
_emp.FirstName,
_emp.LastName,
_emp.Super_ssn,
_emp.NationalCode,
_emp._role,
_emp._level 1
From t_employee _emp
Join accessed_employees a
on _emp.Super_ssn = a.Ssn
)
End -- Here is the problem
End
我不知道為什么會這樣。所有的Begin ... End
都是配對的,我無法理解它的原因。
對于如何解決此錯誤或實施此問題的其他方法,我將不勝感激。
uj5u.com熱心網友回復:
您已使用 CTE 定義With accessed_employees (...) as (...)
但未對其執行任何操作,因此查詢不完整。例如,如果您正在查找SELECT
來自 的資料accessed_employees
,那么您需要執行以下操作:
With accessed_employees (...) as (...) SELECT * FROM accessed_employees;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486991.html
上一篇:(劇作家異步)如何修復:'AttributeError:'coroutine'物件沒有屬性'inner_text''