在存储过程 postgres 中创建自定义列
Creating Custom columns in stored procedure postgres
我正在使用存储过程来 return 在我的大学注册的学生类型。将他们的 ID 推送到将要创建的新列中 return 他们的名字和姓氏(例如:通勤者、雇员、居民)。我一直收到错误消息:
ERROR: syntax error at or near "if"
LINE 8: if exists (select count(commuterid) > 0 from commuter wh...).
有什么建议或想法吗?
create or replace function roleAtMarist(int, REFCURSOR) returns refcursor as
$$
declare
identifier int := ;
resultset refcursor := ;
begin
open resultset for
if exists (select count(commuterid) > 0 from commuter where commuterid = identifier) then
select fname, lname, "Commuter" as Role
from people
where peopleid = identifier;
end if;
if exists (select count(employeeid) > 0 from employee where emplpoyeeid = identifier) then
select fname, lname, "Employee" as Role
from people
where peopleid = identifier;
end if;
if exists (select count(residentid) > 0 from studentpark where residentid = identifier) then
select fname, lname, "Resident" as Role
from people
where peopleid = identifier;
end if;
return resultset;
end;
$$
language plpgsql;
select roleAtMarist(12, 'resultset') ;
fetch all from results ;
这在很多方面都是倒退的。游标将使用有效的 SQL 而不是 plpgsql 命令。但是您不需要游标,也不需要 plpgsql 开始。一个简单的 SQL 函数应该做:
CREATE OR REPLACE FUNCTION role_at_marist(_identifier int)
RETURNS TABLE (fname text, lname text, "role" text) AS
$func$
SELECT p.fname, p.lname, text 'Commuter'
FROM people
WHERE p.peopleid =
AND EXISTS (SELECT 1 FROM commuter c WHERE c.commuterid = p.peopleid)
UNION ALL
SELECT p.fname, p.lname, 'Employee'
FROM people
WHERE p.peopleid =
AND EXISTS (SELECT 1 FROM employee e WHERE e.emplpoyeeid = p.peopleid)
UNION ALL
SELECT p.fname, p.lname, 'Resident'
FROM people
WHERE p.peopleid =
AND EXISTS (SELECT 1 FROM studentpark s WHERE s.residentid = p.peopleid)
$func$ LANGUAGE sql;
致电:
SELECT * FROM role_at_marist(12);
集合返回函数可以像 FROM
列表中的表一样使用。
字符串文字用单引号括起来!
我正在使用存储过程来 return 在我的大学注册的学生类型。将他们的 ID 推送到将要创建的新列中 return 他们的名字和姓氏(例如:通勤者、雇员、居民)。我一直收到错误消息:
ERROR: syntax error at or near "if" LINE 8: if exists (select count(commuterid) > 0 from commuter wh...).
有什么建议或想法吗?
create or replace function roleAtMarist(int, REFCURSOR) returns refcursor as
$$
declare
identifier int := ;
resultset refcursor := ;
begin
open resultset for
if exists (select count(commuterid) > 0 from commuter where commuterid = identifier) then
select fname, lname, "Commuter" as Role
from people
where peopleid = identifier;
end if;
if exists (select count(employeeid) > 0 from employee where emplpoyeeid = identifier) then
select fname, lname, "Employee" as Role
from people
where peopleid = identifier;
end if;
if exists (select count(residentid) > 0 from studentpark where residentid = identifier) then
select fname, lname, "Resident" as Role
from people
where peopleid = identifier;
end if;
return resultset;
end;
$$
language plpgsql;
select roleAtMarist(12, 'resultset') ;
fetch all from results ;
这在很多方面都是倒退的。游标将使用有效的 SQL 而不是 plpgsql 命令。但是您不需要游标,也不需要 plpgsql 开始。一个简单的 SQL 函数应该做:
CREATE OR REPLACE FUNCTION role_at_marist(_identifier int)
RETURNS TABLE (fname text, lname text, "role" text) AS
$func$
SELECT p.fname, p.lname, text 'Commuter'
FROM people
WHERE p.peopleid =
AND EXISTS (SELECT 1 FROM commuter c WHERE c.commuterid = p.peopleid)
UNION ALL
SELECT p.fname, p.lname, 'Employee'
FROM people
WHERE p.peopleid =
AND EXISTS (SELECT 1 FROM employee e WHERE e.emplpoyeeid = p.peopleid)
UNION ALL
SELECT p.fname, p.lname, 'Resident'
FROM people
WHERE p.peopleid =
AND EXISTS (SELECT 1 FROM studentpark s WHERE s.residentid = p.peopleid)
$func$ LANGUAGE sql;
致电:
SELECT * FROM role_at_marist(12);
集合返回函数可以像 FROM
列表中的表一样使用。
字符串文字用单引号括起来!