1
votes

I would like to create a table the name of which comes from result of a query. Any very basic example will do. The result could be a single column , single row also. I just need a basic example so I can tweak and modify it as per my requirement.

1

1 Answers

2
votes

any control command which creates a table requires the table name to be known in advance and part of the command's text.

you could run a 2-step flow programmatically, where:

  • the 1st step gets the table name (e.g. using a query),
  • the 2nd step generates the .create table or .set command string (based on the 1st), then invokes the command.

an example, using the .NET client library:

    using Kusto.Data;
    using Kusto.Data.Common;
    using Kusto.Data.Net.Client;

    using System.Linq;

    namespace Playground
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string clusterName = "myClusterName";
                const string regionName = "westus";
                const string databaseName = "myDatabaseName";
                const string queryForTableName = "MyExistingTable | summarize count() by TableName | top 1 by count_ desc | project TableName";
                var kcsb = new KustoConnectionStringBuilder($"https://{clusterName}.{regionName}.kusto.windows.net", databaseName).WithAadUserPromptAuthentication();
                using (var queryProvider = KustoClientFactory.CreateCslQueryProvider(kcsb))
                {
                    // step 1: get the table name, based on the result of a query                    
                    var tableName = queryProvider.ExecuteQuery<string>(queryForTableName).Single();

                    using (var adminProvider = KustoClientFactory.CreateCslAdminProvider(kcsb))
                    {
                        // step 2.1: generate the control command's text, using the value from step 1
                        var createTableCommand = CslCommandGenerator.GenerateTableSetCommand(tableName, "print value = 'This is a value in my new table'", isAsync: false);

                        // step 2.2: invoke the control command
                        adminProvider.ExecuteControlCommand(createTableCommand);
                    }
                }
            }
        }
    }