我想创建一个查询,该查询在由某些变量定义的时间范围内计算一些指标,但是我不确定是否可以按照我的意图使用变量。
我收到以下错误:
第137条消息,第2级,第7行,第7行
必须声明标量变量“ @vDiai”。
列TPO_CFecha
的类型为int
。
我不知道自己在做错什么:
DECLARE @vDiai INT;
DECLARE @vDiaf INT;
SET @vDiai = 20171030;
SET @vDiaf = 20181030;
USE DBDMS
GO
SELECT
CAST(100 * Baja / (HeadcountBruto / Dias) AS decimal(20, 5))
FROM
(SELECT
SUM(ROT_IBAJA) AS Baja,
COUNT(DISTINCT R.TPO_KFecha) AS Dias,
SUM(ROT_IHCOUNT) AS HeadcountBruto
FROM
TH_Rotacion R
INNER JOIN
TD_Tiempo T ON R.TPO_KFECHA=T.TPO_KFecha
WHERE
T.TPO_CFecha >= @vDiai
AND T.TPO_CFecha <= @vDiaf) T
go
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'TD_Tiempo' AND
COLUMN_NAME = 'TPO_CFecha'
go
答案 0 :(得分:8)
GO
分解了您的脚本。只需移动或删除它即可:
USE DBDMS
GO
DECLARE @vDiai INT;
DECLARE @vDiaf INT;
SET @vDiai = 20171030;
SET @vDiaf = 20181030;
SELECT CAST(100*Baja/(HeadcountBruto/Dias) AS decimal(20,5))
FROM (SELECT SUM(ROT_IBAJA) AS Baja,
COUNT(DISTINCT R.TPO_KFecha) AS Dias,
SUM(ROT_IHCOUNT) AS HeadcountBruto
FROM TH_Rotacion R INNER JOIN
TD_Tiempo T
ON R.TPO_KFECHA = T.TPO_KFecha
WHERE T.TPO_CFecha >= @vDiai AND T.TPO_CFecha <= @vDiaf
) T;