# Green plum函数和存储过程

> 作者/来源: UCloud 运营管理员
> 发布时间: 2023-01-11T05:19:00.000Z
> 分类: 云主机
> 标签: AI, 数据库, 安全
> 原文链接: http://117.50.162.249:3000/yun/articles/1412

---

# Green plum函数和存储过程

> 来源: https://www.ucloud.cn/yun/129485.html
> 作者: IT那活儿
> 发布日期: 发布于2023-01-11 13:19

Green plum函数和存储过程

**点击上方“IT那活儿”公众号，关注后了解更多内容，不管IT什么活儿，干就完了！！！**

# 01

存储过程是一组可以完成特定功能的 SQL 语句集，经编译后存储在数据 库中。用户在行存储过程时，需要指定存储过程的名称并给出参数函数和存储过程语句。

**优点：**

- 多个客户端应用程序是由不同的语言编写，或者运行在不同的平台，需要执行同样的数据库操作；
- 安全性很高；
- 可以提升性能；
- 允许用户在数据库服务器中使用函数库。

**语法规范如下：**

```
CREATE FUNCTION RETURNS AS $$DECLARE;BEGIN;;;[exception]when thenEND;$$ LANGUAGE plpgsql [volatile|immutable|stable];
```

green plum中如果有具体的返回值就是函数，如果没有返回值就是存储过程。

# 02

**函数的创建**

```
CREATE OR REPLACE FUNCTION storcfunc() RETURNS integer AS $$DECLAREquantity integer :=6;BEGINRAISE NOTICE Quantity here is % ,quantity; -- Quantity here is 6quantity :=50;DECLAREquantity integer :=9;BEGINRAISE NOTICE Quantity here is % ,quantity; -- Quantity here is 9END;RAISE NOTICE Quantity here is % ,quantity; -- Quantity here is 3RETURN quantity;END;$$ LANGUAGE plpgsql;
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_000.png)

如果需要查看具体函数，则需要使用df+ [函数名]：

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_001.png)

# 03

**存储过程的创建**

```
CREATE OR REPLACE FUNCTION stored_procedure() RETURNS void as $$BEGINtruncate updated;insert into updatedselect y.* from omn_dew x right outer join omn_src y on x.empid=y.empid where x.empid is not null;update omn_dewset deptno=y.deptno,sal=y.salfrom updated ywhere omn_dew.empid=y.empid;insert into omn_dewselect y.* from omn_dew x right outer join omn_src y on x.empid=y.empid where x.empid is null;end;$$ language plpgsql;
```

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_002.png)

存储过程同理df+ [存储过程名]。

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_003.png)

# 04

**pg\_proc系统表：**此表存储着关于函数(或过程)的信息，包括内置函数和那些通过create。

**function定义的函数**

- proname：函数的名字。
- pronamespace：函数的oid。
- proowner：函数的所有者。
- prolang：函数的实验语言或调用接口。
- proisagg：是否为聚集函数。
- prosecdef：是否为安全定义器(一个 setuid 函数)。
- proisstrict：如果任何调用参数都为空，函数直接返回空，此时函数实际上不会调用。
- proretset：是否返回一个集合。
- provolatile：函数结果是否只依赖于输入参数，或者会被外界因素影响。
- pronargs：参数的个数。
- prorettype：返回值的数据类型。
- proargtypes：存放函数参数数据类型的数组。
- proallargtypes：存放函数参数数据类型的数组。
- proargmodes：存放函数参数模式的数组。
- proargnames：存放函数参数名字的数组。
- prosrc 该字段告诉函数处理器如何调用该函数。
- probin：如何调用该函数的附加信息。
- proacl：grant和revoke分配的访问权限。

通过系统表查看函数：

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_004.png)

多表查询可以确认函数是否正常：

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_005.png)

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_006.png)

END

## 本文作者：陆 游

## 本文来源：IT那活儿（上海新炬王翦团队）

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129485/images/129485_007.png)