如何调用函数,PostgreSQL

发布于 2024-08-15 19:01:59 字数 1642 浏览 8 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

似最初 2024-08-22 19:01:59

函数调用仍然应该是有效的 SQL 语句:

SELECT "saveUser"(3, 'asd','asd','asd','asd','asd');

The function call still should be a valid SQL statement:

SELECT "saveUser"(3, 'asd','asd','asd','asd','asd');
疏忽 2024-08-22 19:01:59

对于 Postgresql,您可以使用 执行。 PERFORM 仅在 PL/PgSQL 过程语言中有效。

DO $ BEGIN
    PERFORM "saveUser"(3, 'asd','asd','asd','asd','asd');
END $;

postgres团队的建议:

提示:如果您想放弃 SELECT 的结果,请使用 PERFORM。

For Postgresql you can use PERFORM. PERFORM is only valid within PL/PgSQL procedure language.

DO $ BEGIN
    PERFORM "saveUser"(3, 'asd','asd','asd','asd','asd');
END $;

The suggestion from the postgres team:

HINT: If you want to discard the results of a SELECT, use PERFORM instead.

眼泪都笑了 2024-08-22 19:01:59

我们可以有两种方法来调用 pgadmin 中为 postgre sql 数据库编写的函数。

假设我们定义了该函数如下:

CREATE OR REPLACE FUNCTION helloWorld(name text) RETURNS void AS $helloWorld$
DECLARE
BEGIN
    RAISE LOG 'Hello, %', name;
END;
$helloWorld$ LANGUAGE plpgsql;

我们可以通过以下方式之一调用函数 helloworld:

SELECT "helloworld"('myname');

SELECT public.helloworld('myname')

We can have two ways of calling the functions written in pgadmin for postgre sql database.

Suppose we have defined the function as below:

CREATE OR REPLACE FUNCTION helloWorld(name text) RETURNS void AS $helloWorld$
DECLARE
BEGIN
    RAISE LOG 'Hello, %', name;
END;
$helloWorld$ LANGUAGE plpgsql;

We can call the function helloworld in one of the following way:

SELECT "helloworld"('myname');

SELECT public.helloworld('myname')
七度光 2024-08-22 19:01:59

如果您的函数不想返回任何内容,您应该将其声明为“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...);"

走走停停 2024-08-22 19:01:59

我在尝试测试一个非常相似的函数时遇到了同样的问题,该函数使用 SELECT 语句来决定是否应该执行 INSERT 或 UPDATE。该函数是 T-SQL 存储过程的重写。
当我从查询窗口测试该函数时,出现错误“查询没有结果数据的目的地”。我最终发现,因为我在函数内使用了 SELECT 语句,所以在使用 INTO 语句将 SELECT 的结果分配给局部变量之前,我无法从查询窗口测试该函数。这解决了问题。

如果该线程中的原始函数更改为以下内容,则从查询窗口调用时它将起作用,

$BODY$
DECLARE
   v_temp integer;
BEGIN
SELECT 1 INTO v_temp
FROM "USERS"
WHERE "userID" = $1;

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,

$BODY$
DECLARE
   v_temp integer;
BEGIN
SELECT 1 INTO v_temp
FROM "USERS"
WHERE "userID" = $1;
_失温 2024-08-22 19:01:59

您将函数声明为返回布尔值,但它从不返回任何内容。

you declare your function as returning boolean, but it never returns anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文