如何在PostgreSQL中创建一个临时函数? [英] How to create a temporary function in PostgreSQL?

查看:481
本文介绍了如何在PostgreSQL中创建一个临时函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在数据库中执行循环.这只是一个时间要求. 执行完函数后,我现在要删除函数.

I have to execute a loop in database. This is only a one time requirement. After executing the function, I am dropping the function now.

是否有创建临时/一次性功能的好方法?

Is there any good approach for creating temporary / disposable functions?

推荐答案

我需要知道如何在正在编写的脚本中多次使用.事实证明,您可以使用pg_temp模式创建一个临时函数.这是根据需要为您的连接创建的架构,是存储临时表的位置.当您的连接关闭或过期时,此架构将被删除.事实证明,如果您在此架构上创建函数,该架构将自动创建.因此,

I needed to know how to do a many time use in a script I was writing. Turns out you can create a temporary function using the pg_temp schema. This is a schema that is created on demand for your connection and is where temporary tables are stored. When your connection is closed or expires this schema is dropped. Turns out if you create a function on this schema, the schema will be created automatically. Therefore,

create function pg_temp.testfunc() returns text as 
$$ select 'hello'::text $$ language sql;

将是一个功能,只要您的连接一直保持不变.无需调用放置命令.

will be a function that will stick around as long as your connection sticks around. No need to call a drop command.

这篇关于如何在PostgreSQL中创建一个临时函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆