
# 插入数据至DCS表
#### 功能描述
INSERT INTO命令将DLI表中的数据插入到已关联的DCS Key中。
#### 语法格式
- 将SELECT查询结果插入到表中：
  ```
  INSERT INTO DLI_TABLE
    SELECT field1,field2...
    [FROM DLI_TEST]
    [WHERE where_condition]
    [LIMIT num]
    [GROUP BY field]
    [ORDER BY field] ...;
  ```
  
- 将某条数据插入到表中：
  ```
  INSERT INTO DLI_TABLE
    VALUES values_row [, values_row ...];
  ```
  
 
#### 关键字
SELECT对应关键字说明请参考[基本语句](https://support.huaweicloud.com/sqlref-spark-dli/dli_08_0150.html#dli_08_0150)。
#### 参数说明
表1参数描述 
| **参数**                 | **描述**                                  |
|:---|:---|
| DLI_TABLE              | 已创建跨源连接的DLI表名称。                         |
| DLI_TEST               | 为包含待查询数据的表。                             |
| field1,field2...，field | 表"DLI_TEST"中的列值，需要匹配表"DLI_TABLE"的列值和类型。 |
| where_condition        | 查询过滤条件。                                 |
| num                    | 对查询结果进行限制，num参数仅支持INT类型。                |
| values_row             | 想要插入到表中的值，列与列之间用逗号分隔。                   |
   
#### 注意事项
- DLI表必须已经存在。
- DLI表在创建时需要指定Schema信息。
- 如果在建表时指定"key.column"，则在Redis中会以指定字段的值作为Redis Key名称的一部分。例如：
  ```
  create table test_redis(name string, age int) using redis options(
    'host' = '192.168.4.199',
    'port' = '6379',
    'passwdauth' = '******',
    'table' = 'test_with_key_column',
    'key.column' = 'name'
  );
  insert into test_redis values("James", 35), ("Michael", 22);
  ```
  在redis中将会有2个名为test_with_key_column:James和test_with_key_column:Michael的表：
  ![](https://support.huaweicloud.com/sqlref-spark-dli/zh-cn_image_0223994226.png)
  ![](https://support.huaweicloud.com/sqlref-spark-dli/zh-cn_image_0223994227.png)
  
- 如果在建表时没有指定"key.column"，则在Redis中的key name将会使用uuid。例如：
  ```
  create table test_redis(name string, age int) using redis options(
    'host' = '192.168.7.238',
    'port' = '6379',
    'passwdauth' = '******',
    'table' = 'test_without_key_column'
  );
  insert into test_redis values("James", 35), ("Michael", 22);
  ```
  在redis中将会有2个以"test_without_key_column:uuid"命名的表：
  ![](https://support.huaweicloud.com/sqlref-spark-dli/zh-cn_image_0223994228.png "点击放大")
  ![](https://support.huaweicloud.com/sqlref-spark-dli/zh-cn_image_0223994229.png "点击放大")
  
 
#### 示例
```
INSERT INTO test_redis
  VALUES("James", 35), ("Michael", 22);
```
