I have a requirement to print all rows if parameter is null is passed and if not null, use it in where clause
I am writing a Kusto function where I want to have some fixed parameters to the function which will be used in the "where" clause. I want to dynamically handle this. For example: If the value is present, then my where clause should consider that column filter, else it should not be included in the "where" clause>
I saw this question which seems to be doing it but unbale to fit in my requirement to the pseudo code in the this post.
My Table Name: SecurityGroups My where clause operator is GroupType My Current Sample Kusto Function:
.create-or-alter function fnGetSecurityGroups(groupType:string) {
SecurityGroups | where GroupType =~ groupType
}
The same query by @Yoni L. in the post which you have provided will work for you.
As per the Documentation,
null values are not supported for string data type.
So, use the empty string(""
) as the default value for the string parameter in your function.
As you want to print the rows, after the where
clause, don't add anything.
My sample:
let r = datatable(name:string, game:string)
[
"Rakesh","TT",
"Laddu","BB",
"Virat","Cricket"
];
let F1 = (_name: string = "") {
r
| where isempty(_name) or name == _name
};
F1("Rakesh");
Here, as we are using or
operator,
True
), then it won't check the next condition in where and it will directly print all the rows.My output when no value provided:
When value provided: