Hello, I'm trying to save the value of a common table expressions (CTE) in a declared variable.
This is all I have.
DECLARE @valor decimal(12,0)
with xc as (
select v.productoid, p.nombre, eq.unidadid, eq.FactorConversion, sum(v.cantidad) as cantidad
from ventas as v
inner join productos as p on p.id = v.productoid
left join unidades as u on u.id = p.unidadid
left join equivalencias as eq on eq.unidadid = v.unidadid
where v.productoid = 1270
group by v.productoid, p.nombre, eq.unidadid, eq.FactorConversion
) select ( cantidad * FactorConversion ) as Cantidad
from xc inner join (
select p.id, p.nombre, p.pesomililitros from productos as p where p.id = 1270
) as x1
on x1.id = productoid
select @valor = Cantidad from xc
When I execute it, it tells me this:
(1 row(s) affected)
Msg 208, Level 16, State 1, Line 17
Invalid object name 'xc'.
It does not seem to recognize the xc
object.
The value that the CTE obtains is
700.000
That value I want to save it in my variable @valor
if I just execute .. this ..
with xc as (
select v.productoid, p.nombre, eq.unidadid, eq.FactorConversion, sum(v.cantidad) as cantidad
from ventas as v
inner join productos as p on p.id = v.productoid
left join unidades as u on u.id = p.unidadid
left join equivalencias as eq on eq.unidadid = v.unidadid
where v.productoid = 1270
group by v.productoid, p.nombre, eq.unidadid, eq.FactorConversion
) select ( cantidad * FactorConversion ) as Cantidad
from xc inner join (
select p.id, p.nombre, p.pesomililitros from productos as p where p.id = 1270
) as x1
on x1.id = productoid
I hope someone will guide me. thanks