变量的时间 Table 结果不起作用
Temporal Table result to variable doesn't work
我正在尝试将时间 table 的结果设置为执行此操作的变量我做错了什么??
WITH Consolidado_B (CANTIDAD,CALIDAD) AS(
select
SUM(cbs) [Cantidad],
'BLANCOS' CALIDAD
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%blanco%' and
c.colorcodi=@COLORCODI
union
select
SUM(cbs),
'ACCENT'
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%Acce%'
and c.colorcodi=@COLORCODI
)
SET @TOTALBASES=
(
SELECT sum(CANTIDAD) TOTAL
FROM Consolidado_B
)
既然你没有提到它,我猜错误信息是:子查询返回了超过 1 个值。或者 Consolidado_B 未定义。
CTE需要和select语句在一起,不能和SET语句分开
试试这个:
WITH Consolidado_B (CANTIDAD,CALIDAD) AS(
select
SUM(cbs) [Cantidad],
'BLANCOS' CALIDAD
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%blanco%' and
c.colorcodi=@COLORCODI
union
select
SUM(cbs),
'ACCENT'
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%Acce%'
and c.colorcodi=@COLORCODI
)
SELECT @TOTALBASES= sum(CANTIDAD)
FROM Consolidado_B
使用可以只用一个SELECT
。但这会更简单:
select @TOTALBASES = SUM(cbs)
from CBases c join
v_Bases v
on v.CODIGO=c.basecodi
where c.colorcodi = @COLORCODI or
(v.[NOMBRE COMPLETO] like '%blanco%' or v.[NOMBRE COMPLETO] like '%Acce%') ;
如果 [NOMBRE COMPLETO]
可以同时满足这两个条件,则这不是 100% 等价的——而您实际上希望将这些计算两次。但是,它的效率要高得多。如果是这种情况,可以调整查询来处理这个问题——而不会使查询复杂化。
我正在尝试将时间 table 的结果设置为执行此操作的变量我做错了什么??
WITH Consolidado_B (CANTIDAD,CALIDAD) AS(
select
SUM(cbs) [Cantidad],
'BLANCOS' CALIDAD
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%blanco%' and
c.colorcodi=@COLORCODI
union
select
SUM(cbs),
'ACCENT'
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%Acce%'
and c.colorcodi=@COLORCODI
)
SET @TOTALBASES=
(
SELECT sum(CANTIDAD) TOTAL
FROM Consolidado_B
)
既然你没有提到它,我猜错误信息是:子查询返回了超过 1 个值。或者 Consolidado_B 未定义。
CTE需要和select语句在一起,不能和SET语句分开
试试这个:
WITH Consolidado_B (CANTIDAD,CALIDAD) AS(
select
SUM(cbs) [Cantidad],
'BLANCOS' CALIDAD
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%blanco%' and
c.colorcodi=@COLORCODI
union
select
SUM(cbs),
'ACCENT'
from CBases c
inner join v_Bases v on v.CODIGO=c.basecodi
where v.[NOMBRE COMPLETO] like '%Acce%'
and c.colorcodi=@COLORCODI
)
SELECT @TOTALBASES= sum(CANTIDAD)
FROM Consolidado_B
使用可以只用一个SELECT
。但这会更简单:
select @TOTALBASES = SUM(cbs)
from CBases c join
v_Bases v
on v.CODIGO=c.basecodi
where c.colorcodi = @COLORCODI or
(v.[NOMBRE COMPLETO] like '%blanco%' or v.[NOMBRE COMPLETO] like '%Acce%') ;
如果 [NOMBRE COMPLETO]
可以同时满足这两个条件,则这不是 100% 等价的——而您实际上希望将这些计算两次。但是,它的效率要高得多。如果是这种情况,可以调整查询来处理这个问题——而不会使查询复杂化。