Azure Logic App:SQL查询空结果集条件检查返回True的异常问题
Alright, let's dig into why your Azure Logic App's condition is misbehaving when the SQL query returns an empty result set. I've run into similar head-scratchers before, so here are the most common culprits and fixes:
You're checking the wrong JSON path in your condition
The SQL Server action in Logic Apps nests results underresultsets>Table1(or your custom result set name). If you're checking the rootbodyof the action instead of the actual rows array, you might be evaluating a non-empty metadata object instead of the empty results.
Fix: Open the "Run History" of your Logic App, navigate to the "Execute SQL query" step, and look at the raw output. Grab the exact path to your rows (e.g.,body('Execute_SQL_query')?['resultsets']?['Table1']). Update your condition to target this path—useempty(body('Execute_SQL_query')?['resultsets']?['Table1'])to check if rows are empty.Null vs. Empty Array confusion
Sometimes, when the query returns no rows, the SQL action returnsnullinstead of an empty array[]. Logic Apps' "is not empty" condition treatsnullas "not empty" (since it’s a non-empty value type), which is why you’re seeingTruewhen you expectFalse.
Fix: Usecoalesce()to convert null to an empty array before checking. Example:empty(coalesce(body('Execute_SQL_query')?['resultsets']?['Table1'], [])). This handles both null and empty array cases correctly.Implicit type conversion messing up evaluations
Logic Apps can auto-convert values in conditions unexpectedly. A simple "is not empty" check on a null or non-array value might not behave as you’d expect.
Fix: Explicitly check the length of the results array. Uselength(coalesce(body('Execute_SQL_query')?['resultsets']?['Table1'], []))and compare it to0. If the length equals 0, the result set is empty.Stale run history or cached results
It’s easy to accidentally look at an older run where the query returned results instead of the latest empty run.
Fix: Open the most recent run with the unexpected condition result, go to the SQL query step, and inspect the "Outputs" tab. Copy the raw JSON to confirm exactly what data the Logic App is processing—this will eliminate any confusion about stale data.The query returns metadata but no rows
In some cases, even with no rows, the SQL action might return an object with metadata (like column names) but an empty rows array. If your condition checks the parent metadata object instead of the rows, it will think the result is non-empty.
Fix: Double-check the raw output to identify the exact array containing your rows, and adjust your condition to target that specific array.
Pro tip: Always start by inspecting the raw output of your SQL query step. Seeing the exact JSON structure the Logic App is working with makes troubleshooting the condition logic way easier.
内容的提问来源于stack exchange,提问作者Fisqkuz

