我有以下选择声明。
WITH cte AS
(
SELECT MAX(logDate) AS Daily
FROM PhysicalDriveSize
GROUP BY DATEADD(m, DATEDIFF(m, 0, logDate), 0)
)
SELECT CAST(MIN(t.logDate) AS DATE) AS [Date], Drive, MIN(t.Free) AS [PercentagFree]
FROM PhysicalDriveSize AS t
JOIN cte AS m
ON t.logDate = m.Daily
WHERE t.Free <= 10 AND t.Free >= 0
GROUP BY Drive, logDate
我想根据前面的结果做一些行动。
WITH cte AS
(
SELECT MAX(logDate) AS Daily
FROM PhysicalDriveSize
GROUP BY DATEADD(m, DATEDIFF(m, 0, logDate), 0)
)
SELECT MIN(t.Free) AS [PercentagFree]
FROM PhysicalDriveSize AS t
JOIN cte AS m
ON t.logDate = m.Daily
WHERE t.Free <= 10 AND t.Free >= 0
GROUP BY Drive, logDate
IF t.Free <= 10 AND t.Free >= 5
PRINT 'Warning'
Else
PRINT 'Critical'
如果我执行if语句,我会收到以下错误:
Msg 4104, Level 16, State 1, Line 13 The multi-part identifier "t.Free" could not be bound. Msg 4104, Level 16, State 1, Line 13 The multi-part identifier "t.Free" could not be bound
如何在没有错误的情况下使用if语句?
答案 0 :(得分:2)
尝试使用case when then
,如下所示:
WITH cte AS
(
SELECT MAX(logDate) AS Daily
FROM PhysicalDriveSize
GROUP BY DATEADD(m, DATEDIFF(m, 0, logDate), 0)
)
SELECT MIN(t.Free) AS [PercentagFree],
case when t.Free <= 10 AND t.Free >= 5 then 'Warning'
else 'Critical' end
FROM PhysicalDriveSize AS t
JOIN cte AS m
ON t.logDate = m.Daily
WHERE t.Free <= 10 AND t.Free >= 0
GROUP BY Drive, logDate
修改强>
如果要发送消息,可以尝试创建一个临时变量,如
DECLARE @x int;
WITH cte AS
(
SELECT MAX(logDate) AS Daily
FROM PhysicalDriveSize
GROUP BY DATEADD(m, DATEDIFF(m, 0, logDate), 0)
)
SELECT @x = MIN(t.Free) AS [PercentagFree]
FROM PhysicalDriveSize AS t
JOIN cte AS m
ON t.logDate = m.Daily
WHERE t.Free <= 10 AND t.Free >= 0
GROUP BY Drive, logDate
if(@x is not null)
PRINT 'Warning'
Else
PRINT 'Critical'
答案 1 :(得分:1)
你可以像这样使用:
IF(t.Free <= 10 AND t.Free >= 0, 'Warning', 'Critical') as free