如何调用函数,PostgreSQL
我正在尝试使用 PostgreSQL 的函数来保存一些数据。这是创建脚本:
-- Function: "saveUser"(integer, character varying, character varying, character varying, character varying, character varying)
-- DROP FUNCTION "saveUser"(integer, character varying, character varying, character varying, character varying, character varying);
CREATE OR REPLACE FUNCTION "saveUser"("pUserID" integer, "pName" character
varying, "pLastName" character varying, "pUserName" character varying,
"pPassword" character varying, "peMail" character varying)
RETURNS boolean AS
$BODY$
BEGIN
SELECT 1
FROM "USERS"
WHERE "userID" = $1;
IF FOUND THEN
UPDATE "USERS"
SET "name" = $2,
"lastName" = $3,
"userName" = $4,
"password" = $5,
"eMail" = $6
WHERE "userID" = $1;
ELSE
INSERT INTO "USERS"
("name", "lastName", "userName", "password", "eMail")
VALUES
($2, $3, $4, $5, $6);
END IF;
END;$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION "saveUser"(integer, character varying, character varying, character varying, character varying, character varying) OWNER TO postgres;
PostreSQL 文档指出,要调用不返回任何结果集的函数,只需编写其名称和属性就足够了。所以我尝试像这样调用该函数:
"saveUser"(3, 'asd','asd','asd','asd','asd');
但出现以下错误:
ERROR: syntax error at or near ""saveUser""
LINE 1: "saveUser"(3, 'asd','asd','asd','asd','asd')
^
********** Error **********
ERROR: syntax error at or near ""saveUser""
SQL state: 42601
Character: 1
我还有其他返回结果集的函数。我使用 SELECT * FROM "fnc"(...) 来调用它们并且它有效。为什么我会收到此错误?
编辑:我正在使用 pgAdmin III 查询工具并尝试在那里执行 SQL 语句。
I'm trying to use a function with PostgreSQL to save some data. Here is the create script:
-- Function: "saveUser"(integer, character varying, character varying, character varying, character varying, character varying)
-- DROP FUNCTION "saveUser"(integer, character varying, character varying, character varying, character varying, character varying);
CREATE OR REPLACE FUNCTION "saveUser"("pUserID" integer, "pName" character
varying, "pLastName" character varying, "pUserName" character varying,
"pPassword" character varying, "peMail" character varying)
RETURNS boolean AS
$BODY$
BEGIN
SELECT 1
FROM "USERS"
WHERE "userID" = $1;
IF FOUND THEN
UPDATE "USERS"
SET "name" = $2,
"lastName" = $3,
"userName" = $4,
"password" = $5,
"eMail" = $6
WHERE "userID" = $1;
ELSE
INSERT INTO "USERS"
("name", "lastName", "userName", "password", "eMail")
VALUES
($2, $3, $4, $5, $6);
END IF;
END;$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION "saveUser"(integer, character varying, character varying, character varying, character varying, character varying) OWNER TO postgres;
PostreSQL Documentation states that to call a function which does not return any resultset, it is sufficient to write only its name and properties. So I try to call the function like this:
"saveUser"(3, 'asd','asd','asd','asd','asd');
But I get the error below:
ERROR: syntax error at or near ""saveUser""
LINE 1: "saveUser"(3, 'asd','asd','asd','asd','asd')
^
********** Error **********
ERROR: syntax error at or near ""saveUser""
SQL state: 42601
Character: 1
I have other functions which return a resultset. I use SELECT * FROM "fnc"(...)
to call them and it works. Why am I getting this error?
EDIT: I am using pgAdmin III Query tool and trying to execute the SQL Statements there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
函数调用仍然应该是有效的 SQL 语句:
The function call still should be a valid SQL statement:
对于 Postgresql,您可以使用 执行。 PERFORM 仅在 PL/PgSQL 过程语言中有效。
postgres团队的建议:
For Postgresql you can use PERFORM. PERFORM is only valid within PL/PgSQL procedure language.
The suggestion from the postgres team:
我们可以有两种方法来调用 pgadmin 中为 postgre sql 数据库编写的函数。
假设我们定义了该函数如下:
我们可以通过以下方式之一调用函数 helloworld:
We can have two ways of calling the functions written in pgadmin for postgre sql database.
Suppose we have defined the function as below:
We can call the function helloworld in one of the following way:
如果您的函数不想返回任何内容,您应该将其声明为“return void”,然后您可以像这样调用它“perform functionName(parameter...);”
if your function does not want to return anything you should declare it to "return void" and then you can call it like this "perform functionName(parameter...);"
我在尝试测试一个非常相似的函数时遇到了同样的问题,该函数使用 SELECT 语句来决定是否应该执行 INSERT 或 UPDATE。该函数是 T-SQL 存储过程的重写。
当我从查询窗口测试该函数时,出现错误“查询没有结果数据的目的地”。我最终发现,因为我在函数内使用了 SELECT 语句,所以在使用 INTO 语句将 SELECT 的结果分配给局部变量之前,我无法从查询窗口测试该函数。这解决了问题。
如果该线程中的原始函数更改为以下内容,则从查询窗口调用时它将起作用,
I had this same issue while trying to test a very similar function that uses a SELECT statement to decide if a INSERT or an UPDATE should be done. This function was a re-write of a T-SQL stored procedure.
When I tested the function from the query window I got the error "query has no destination for result data". I finally figured out that because I used a SELECT statement inside the function that I could not test the function from the query window until I assigned the results of the SELECT to a local variable using an INTO statement. This fixed the problem.
If the original function in this thread was changed to the following it would work when called from the query window,
您将函数声明为返回布尔值,但它从不返回任何内容。
you declare your function as returning boolean, but it never returns anything.