Only run MySQL query if variable is filled

Hello everyone,

I would like to know if it is possible in Grafana to cancel a MySQL query directly if none of the variables on the dashboard are filled. So far, every attempt has failed.

The problem is that the query runs for a relatively long time and a table should only be output if at least one of the two variables are filled.

My Query looks like this (simplified):

SELECT M.FIRMA1, M.PLZ, M.ORT, A.DATUM
FROM Table1 M
WHERE SOLL LIKE '0%' AND STORNO_NR = 0 AND A.DELETED = 0 AND M.DELETED = 0
AND M.FIRMA1 LIKE '%${firma}%' AND M.ORT LIKE '%${ort}%'

How can i achive this?

Thank you,
Benjamin

if not filled what is the value of those vars? null or empty string?

not a grafana issue but this might work.

AND ( (${firma} = '' or ${firma} is null) OR  
M.FIRMA1 LIKE '%${firma}%'
 )

AND ( (${ort} = '' or ${ort} is null) OR 
M.ORT LIKE '%${ort}%')
1 Like

Thank you, but unfortunately, it does not work.

The query is still executed when the dashboard is opened and takes a long time.

There is no setting in Grafana that a query is only executed when a user action is performed, right?

gotcha, you can check the values as following in the query panel.

if ${firma} <> '' or ${firma} is not null 
begin
   --query here.
end
2 Likes

Thank you again! Sadly it did not work.

But I tried again with a different query and this time it did the trick.

I simply added the following row:

AND (CASE WHEN ('${firma}' = '') THEN 1=2 ELSE 1=1 END)

Thank you for writing so quickly!

1 Like