标签: plpgsql

是否可以在 PostgreSQL 中创建一个带有变量名的表?

使用 PL/pgSQL 或(其他一些机制),是否可以创建一个带有变量名的表?

我想创建多个名为 table_1、table_2、table_3 等的表……如果我可以使用循环来创建它们,而不是显式地创建每个表,那就更简单了。

我怀疑这个答案是否定的,但我想确认一下。

sql postgresql plpgsql

6
推荐指数
1
解决办法
5219
查看次数

在SQL中编写函数以遍历UDF中的日期范围

我正在尝试自动运行PLPGSQL一系列日期的函数的过程.
通常,我必须运行以下代码,每个函数调用每天生成一个表:

SELECT dhcp.singleday('2012-11-24'::date, '2012-11-25'::date);
SELECT dhcp.singleday('2012-11-25'::date, '2012-11-26'::date);
SELECT dhcp.singleday('2012-11-26'::date, '2012-11-27'::date);
SELECT dhcp.singleday('2012-11-27'::date, '2012-11-28'::date);
SELECT dhcp.singleday('2012-11-28'::date, '2012-11-29'::date);
SELECT dhcp.singleday('2012-11-29'::date, '2012-11-30'::date);
SELECT dhcp.singleday('2012-11-30'::date, '2012-12-01'::date);
SELECT dhcp.singleday('2012-12-01'::date, '2012-12-02'::date);
SELECT dhcp.singleday('2012-12-02'::date, '2012-12-03'::date);
SELECT dhcp.singleday('2012-12-03'::date, '2012-12-04'::date);
Run Code Online (Sandbox Code Playgroud)

有一个很好的方法可以通过一个简单的循环或函数自动化这种事情来处理任意日期范围吗?

我认为可能很难处理每个月的情况,所以我认为假设日期范围是一个月更好.

sql postgresql plpgsql

6
推荐指数
2
解决办法
9113
查看次数

在 PostgreSQL 表中查找循环引用?

我有一张带有属性的表(ID int、SourceID int、TargetID int、TargetType int)

ID 源ID 目标ID
--------------------
1 123 456  
2 456 789  
3 1 123  
4 456 1   
5 2 1   

我想找出所有循环引用。我想为此编写 PL/pgsql 函数。

这里 ID 4 的循环引用 = 456 1 123 456

我想找到这样的例子。谁能建议我如何进行此操作。

sql postgresql plpgsql circular-reference postgresql-9.1

6
推荐指数
1
解决办法
2390
查看次数

在 Postgres 触发器函数中的异常调用之前执行操作

Postgres 8.4 在这里。想象一下来自 Postgres doc 的这个代码片段:

CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
    -- Check that empname and salary are given
    IF NEW.empname IS NULL THEN
        RAISE EXCEPTION 'empname cannot be null';
    END IF;
    IF NEW.salary IS NULL THEN
        RAISE EXCEPTION '% cannot have null salary', NEW.empname;
    END IF;

    -- Who works for us when she must pay for it?
    IF NEW.salary < 0 THEN
        RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
    END IF;

    -- …
Run Code Online (Sandbox Code Playgroud)

postgresql triggers exception plpgsql

6
推荐指数
1
解决办法
5773
查看次数

在 plpgsql 声明块中使用 \set 变量

我的 SQL 脚本包含以下内容:

\set test 'some value'  
DO $$DECLARE  
  v_test text:= :'test';  
BEGIN  
  RAISE NOTICE 'test var is %',v_test;  
END$$;  
Run Code Online (Sandbox Code Playgroud)

尝试评估 test 的值时出现语法错误:

ERROR:  syntax error at or near ":"  
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望有一个匿名 plpqsql 块存在于一个文件中,然后使用一组环境变量从 shell 脚本中调用该块

postgresql plpgsql psql

6
推荐指数
1
解决办法
1483
查看次数

在函数体之前还是之后声明“LANGUAGE plpgsql”?

之间有什么区别:

CREATE FUNCTION func() RETURNS integer
    LANGUAGE plpgsql AS $$
    declare
    begin
      -- do something
    end
$$;
Run Code Online (Sandbox Code Playgroud)

和

CREATE FUNCTION func() RETURNS INTEGER AS $$
    declare
    begin
      -- do something
    end
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

LANGUAGE plpgsql基本上只需要超出使用范围吗$$?

postgresql syntax quotes plpgsql user-defined-functions

6
推荐指数
1
解决办法
2250
查看次数

将函数返回的记录拆分为多列

在基本的 Postgres 函数教程中,有一个带有OUT如下参数的示例:

create or replace function hi_lo(a numeric, 
                                 b numeric, 
                                 c numeric, 
                                 OUT hi numeric, 
                                 OUT lo numeric)
as $$
begin
hi := greatest(a, b, c);
lo := least(a, b, c);
end; $$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)

然后结果看起来像

select hi_lo(2, 3, 4);
-- returns one column, "hi_lo" with value "(4, 2)".

select * from hi_lo(2, 3, 4);
-- returns two columns, "hi" / 4 and "lo" / 2.
Run Code Online (Sandbox Code Playgroud)

但是,假设您想要对执行联接的列执行该函数,并且您无权修改该函数或使用替代函数?例如,使用一些玩具数据:

select hi_lo(a.actor_id, length(a.name), ma.movie_id) 
from 
    actors a 
join 
    movies_actors ma …
Run Code Online (Sandbox Code Playgroud)

sql postgresql record plpgsql

6
推荐指数
1
解决办法
2562
查看次数

有没有办法在 pgsql 中捕获延迟约束的异常?

考虑以下代码:

drop table if exists demo cascade;
drop table if exists child cascade;

create table demo
(
    id bigint not null 
    constraint demo_pk primary key
);

create table child
(
    id bigint not null 
        constraint child_pk primary key,
    pid bigint,
        constraint child_pid_fk
        foreign key (pid)
        references demo (id)
        deferrable initially deferred  -- remove this line and exceptions get caught
);

insert into demo values (1);
insert into child values (11, 1);

do language plpgsql $$
begin
    delete from demo where …
Run Code Online (Sandbox Code Playgroud)

postgresql exception constraints plpgsql

6
推荐指数
1
解决办法
646
查看次数

如何从函数返回表的行类型和附加列?

我有一个像这样定义的表:

create table users (
  id serial primary key,
  name text,
  email text,
);
Run Code Online (Sandbox Code Playgroud)

...我想编写一个返回形状行的函数:

(
  id integer,
  name text,
  email text,
  some_other_column boolean,
)
Run Code Online (Sandbox Code Playgroud)

我设法使用下面的代码使其工作,但我宁愿不重新定义用户表中的列:

create or replace function get_users () 
returns table (
  id integer,
  name text,
  email text,
  some_other_column boolean,
) as $$
    select users.*, true as some_other_column from users;
$$ language sql;
Run Code Online (Sandbox Code Playgroud)

有没有办法通过执行这样的操作来动态创建行类型?(postgres 在 处抱怨语法错误users.*):

create or replace function get_users () 
returns table (
  users.*, 
  some_other_column boolean
) as $$
    select users.*, true as …
Run Code Online (Sandbox Code Playgroud)

postgresql return-type plpgsql set-returning-functions

6
推荐指数
1
解决办法
1785
查看次数

如何在 ON CONFLICT 子句中消除 plpgsql 变量名称的歧义?

鉴于此表:

create table test (
    name text primary key
);
Run Code Online (Sandbox Code Playgroud)

我需要编写一个变量名与主键名冲突的 plpgsql 函数,我必须在on conflict子句中使用它:

create or replace function func(
    name text                               -- this variable name...
) returns void language plpgsql as 
$$
begin 
    insert into test (name) values (name) 
    on conflict (name) do update            -- ...conflicts with this line
    set name = func.name; 
end; 
$$;
Run Code Online (Sandbox Code Playgroud)

这会编译,但会抛出一个不明确的列引用:

select * from func('one');
ERROR:  column reference "name" is ambiguous
LINE 2:     on conflict (name) do update 
                        ^
DETAIL:  It could refer …
Run Code Online (Sandbox Code Playgroud)

sql postgresql upsert plpgsql name-conflict

6
推荐指数
1
解决办法
1649
查看次数