assert()
適用於:✅Microsoft網狀架構✅Azure 數據✅總管 Azure 監視器✅Microsoft Sentinel
檢查條件。 如果條件為 false,則輸出錯誤訊息並失敗查詢。
注意
函 assert
式會在查詢分析階段進行評估,然後再套用常數折疊和述詞等優化。
注意
提供給 assert
的參數必須在查詢分析階段評估為常數。 換句話說,它只能從參考常數的其他表達式建構,而且無法系結至數據列內容。
語法
assert(
條件,
訊息)
深入瞭解 語法慣例。
參數
姓名 | 類型 | 必要 | 描述 |
---|---|---|---|
condition | bool |
✔️ | 要評估的條件運算式。 條件必須在查詢分析階段評估為常數。 |
message | string |
✔️ | 判斷提示評估為 false 時所使用的訊息。 |
傳回
如果條件為 true
,則傳true
回 。
如果條件評估為 false
,就會引發語意錯誤。
範例
下列查詢會定義檢查輸入字串長度的函 checkLength()
式,並使用 assert
來驗證輸入長度參數(檢查其是否大於零)。
let checkLength = (len:long, s:string)
{
assert(len > 0, "Length must be greater than zero") and
strlen(s) > len
};
datatable(input:string)
[
'123',
'4567'
]
| where checkLength(len=long(-1), input)
執行此查詢會產生錯誤: assert() has failed with message: 'Length must be greater than zero'
使用有效 len
輸入執行的範例:
let checkLength = (len:long, s:string)
{
assert(len > 0, "Length must be greater than zero") and strlen(s) > len
};
datatable(input:string)
[
'123',
'4567'
]
| where checkLength(len=3, input)
輸出
input |
---|
4,567 |
下列查詢一律會失敗,示範assert
即使 where b
運算符在 為false
時b
不會傳回任何數據,仍會評估函式:
let b=false;
print x="Hello"
| where b
| where assert(b, "Assertion failed")