如何在Postgresql中编写更新函数(存储过程)?
我想在postgresql中创建一个如何在Postgresql中编写更新函数(存储过程)?,postgresql,Postgresql,我想在postgresql中创建一个更新函数(存储过程),我在google上搜索了很多次,但没有找到一个合适的更新函数(存储过程)示例。如何在Postgresql中编写更新函数并更改表中的现有数据 提前谢谢 Example of Function CREATE OR REPLACE FUNCTION updateuser_login(userloginidp integer, usercategoryidf integer, usertypeidf integer, usertyperefer
更新函数
(存储过程),我在google上搜索了很多次,但没有找到一个合适的更新函数(存储过程)示例。如何在Postgresql中编写更新函数并更改表中的现有数据
提前谢谢
Example of Function
CREATE OR REPLACE FUNCTION updateuser_login(userloginidp integer, usercategoryidf integer, usertypeidf integer, usertypereferenceidf integer, loginname text, loginpassword text, menutypeidf integer, username text, dashboardconfig text, careprovideridf integer, isactive boolean)
RETURNS void AS
$BODY$BEGIN
UPDATE tbuserlogin
SET usercategoryidf="@usercategoryidf",
usetypeidf="@usertypeidf",
usertypereferenceidf="@usertypereferenceidf",
loginname="@loginname",
loginpassword="@loginpassword",
menutypeidf="@menutypeidf",
username="@username",
dashboardconfig="@dashboardconfig",
careprovideridf="@careprovideridf",
isactive="@isactive"
WHERE userloginidp = "@userloginidp";
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION updateuser_login(integer, integer, integer, integer, text, text, integer, text, text, integer, boolean)
OWNER TO postgres;
您可以在PGXN站点的源代码中找到此类内容的优秀示例: 用户sql文件中的示例:
CREATE OR REPLACE FUNCTION update_user(
nickname LABEL,
full_name TEXT DEFAULT NULL,
email EMAIL DEFAULT NULL,
uri URI DEFAULT NULL,
twitter CITEXT DEFAULT NULL
) RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$
/*
% SELECT update_user(
nickname := 'theory',
full_name := 'David E. Wheeler',
email := 'justatheory@pgxn.org',
uri := 'http://www.justatheory.com/',
twitter :- 'theory'
);
update_user
─────────────
t
Update the specified user. The user must be active. The nickname cannot be
changed. The password can only be changed via `change_password()` or
`reset_password()`. Pass other attributes as:
full_name
: The full name of the user.
email
: The email address of the user. Must be a valid email address as verified by
[Email::Valid](http://search.cpan.org/perldoc?Email::Valid).
uri
: Optional URI for the user. Should be a valid URI as verified by
[Data::Validate::URI](http://search.cpan.org/perldoc?Data::Validate::URI).
twitter
: Optional Twitter username. A leading "@" wil be removed.
Returns true if the user was updated, and false if not.
*/
BEGIN
UPDATE users
SET full_name = COALESCE(update_user.full_name, users.full_name),
email = COALESCE(update_user.email, users.email),
uri = COALESCE(update_user.uri, users.uri),
twitter = COALESCE(trim(leading '@' FROM update_user.twitter), users.twitter),
updated_at = NOW()
WHERE users.nickname = update_user.nickname
AND users.status = 'active';
RETURN FOUND;
END;
$$;
Postgresql读取双引号作为列的名称。。将其替换为单引号,也可以只使用参数
创建或替换函数updateuser\u login(userloginidp integer,
usercategoryidf integer、usertypeidf integer、usertypereferenceidf
整数、loginname文本、loginpassword文本、menutypeidf整数、,
用户名文本、仪表板配置文本、careprovideridf整数、isactive
boolean)返回void作为
$BODY$
开始
更新tbuserlogin
设置usercategoryidf='@usercategoryidf',
usetypeidf='@usertypeidf',
usertypereferenceidf='@usertypereferenceidf',
loginname='@loginname',
loginpassword='@loginpassword',
menutypeidf='@menutypeidf',
用户名=“@username”,
dashboardconfig='@dashboardconfig',
careprovideridf='@careprovideridf',
isactive='@isactive'
其中userloginidp='@userloginidp';
结束;
$BODY$
语言plpgsql VOLATILE
成本100;
ALTER FUNCTION updateuser_login(整型、整型、整型、整型、文本、文本、整型、文本、文本、整型、布尔型)
博士后的所有者;
什么是升级功能?我不知道这个术语。这个函数应该做什么?在PostgreSQL中调用函数的一种类型的存储过程我仍然不明白-你能给出一些例子吗?@Pavel现在你看到在我的问题中,我用PostgreSQL术语给出了函数的例子,它是带有嵌入式SQL语句的void标量函数。它可以用SQL或PL/pgSQL语言实现。“更新函数”的名称不常见。@IrenPatel:这是Postgres。请参阅语言:plpgsql
。(David Wheeler是Postgres的贡献者之一。)@IrenPatel同样,PostgreSQL是一个SQL数据库。我猜你所说的“SQL”是指“Microsoft SQL Server”。最好在这里这样说,“SQL”只是一种语言,所以当你说“SQL”是指“MS-SQL”时,你会把人搞糊涂@Denis我试着在我的演示函数中给你提个建议,但当我在我的主函数中试着这样做时,那次会产生错误“missing FROM子句entry for table”,对此有什么解决办法。@Irenpael:使用最新的Postgres?如果David,在所有人中,有一个repo中的源代码不会在Postgres上运行,我会感到惊讶