ALTER PROCEDURE [dbo].[p_ReturnEmployeeOrg]
( @BusinessEntityID int = 21 ) **-- NOT WORKING**
AS
SELECT * FROM HumanResources.Employee
;WITH cte_Emp (EmployeeID,ManagerID,Title,EmployeeName,ManagerName,EmployeeLevel)
AS
-- Anchor member definition
(SELECT e.BusinessEntityID,e.MaritalStatus,e.JobTitle,
CAST(c.LastName + ',' + c.FirstName AS VARCHAR(200)),
CAST ('N/A' AS VARCHAR(200)),
CAST (0 AS INT)
FROM HumanResources.Employee e
INNER JOIN Person.Person c
ON e.BusinessEntityID = c.BusinessEntityID
WHERE e.BusinessEntityID = @BusinessEntityID
UNION ALL
-- Recursive member definition
SELECT e.BusinessEntityID,e.MaritalStatus,e.JobTitle,
CAST(c.LastName + ',' + c.FirstName AS VARCHAR(200)),
CAST ('N/A' AS VARCHAR(200)),
CAST (0 AS INT)
FROM HumanResources.Employee e
INNER JOIN Person.Person c
ON e.BusinessEntityID = c.BusinessEntityID)
ORDER BY EmployeeLevel - **NOT WORKING**
EXEC p_ReturnEmployeeOrg 21)
-- exec不工作,结果是原始数据而不是 BusinessEntityID = 21