Search code examples
azure-log-analyticsazure-data-explorer

Check if a table exists in Kusto language?


Is there a way to programmatically check if a table is existent in log analytics using the kusto language?

For example, let's suppose I want to check if a workspace contains the VMConnection table something like :

IF OBJECT_ID('*objectName*', 'U') IS NOT NULL 

OR

IF (EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'TheSchema' 
                 AND  TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END

Solution

  • Perhaps, you can use next technique to check if there is non-empty table present:

    let hasNonEmptyTable = (T:string) 
    { 
       toscalar( union isfuzzy=true ( table(T) | count as Count ), (print Count=0) | summarize sum(Count) ) > 0
    };
    let TableName = 'StormEvents';
    print Table=TableName, IsPresent=iif(hasNonEmptyTable(TableName), "Table present", "Table not preset")
    

    You can try running it online using https://dataexplorer.azure.com/clusters/help/