嗨,我有以下查询
GO
DECLARE @tempTable TABLE
(
Id INT PRIMARY KEY,
Referencia VARCHAR(15),
UAP NVARCHAR(20),
ConsumoWeek01 FLOAT,
ConsumoWeek02 FLOAT,
Stock INT,
PecasPorCaixa INT
UNIQUE (Id)
)
INSERT INTO
@tempTable
SELECT *
FROM
viewConsumoPDP
SELECT
C.Id,
C.Referencia,
C.UAP,
C.ConsumoWeek01 AS ConsumoInicialWeek01,
T.ConsumoWeek01 AS ConsumoActualWeek01,
T.ConsumoWeek01 / 5 AS ConsumoDiarioWeek01,
T.ConsumoWeek02 AS ConsumoWeek02
FROM
@tempTable T
INNER JOIN Parametros P
ON P.Referencia = T.Referencia
AND P.UAP = T.UAP
INNER JOIN Consumos C
ON C.Referencia = P.Referencia
AND C.UAP = P.UAP
我需要创建一些使用某些列值计算的变量,并且需要使用该变量来计算其他列。
原因是因为我不想对select语句进行所有计算,这会造成很大的混乱,但是我不知道是否可以使用SQL Transact。
例如
GO
DECLARE @tempTable TABLE
(
Id INT PRIMARY KEY,
Referencia VARCHAR(15),
UAP NVARCHAR(20),
ConsumoWeek01 FLOAT,
ConsumoWeek02 FLOAT,
Stock INT,
PecasPorCaixa INT
UNIQUE (Id)
)
DECLARE @NumPAB INT
INSERT INTO
@tempTable
SELECT *
FROM
viewConsumoPDP
SELECT
C.Id,
C.Referencia,
C.UAP,
C.ConsumoWeek01 AS ConsumoInicialWeek01,
T.ConsumoWeek01 AS ConsumoActualWeek01,
T.ConsumoWeek01 / 5 AS ConsumoDiarioWeek01,
T.ConsumoWeek02 AS ConsumoWeek02,
@ConsumoPAB = P.NumPab / T.ConsumoWeek01
FROM
@tempTable T
INNER JOIN Parametros P
ON P.Referencia = T.Referencia
AND P.UAP = T.UAP
INNER JOIN Consumos C
ON C.Referencia = P.Referencia
AND C.UAP = P.UAP
您可以看到我想将计算结果存储在一个变量中,然后在另一个计算列中使用它。但是,如果我无法在select语句中分配值,该如何实现?
答案 0 :(得分:2)
使用SELECT
语句,您可以返回结果集 OR 为变量赋值。您不能同时执行两项操作。
主要原因是两个操作在逻辑上是不同的。一个将处理一组值,而另一个将处理标量值。