使用 PL/pgSQL 或(其他一些机制),是否可以创建一个带有变量名的表?
我想创建多个名为 table_1、table_2、table_3 等的表……如果我可以使用循环来创建它们,而不是显式地创建每个表,那就更简单了。
我怀疑这个答案是否定的,但我想确认一下。
我正在尝试自动运行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)
有一个很好的方法可以通过一个简单的循环或函数自动化这种事情来处理任意日期范围吗?
我认为可能很难处理每个月的情况,所以我认为假设日期范围是一个月更好.
我有一张带有属性的表(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
我想找到这样的例子。谁能建议我如何进行此操作。
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) 我的 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 脚本中调用该块
之间有什么区别:
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基本上只需要超出使用范围吗$$?
在基本的 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) 考虑以下代码:
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) 我有一个像这样定义的表:
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) 鉴于此表:
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) plpgsql ×10
postgresql ×10
sql ×5
exception ×2
constraints ×1
psql ×1
quotes ×1
record ×1
return-type ×1
syntax ×1
triggers ×1
upsert ×1