我想为插入操作创建触发器,以及打印插入值的过程。
CREATE TRIGGER added_product_info_trigger
BEFORE INSERT
ON products
EXECUTE PROCEDURE added_product_info();
Run Code Online (Sandbox Code Playgroud)
还有我的程序
CREATE OR REPLACE FUNCTION added_product_info()
RETURNS trigger
AS
$$
(Select p.productname, s.companyname from products as p, suppliers as s
where p.supplierid = s.supplierid)
$$ LANGUAGE SQL;
Run Code Online (Sandbox Code Playgroud)
如何打印我插入的值?
我知道有 的过程SELECT array_agg(f) FROM (SELECT blah FROM stuff) f,这在 SQL 中很棒,但是在 PL/pgSQL 中编写函数时,有没有简写方法?
我正在尝试将 JSON 键放入一个数组中,我可以用它来查看长度。就像是...
v_len := array_length( array_agg(json_object_keys(myjson)), 1);
而不是长, DECLARE a 变量,做 a SELECT array_agg(f) INTO ...,这是我一直在做的。我似乎已经使用相同的 SQL 字符串实现了数百种实现,但我真的很想减少我的代码,我的手指因所有多余的输入而变得麻木。
这种速记方法缺少什么?
我想从代表 URL 的字符串中提取查询参数,并且我想在存储函数中执行此操作(偶然没有我可以使用的标准函数?)。
在 Python 中,这将是:
from urlparse import urlparse, parse_qs
def extract_oid(url):
"""
extract the 'oid' query argument
(simplified, no error handling)
>>> extract_oid('http://some.host/some/path?oid=abc123&other')
'abc123'
"""
return parse_qs(urlparse(url).query)['oid'][0]
Run Code Online (Sandbox Code Playgroud)
我目前的尝试plpgsql是:
CREATE OR REPLACE FUNCTION extract_oid (link text)
RETURNS text
AS $$
DECLARE
pos1 integer := position('&oid=' in link);
tail text := substring(link from pos1 + 1);
endpos integer := position('&' in tail);
BEGIN
if link is NULL or pos1 = 0 then
RETURN NULL;
ELSIF endpos = 0 …Run Code Online (Sandbox Code Playgroud) 我有两个数组:函数内的 foo_array text[]、bar_array text[] 。它们每个都包含字符串,这些字符串将使用“string_to_array”函数拆分为数组元素,并将类型转换为 bigint。
我想在表中返回这些数组(out1 bigint,out2 bigint)。
例如,foo_array 和 bar_array 每个包含 10 个元素,我希望该函数返回包含这些元素的 10 行。我只能产生 20 个元素的输出,并且不太理解它。
CREATE OR REPLACE FUNCTION ___two_unnests()
RETURNS TABLE(out1 bigint, out2 bigint) AS $$
DECLARE
foo_array text[];
bar_array text[];
foo1 text := array_to_string(ARRAY[1, 2, 3, 4, 5], ',');
foo2 text := array_to_string(ARRAY[11, 22, 33, 44, 55], ',');
bar1 text := array_to_string(ARRAY[6, 7, 8, 9, 10], ',');
bar2 text := array_to_string(ARRAY[66, 77, 88, 99, 1010], ',');
BEGIN
foo_array := (SELECT foo_array || foo1 || …Run Code Online (Sandbox Code Playgroud) days函数中的参数getAvgByDay()不起作用,我猜是因为它在引号内:
CREATE OR REPLACE FUNCTION getAvgByDay(days int)
RETURNS TABLE ( average text,
date timestamp with time zone
) AS
$func$
BEGIN
RETURN QUERY
SELECT to_char( AVG(measure), '99999D22') AS average, ( now() - interval '$1 day') AS date
FROM (
SELECT mes.date, mes.measure
FROM measures mes
WHERE mes.date < ( now() - interval '$1 day')
) AS mydata;
END
$func$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud) 我想创建一个函数,尝试将一组值转换为用户指定的类型(默认为文本)。一个非常简单的函数如下所示:
CREATE OR REPLACE FUNCTION cast_to(variable jsonb, key text, target_type anyelement DEFAULT 'TEXT'::regtype) RETURNS anyelement as $$
begin
RETURN CAST(variable->>key AS target_type);
end
$$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)
我已经尝试过以下方法:
SELECT CAST('foo' AS 'text');: 给出语法错误SELECT CAST('foo' AS 'text'::regtype);: 与 1 相同的错误SELECT CAST('foo' AS pg_typeof(null::text));说type pg_typeof does not exist 最后一次尝试是我可以传入一个具有目标类型的变量而不是文本表示形式。使用该函数将如下所示SELECT cast_to('text', NULL::text);。
如何实现这个或类似的功能?
编辑:正如评论中所建议的,我尝试使用动态 SQL。我运气不太好。我创建了一个非常基本的案例,不使用任何变量:
CREATE OR REPLACE FUNCTION audit.cast_to() RETURNS text as $$
DECLARE
_sql TEXT := 'SELECT CAST($1 AS $2)';
out TEXT;
begin …Run Code Online (Sandbox Code Playgroud) 我有一个插入、更新或删除表行的存储过程。当所有参数都用作输入时,它工作正常。但是,我需要返回最后插入行的 ID。为此,我尝试使用INOUT参数并在语句RETURNING之后INSERT返回 ID。
但是,我不知道如何将返回的 ID 绑定到参数INOUT。以下是存储过程的代码:
CREATE OR REPLACE PROCEDURE public.spproductinsertupdatedelete(
_ser integer,
_subcategid integer,
_inrprice numeric,
_usdprice numeric,
_colour integer,
_size integer,
_qty integer,
_prodid integer DEFAULT NULL::integer,
inout _pid integer default null
)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
if _ser=1 then --- Insert
INSERT INTO product (prod_subcateg_id,prod_inr_price,prod_usd_price,prod_colour,prod_size,prod_qty)
VALUES (_subcategID, _inrprice, _usdprice, _colour, _size, _qty)
RETURNING prod_id;
ELSEIF _ser=2 THEN
UPDATE PRODUCT SET
prod_subcateg_id = _subcategid,
prod_inr_price = _inrprice,
prod_usd_price = …Run Code Online (Sandbox Code Playgroud) 我将 plpgsql 与 Postgres 10.6 一起使用。我有一个函数声明变量并为其赋值。该函数还定义了一个视图,我想在定义中使用该变量。
create view myview as
select
some_columns
from
mytable
where
id = _id /*_id is declared earlier in function */
;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,可以定义该函数,但运行时会出现错误:UndefinedColumn: column "_id" does not exist
在 Postgres 中这样的事情可能吗?视图可以包含变量作为其定义的一部分吗?
我确实在这里看到,在 BigQuery (我从未使用过)中,我所要求的是不可能的,这让我认为这在 plpgsql 中也可能是不可能的。
这没什么大不了的,但我很好奇。一种解决方法 - 可能也是推荐的解决方案 - 是当我从视图中选择时传递 _id (例如select * from myview where id = 3)。或者,如果我真的想让 select 调用保持简单(我这样做,因为我的实际实现更复杂并且有多个变量),我可以将视图定义为字符串并execute在函数中使用(这是所有使用的内部内容)在构建和创建数据库时,而不是在担心动态 sql 固有的各种风险的情况下)。
如何识别 Postgres 函数中的慢查询?
例如:
CREATE OR REPLACE FUNCTION my_function ()
RETURNS void AS $$
BEGIN
query#1;
query#2; --> slow query (duration 4 sec)
query#3;
query#4;
END
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
执行后,my_function()我在 Postgres 日志文件中得到如下内容:
持续时间:4.904 ms 语句:select my_function ();",,,,,,,,,"psql"
所以我无法识别函数中的慢速查询。
我有一个plpgsql函数,它接受一个整数[]作为输入.
整个设置可以在这个问题中找到: 将记录作为函数参数PL/pgSQL传递
简短版本:我有一个从书本到作者的n到m关系,其中包含一个名为books_author的链接表.我现在有一个看起来像这样的函数:
create function f_insert_books(title varchar, isbn varchar, publisher varchar,
author_id integer[]) returns void as $$
begin
--insert book
--insert link to authors into the books_author table
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
我现在想在书中添加number_of_authors.有没有一种简单的方法来确定author_id数组的大小,或者您是否会建议将"number_of_authors int"作为输入参数传递?
我发现了这个建议,但我对这种方法的表现有点担心.所以也许有更简单/更快的东西. http://archives.postgresql.org/pgsql-sql/2000-06/msg00169.php
非常感谢您的帮助.
plpgsql ×10
postgresql ×10
sql ×4
function ×2
array-agg ×1
dynamic-sql ×1
performance ×1
polymorphism ×1
triggers ×1
view ×1