创建和更改Azure SQL外部表

使用 “版本 ”下拉列表切换服务。 了解有关导航的详细信息
适用于:✅ Azure Data Explorer

在执行命令的数据库中创建或更改 Azure SQL external 表

Note

  • 如果该表存在,则 .create 命令会失败并显示错误。 使用 .create-or-alter.alter 修改现有表。
  • 不支持更改外部Azure SQL表的架构。

支持Azure SQL外部表类型

  1. SQL Server
  2. MySQL
  3. PostgreSQL
  4. Cosmos DB

Permissions

若要.create至少需要 Database User 权限,并且 .alter 至少需要 Table Admin 权限。

若要使用托管标识身份验证.create.alter.create-or-alter外部表,需要具有 Database Admin 权限。 SQL Server和 Cosmos DB 外部表支持此方法。

Syntax

(.create | .alter | .create-or-alter) externaltable TableName(Schema)kind=sql [ table= SqlTableName ] (SqlConnectionString) [with( [ sqlDialect= SqlDialect ] , [ Property, ... ])]

详细了解语法约定

Parameters

Name 类型 Required Description
TableName string ✔️ 外部表的名称。 该名称必须遵循实体名称规则,并且外部表不能与同一数据库中的常规表同名。
Schema string ✔️ 外部数据架构是包含一个或多个列名和数据类型的逗号分隔列表,其中的每个项遵循以下格式:ColumnName ColumnType:
SqlConnectionString string ✔️ SQL server的connection string。
SqlTableName string 不包括数据库名称的 SQL 表的名称。 如果表的名称包含句点(“.”),则使用 ['Name.of.the.table'] 表示法。 如果表不在默认 SQL 数据库架构中,请使用 ['SchemaName.TableName'] 表示法。 例如,对于架构“S1”中的表“T1”:['S1。T1']

除 Cosmos DB 以外的所有类型的表都需要此规范,因为对于 Cosmos DB,集合名称是connection string的一部分。
SqlDialect string 指示Azure SQL外部表的类型。 SQL Server为默认值。 对于 MySQL,请指定 MySQL。 对于 PostgreSQL,请指定 PostgreSQL。 对于 Cosmos DB,请指定 CosmosDbSql
Property string 采用 PropertyName= PropertyValue 格式的键值属性对。 请参阅可选属性

Warning

应该对包含机密信息的连接字符串和查询进行模糊处理,使其在任何 Kusto 跟踪中都被忽略。 有关详细信息,请参阅经过模糊处理的字符串文本

可选属性

Property 类型 Description
folder string 表的文件夹。
docString string 一个用来记录表的字符串。
firetriggers true/false 如果为 true,则指示目标系统激发 SQL 表上定义的 INSERT 触发器。 默认为 false。 (有关详细信息,请参阅 BULK INSERTSystem.Data.SqlClient.SqlBulkCopy
createifnotexists true/ false 如果为 true,则会在目标 SQL 表不存在的情况下创建该表;在这种情况下,必须提供 primarykey 属性来指示作为主键的结果列。 默认为 false
primarykey string 如果 createifnotexiststrue,则生成的列名称将用作 SQL 表的主键(如果是此命令创建的)。

身份验证和授权

若要与外部Azure SQL表交互,必须将身份验证方法指定为 SqlConnectionString 的一部分。 SqlConnectionString定义要access的资源及其身份验证信息。

有关详细信息,请参阅 Azure SQL外部表身份验证方法

Note

如果外部表用于连续导出,则必须通过用户名/密码或托管标识进行身份验证。

Examples

以下示例演示如何创建每种Azure SQL外部表。

SQL Server

.create external table MySqlExternalTable (x:long, s:string) 
kind=sql
table=MySqlTable
( 
   h@'Server=tcp:myserver.database.chinacloudapi.cn,1433;Authentication=Active Directory Integrated;Initial Catalog=mydatabase;'
)
with 
(
   docstring = "Docs",
   folder = "ExternalTables", 
   createifnotexists = true,
   primarykey = x,
   firetriggers=true
)  

Output

TableName TableType Folder DocString 属性
MySqlExternalTable Sql ExternalTables Docs {
“TargetEntityKind”: “sqltable'”,
"TargetEntityName":"MySqlTable",
“TargetEntityConnectionString”: “Server=tcp:myserver.database.chinacloudapi.cn,1433;Authentication=Active Directory Integrated;初始 Catalog=mydatabase;",
“FireTriggers”: true,
“CreateIfNotExists”: true,
“PrimaryKey”: “x”
}

MySQL

.create external table MySqlExternalTable (x:long, s:string) 
kind=sql
table=MySqlTable
( 
   h@'Server=myserver.mysql.database.chinacloudapi.cn;Port = 3306;UID = USERNAME;Pwd = PASSWORD;Database = mydatabase;'
)
with 
(
   sqlDialect = "MySql",
   docstring = "Docs",
   folder = "ExternalTables", 
)  

PostgreSQL

.create external table PostgreSqlExternalTable (x:long, s:string) 
kind=sql
table=PostgreSqlTable
( 
   h@'Host = hostname.postgres.database.chinacloudapi.cn; Port = 5432; Database= db; User Id=user; Password=pass; Timeout = 30;'
)
with 
(
   sqlDialect = "PostgreSQL",
   docstring = "Docs",
   folder = "ExternalTables", 
)  

Cosmos DB

.create external table CosmosDBSQLExternalTable (x:long, s:string) 
kind=sql
( 
   h@'AccountEndpoint=https://cosmosdbacc.documents.azure.cn/;Database=MyDatabase;Collection=MyCollection;AccountKey=' h'R8PM...;'
)
with 
(
   sqlDialect = "CosmosDbSQL",
   docstring = "Docs",
   folder = "ExternalTables", 
)