Here is my CTE – it is causing an error. Help me to figure out the error
CREATE TABLE [dbo].[myEmpTab](
[EMPNO] [int] NULL,
[ENAME] [varchar](10) NULL,
[JOB] [varchar](20) NULL,
[MGR] [int] NULL,
[HIREDATE] [datetime] NULL,
[SAL] [int] NULL
) ON [PRIMARY]
;
WITH MyCTE AS
(
SELECT EMPNO, EName, Null as MGR, NULL as ManagerName
FROM myEmpTab
WHERE MGR IS NULL
UNION ALL
SELECT EMPNO, EName, MGR, MyCTE.EName
FROM myEmpTab
INNER JOIN MyCTE ON myEmpTab.MGR = MyCTE.EMPNO
WHERE myEmpTab.MGR IS NOT NULL
)
SELECT * FROM MyCTE
when i am executing the sql then i am getting error like
Msg 209, Level 16, State 1, Line 8
Ambiguous column name 'EMPNO'.
Msg 209, Level 16, State 1, Line 8
Ambiguous column name 'EName'.
Msg 209, Level 16, State 1, Line 8
Ambiguous column name 'MGR'.
i am not being able to catch the error . so please anyone have a lot and help me to fix it. thanx.
I don’t think you can reference a CTE inside itself. Try this
OR