;with CTEima(PersonId,IsEmployeeActive)
as
(select count(*)
from custom.viwSSAppsEmpMasterExtended vem
where vem.SupervisorPersonId = @p_PersonId
union all
select CTEima.IsEmployeeActive
from Custom.viwSSAppsEmpMasterExtended vem
join CTEima on CTEima.PersonId = vem.SupervisorPersonId
)
set @v_IsManager = (select count(*)from CTEima where IsEmployeeActive = 'Y')
here i am getting error like Incorrect syntax near the keyword 'set'
tell me how to set values from CTE into variable
4条答案
按热度按时间jc3wubiy1#
You can not set values with the
SET
keyword in theSELECT
statement. You can either assign the fields from the query to variables in theSELECT
statement:In this case all fields in the
SELECT
list should be assigned to a variable!Or you can assign a single row-single column
SELECT
statement's result to a variable by theSET
keyword:You can not mix the above options.
Furthermore, CTE is defined within the execution scope of a single
SELECT
,INSERT
,UPDATE
, orDELETE
statement. ( http://msdn.microsoft.com/en-us/library/ms175972.aspx ).SET
is not aSELECT
/INSERT
/UPDATE
/DELETE
statement, this is why SQL Server reports a syntax error (CTEs can not be defined in the scope of the SET statement.)The solution with your example query
9rbhqvlz2#
Replace your last line with this:
xesrikrc3#
1aaf6o9v4#