Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function, PostgreSQL

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.

like image 653
Erkan Haspulat Avatar asked Dec 23 '09 15:12

Erkan Haspulat


People also ask

How do you run a function in pgAdmin 4?

Use the drop-down listbox in the Data type field to select a data type. Use the drop-down listbox in the Mode field to select a mode. Select IN for an input parameter; select OUT for an output parameter; select INOUT for both an input and an output parameter; or, select VARIADIC to specify a VARIADIC parameter.

How do you open a function in pgAdmin?

When you click on the functions and select a function you can see the function's code.In pgAdmin 4 I don't see the tab for functions under schema.

What is now () in PostgreSQL?

The PostgreSQL now function returns the current date and time with the time zone.

What are functions in PostgreSQL?

What is the PostgreSQL Function? A PostgreSQL function or a stored procedure is a set of SQL and procedural commands such as declarations, assignments, loops, flow-of-control etc. stored on the database server and can be involved using the SQL interface. And it is also known as PostgreSQL stored procedures.


2 Answers

The function call still should be a valid SQL statement:

SELECT "saveUser"(3, 'asd','asd','asd','asd','asd'); 
like image 156
Milen A. Radev Avatar answered Sep 28 '22 05:09

Milen A. Radev


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.

like image 36
itsnikolay Avatar answered Sep 28 '22 04:09

itsnikolay