Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign cte value to variable

;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

like image 267
taj Avatar asked Nov 13 '14 11:11

taj


People also ask

Can we declare variable in CTE SQL Server?

DECLARE is a statement. A CTE is an expression. In SQL, expressions are contained within statements, and never the other way around. Unless you are trying to make a view, you should be able to just put your DECLARE statements before the statement that contains your CTE's.

Can you update values in a CTE?

If your CTE is based on a single table then you can update using CTE, which in turn updates the underlying table.

Can we index CTE?

You cannot index a CTE, but the approach is that the CTE can make use of the underlying indexes. In the above query, a JOIN b cannot make use of an index on t.

Can we insert data using CTE?

You can also use CTE to insert data into the SQL table. The CTE query definition includes the required data that you can fetch from existing tables using joins. Later, query CTE for inserting data into the target table.


2 Answers

You can not set values with the SET keyword in the SELECT statement. You could either assign the fields from the query to variables in the SELECT statement:

WITH CTE AS (
  /** .. Your Query Here .. **/
)
SELECT
  @YourVariable = FieldNameOrSubquery -- In short: Expression
FROM
  CTE

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 the SET keyword:

SET @YourVariable = (SELECT COUNT(1) FROM YourTable).

You can not mix the above options.

Furthermore, CTE is defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement. (http://msdn.microsoft.com/en-us/library/ms175972.aspx). SET is not a SELECT/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

;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
)
SELECT @v_IsManager = COUNT(*)
FROM CTEima
WHERE IsEmployeeActive = 'Y'
like image 66
Pred Avatar answered Oct 02 '22 17:10

Pred


Replace your last line with this:

select @v_IsManager = count(*) from CTEima where IsEmployeeActive = 'Y'
like image 22
Dan Avatar answered Oct 02 '22 16:10

Dan