Click here to Skip to main content
15,882,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got this error when i ran the query.

An expression of non-boolean type specified in a context where a condition is expected, near 'BEGIN'.


<pre><pre>If (select DATEDIFF(MINUTE,LastUpdatedDateTime ,getdate()) AS Diffinminutes
  FROM [sports_UAT].[dbo].[DB_Spots_UAT] 
  where (DATEDIFF (MINUTE,LastUpdatedDateTime,getdate()) >10))

  BEGIN
EXEC  msdb.dbo.sp_send_dbmail
@profile_name = 'Always on DB Latency Alert Mail',
 @recipients = 'dbagroup@pointsbet.com',
@query = 'select DATEDIFF(MINUTE,[LastUpdatedDateTime] ,getdate()) AS Diffinminutes
  FROM [Sports_DEV].[dbo].[DB_Sports_DEV] 
  where DATEDIFF (MINUTE,[LastUpdatedDateTime],getdate()) >10',
@subject ='Always on Latency Alert',
@attach_query_result_as_file=1;

END


What I have tried:

If (select DATEDIFF(MINUTE,LastUpdatedDateTime ,getdate()) AS Diffinminutes
FROM [sports_UAT].[dbo].[DB_Spots_UAT]
where (DATEDIFF (MINUTE,LastUpdatedDateTime,getdate()) >10))
In the last line, closed the bracket from datediff to >10
Posted
Updated 15-Aug-21 21:53pm

1 solution

This isn't a boolean expression:
SQL
If (select DATEDIFF(MINUTE,LastUpdatedDateTime ,getdate()) AS Diffinminutes
  FROM [sports_UAT].[dbo].[DB_Spots_UAT] 
  where (DATEDIFF (MINUTE,LastUpdatedDateTime,getdate()) >10))

Let me simplify it for you so it's more obvious:
SQL
If (SELECT * FROM MyTable)

That just returns a dataset, not a single BOOLEAN value - which is the only thing IF accepts.

If you want to know if there are any results that match, then use COUNT:
SQL
If (SELECT COUNT(*) FROM MyTable) > 0
Otherwise, you need to work out exactly what your condition is supposed to be looking at - we have no idea!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900