0

I have the below select statement.

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

I want to do some actions based on the results for ex.

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'

I get the below error if I perform if statement:

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

how can I engage if statement without errors??

3
  • What error did you get? Commented Nov 4, 2015 at 10:08
  • 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. Commented Nov 4, 2015 at 10:09
  • you should be using a case statmement Commented Nov 4, 2015 at 10:10

2 Answers 2

2

Try to use case when then like this:

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

EDIT:

If you want to send the message you can try to create a temp variable like

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'
Sign up to request clarification or add additional context in comments.

6 Comments

I got the below error "Incorrect syntax near the keyword 'case'."
@user1071629:- Comma was misisng. Update that. try now!
The code is working, but the purpose is to send alert mail after then instead of warning and critical.
@user1071629:- Not sure if I understand it correctly. I have updated my code. Were you looking for this?
Thanx Rahul its working with temp variable, appreciate your help.
|
1

you can use like this :

IF(t.Free <= 10 AND t.Free >= 0, 'Warning', 'Critical') as free

1 Comment

What about the t.Free >= 0 condition?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.