我有以下类型的几个大查询(为了清楚起见简化了)。
create function myfunction()
returns void
as $$
begin
...
with t as (
total as total,
total * 100 / total as total_percent,
total / people.count as total_per_person,
part1 as part1,
part1 * 100 / total as part1_percent,
part1 / people.count as part1_per_person,
part2 as part2,
part2 * 100 / total as part2_percent,
part2 / people.count as part2_per_person,
...
from (
select
total.amount as total
part1.amount as part1
part2.amount as part2
...
people.amount as people
from (select ...from mytable..) …Run Code Online (Sandbox Code Playgroud) sql postgresql plpgsql common-table-expression postgresql-9.3
我有一个要求,我必须禁止删除除一个用户之外的所有用户的表记录,即 user1。为此,我使用了如下触发器。
CREATE OR REPLACE FUNCTION prevent_deletion() RETURNS trigger AS $$
declare
cur_user varchar(30);
BEGIN
Select current_user into cur_user;
IF cur_user != 'user1' THEN
RAISE EXCEPTION 'You cannot delete records from this table!';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
触发器阻止删除 user1 以外的用户的表记录,这很好,但它不适用于用户 user1。我的意思是它不会删除 user1 的表记录。
我的代码哪里错了?
提前致谢。
我有一个NewSchemaSafe.sql基于项目目录创建新模式的脚本;它是从 Windows 命令行调用的,如下所示:
for %%a in (.) do set this=%%~na
-- other stuff here
psql -U postgres -d SLSM -e -v v1=%this% -f "NewSchemaSafe.sql"
Run Code Online (Sandbox Code Playgroud)
NewSchemaSafe.sql 如下:
-- NewSchemaSafe.sql
-- NEW SCHEMA SETUP
-- - checks if schema exists
-- - if yes, renames existing with current monthyear as suffix
-- NOTE: will always delete any schema with the 'rename' name (save_schema)
-- since any schema thus named must have resulted from this script
-- on this date - …Run Code Online (Sandbox Code Playgroud) 在我的数据库中有许多文本列,其中值是空字符串 ( '')。空字符串需要设置为NULL. 我不知道这个数据库中的确切模式、表和列,或者我想编写一个可以重用的通用解决方案。
我将如何编写查询/函数来查找所有模式中所有表中的所有文本列,并将所有带有空字符串 ( '') 的列更新为NULL?
你可以做这样的事情:
foreach m slice 1 IN array NEW.*
loop
RAISE NOTICE 'var: %', m;
END LOOP;
Run Code Online (Sandbox Code Playgroud)
或者,
FOR i IN NEW LOOP
RAISE NOTICE 'var: %', i;
END LOOP
Run Code Online (Sandbox Code Playgroud) 我有一个表(Id、FK、Date1、Date2),我需要创建一个返回布尔值的函数。通常我想选择与给定 FK 匹配的记录并检查一些附加语句。如果 select 语句返回 0 行,我想返回 true,否则返回 false。我已经写了这段代码:
CREATE FUNCTION isAlreadyTaken(FK INT4, Date1 DATE, Date2 DATE)
RETURNS BOOLEAN
AS $$
BEGIN
CREATE TEMP TABLE helper ON COMMIT DROP AS SELECT COUNT(table.FK) AS quant
FROM table
WHERE table.FK = FK AND table.Date2 IS NULL;
SELECT CASE
WHEN helper.quant > 0
THEN FALSE
ELSE TRUE
END;
END
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
代码正在编译,可以执行。但是当我在 CHECK 语句中调用该函数时,它返回一个错误 [42P01]。解决方案是否接近实际工作的解决方案,或者我已经完全迷失了方向?
将类型(通过强制转换)从 bigint 更改为 text 后,我的 PLPGSQL 函数停止工作。这是我得到的错误:
dev=> select * from app.get_companies(4,808739954140037) ;
NOTICE: Data rows were NOT found (structure of query does not match function result type)
company_id_str | company_name
----------------+--------------
(0 rows)
dev=>
Run Code Online (Sandbox Code Playgroud)
这是我的功能:
CREATE OR REPLACE FUNCTION app.get_companies(ident_id bigint,sess bigint)
RETURNS TABLE(company_id_str text,company_name text) as $$
DECLARE
server_session bigint;
BEGIN
select app.session.session from app.session where app.session.identity_id=ident_id and app.session.session=sess into server_session;
IF FOUND
THEN
BEGIN
RETURN QUERY SELECT quote_ident(app.company.company_id::text)::text as company_id_str,app.company.name as company_name FROM app.company,app.identcomp WHERE app.company.company_id=app.identcomp.company_id and …Run Code Online (Sandbox Code Playgroud) 我正在运行 PostgreSQL 和 NodeJS。
在 PostgreSQL 中,我有一个自定义函数dummy:
... RETURNS RECORD AS $$
...
DECLARE ret RECORD
...
RETURN ret;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
在 NodeJS 中,这将返回一个tuple, 作为 a string:
(x,y,z)
Run Code Online (Sandbox Code Playgroud)
所以我必须手动拆分字符串并读取部分......是否有可能让 PostgreSQL 返回tuplearow或类似的,所以我可以在 NodeJS 中使用data.x,data.y和data.z?
我在表中有 30 个数字数字列。我想为表中的所有列找到平均值、标准和百分位数。我不想像下面那样手动编写所有列名
select date,
avg(col1), stddev(col1),
avg(col2), stddev(col2),
from table name group by date;
Run Code Online (Sandbox Code Playgroud)
有什么方法可以一次找到所有列的均值、标准差和百分位数。
我正在使用 postgres 函数在表中插入多行。我必须返回插入行的所有 id。
我的功能如下
CREATE OR REPLACE FUNCTION insert_multiple_rows(temp integer)
RETURNS integer[]
LANGUAGE 'plpgsql'
VOLATILE
PARALLEL UNSAFE
COST 100
AS $BODY$DECLARE
company_ids_list integer[];
BEGIN
INSERT INTO company VALUES
(default,now(),temp),
(default,now(),temp),
(default,now(),temp),
(default,now(),temp)
RETURNING id INTO company_ids_list;
RETURN company_ids_list;
END;
$BODY$;
Run Code Online (Sandbox Code Playgroud)
当我尝试使用
select insert_multiple_rows(58);
Run Code Online (Sandbox Code Playgroud)
出错
ERROR: query returned more than one row
CONTEXT: PL/pgSQL function create_company_budget_allocation(integer)
line 4 at SQL statement
SQL state: P0003
Run Code Online (Sandbox Code Playgroud) plpgsql ×10
postgresql ×10
sql ×5
arrays ×1
command-line ×1
dynamic-sql ×1
function ×1
greenplum ×1
node.js ×1
psql ×1
select-case ×1
triggers ×1