
# 赋值语句
#### 变量语法
给变量赋值的语法请参见[图1]。
图1assignment_value::=   
![](https://support.huaweicloud.com/distributed-devg-v2-gaussdb/figure/zh-cn_image_0000002220984673.png "点击放大")
对以上语法格式的解释如下：
- variable_name，为变量名。
- value，可以是值或表达式。值value的类型需要和变量variable_name的类型兼容才能正确赋值。
 
#### 变量赋值示例
```
openGauss=# DECLARE
    emp_id  INTEGER := 7788;--赋值
BEGIN
    emp_id := 5;--赋值
    emp_id := 5*7784;
END;
/
```
#### INTO/BULK COLLECT INTO
将存储过程内语句返回的值存储到变量内，BULK COLLECT INTO允许将部分或全部返回值暂存到数组内部。
#### 示例
```
openGauss=# DROP TABLE IF EXISTS customers;
openGauss=# CREATE TABLE customers(id int,name varchar);
openGauss=# INSERT INTO customers VALUES(1,'ab');
openGauss=# DECLARE
    my_id integer;
BEGIN
    select id into my_id from customers limit 1; -- 赋值
END;
/
openGauss=# DECLARE
    type id_list is varray(6) of customers.id%type;
    id_arr id_list;
BEGIN
    select id bulk collect into id_arr from customers order by id DESC limit 20; -- 批量赋值
END;
/
```
![](https://support.huaweicloud.com/distributed-devg-v2-gaussdb/public_sys-resources/notice_3.0-zh-cn.png)
BULK COLLECT INTO 只支持批量赋值给数组。合理使用LIMIT字段避免操作过量数据导致性能下降。
