sql server 2008 - how to assign cte value to variable -



sql server 2008 - how to assign cte value to variable -

;with cteima(personid,isemployeeactive) (select count(*) custom.viwssappsempmasterextended vem vem.supervisorpersonid = @p_personid union select cteima.isemployeeactive custom.viwssappsempmasterextended vem bring together cteima on cteima.personid = vem.supervisorpersonid ) set @v_ismanager = (select count(*)from cteima isemployeeactive = 'y')

here getting error wrong syntax near keyword 'set'

tell me how set values cte variable

you can not set values set keyword in select statement. either assign fields query variables in select statement:

with cte ( /** .. query here .. **/ ) select @yourvariable = fieldnameorsubquery -- in short: look cte

in case fileds in select list should assigned variable!

or can assign single row-single column select statement's result variable set keyword:

set @yourvariable = (select count(1) yourtable).

you can not mix above options.

furthermore, cte defined within execution scope of single select, insert, update, or delete statement. (http://msdn.microsoft.com/en-us/library/ms175972.aspx). set not select/insert/update/delete statement, why sql server reports syntax error (ctes can not defined in scope of set statement.)

the solution illustration query

;with cteima(personid,isemployeeactive) ( select count(*) custom.viwssappsempmasterextended vem vem.supervisorpersonid = @p_personid union select cteima.isemployeeactive custom.viwssappsempmasterextended vem bring together cteima on cteima.personid = vem.supervisorpersonid ) select @v_ismanager = count(*) cteima isemployeeactive = 'y'

sql-server-2008

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -