使用 SQL Server 仿真运行 KQL 查询和存储函数

Azure 数据资源管理器提供表格格式数据流 (TDS) 终结点,使你能够以类似于在 SQL Server 中查询数据的方式查询数据。 终结点支持 TDS 版本 7.x 和 8.0。

本文介绍如何从 SQL 兼容客户端运行存储函数Kusto 查询语言 (KQL) 查询。

有关详细信息,请参阅 Azure 数据资源管理器中的 SQL Server 仿真概述

运行 KQL 查询

SQL 存储过程 sp_execute_kql 可用于运行 KQL 查询,包括参数化查询。 此过程与 sp_executesql 存储过程类似。

sp_execute_kql 的第一个参数是 KQL 查询,任何其他参数都被视为查询参数。 下面的示例说明如何使用 sp_execute_kql

  using (var connection = new SqlConnection(csb.ToString()))
  {
    await connection.OpenAsync();
    using (var command = new SqlCommand("sp_execute_kql", connection))
    {
      command.CommandType = CommandType.StoredProcedure;
      var query = new SqlParameter("@kql_query", SqlDbType.NVarChar);
      command.Parameters.Add(query);
      var parameter = new SqlParameter("mylimit", SqlDbType.Int);
      command.Parameters.Add(parameter);
      query.Value = "StormEvents | take myLimit";
      parameter.Value = 3;
      using (var reader = await command.ExecuteReaderAsync())
      {
        // Read the response.
      }
    }
  }

注意

通过 TDS 调用 sp_execute_kql 时,参数类型是由协议设置的,不需要声明。

调用存储函数

可以像 SQL 存储过程一样创建和调用存储函数。 例如,如果你有下表中描述的存储函数,则可以按照代码示例所示调用它。

名称 parameters 正文 文件夹 DocString
MyFunction (myLimit: long) {StormEvents | take myLimit} MyFolder 带参数的演示函数
  using (var connection = new SqlConnection(csb.ToString()))
  {
    await connection.OpenAsync();
    using (var command = new SqlCommand("kusto.MyFunction", connection))
    {
      command.CommandType = CommandType.StoredProcedure;
      var parameter = new SqlParameter("mylimit", SqlDbType.Int);
      command.Parameters.Add(parameter);
      parameter.Value = 3;
      using (var reader = await command.ExecuteReaderAsync())
      {
        // Read the response.
      }
    }
  }

注意

若要区分存储函数和模拟的 SQL 系统存储过程,请通过对 kusto 架构的显式引用调用存储函数。 在此示例中,使用 kusto.Myfunction 来调用存储函数。