Postgresql update query syntax error in plpgsql function - postgresql

I have to create a update procedure, so i created a function and given only below query
CREATE FUNCTION public.testf()
RETURNS boolean
LANGUAGE 'plpgsql'
AS $BODY$begin
update tt pp
set status = 'Ok'
return true;
end;$BODY$;
ALTER FUNCTION public.testf()
OWNER TO postgres;
returns error
ERROR: syntax error at or near "UPDATE" LINE 5: AS $BODY$UPDATE
return type I have given is int4range
What I am doing wrong
Please help
THanks

Pulling out my crystal ball, I see:
You created the function with LANGUAGE plpgsql.
You didn't surround the function body with BEGIN ... END;.
If your function is just a single SQL statement, use LANGUAGE sql.
Else, use the block-centered PL/pgSQL syntax properly.

You are missing an ; to end the UPDATE statement.
CREATE FUNCTION public.testf()
RETURNS boolean
LANGUAGE plpgsql
AS $BODY$
begin
update tt pp
set status = 'Ok'; --<< here
return true;
end;
$BODY$;

Related

How to add value in enum in plpgsql function

So I have some simple function that should add value to an existing enum "profession" but instead of working it shows error
CREATE FUNCTION add_prof(p text) RETURNS VOID AS $$
BEGIN
ALTER TYPE profession ADD VALUE p;
RETURN;
END;
$$ LANGUAGE plpgsql;
The error is "[42601] syntax error". Btw, DataGrip shows that "string or IF expected but p."
Changing p to ' ' of course works fine but that's not what I need.
ALTER command is DDL command, and DDL commands doesn't allow an parametrization (they has not an execution plan). You need to use dynamic SQL:
CREATE FUNCTION add_prof(p text)
RETURNS VOID AS $$
BEGIN
EXECUTE format('ALTER TYPE profession ADD VALUE %L', p);
RETURN;
END;
$$ LANGUAGE plpgsql;

Function's final statement must be SELECT or INSERT UPDATE DELETE RETURNING

CREATE FUNCTION retrieve_add_friends(user_id text[])
RETURNS SETOF user_rows AS
$BODY$
BEGIN
FOR user_rows IN EXECUTE SELECT * FROM user_details where user_id= $1
LOOP
FOR user_friends IN EXECUTE SELECT * FROM user_add_friends where user_id= $1
LOOP
IF user_rows.user_id!=user_friends.user_friend_id THEN
RETURN NEXT user_rows;
END IF;
END LOOP;
RETURN;
END LOOP;
RETURN;
END
$BODY$
language plpgsql VOLATILE;
When I execute this I get following error:
ERROR: return type mismatch in function declared to return user_details
DETAIL: Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.
CONTEXT: SQL function "retrieve_add_friends"
Can anyone help me out with this?
The function displayed is a PL/pgSQL function.
You are calling a different function, an SQL function, obviously (for which the error msg would make sense):
SQL function "retrieve_add_friends"
Same function name, but different arguments (and possibly in a different database schema). Are you aware of function overloading and its implications?
Related:
ERROR: function addgeometrycolumn is not unique
For a quick diagnosis:
SELECT oid::regprocedure AS function_signature, *
FROM pg_proc
WHERE proname = 'retrieve_add_friends';
All that aside, the function displayed has multiple errors and can be replaced with plain SELECT.

Trigger run after hibernate insert returning error [duplicate]

Is it possible to cancel previous operations in a user defined function?
For example:
CREATE OR REPLACE FUNCTION transact_test () RETURNS BOOLEAN
AS $$
BEGIN
UPDATE table1 SET ...
UPDATE table2 SET ...
IF some_condition THEN
--Here is possible to cancel all above operations?
RETURN FALSE;
END IF;
RETURN TRUE;
END;
$$
LANGUAGE plpgsql;
Both answers so far are incorrect.
If you try to start a transaction or use a SAVEPOINT inside a plpgsql function you get an error message like this:
ERROR: cannot begin/end transactions in PL/pgSQL
HINT: Use a BEGIN block with an EXCEPTION clause instead.
CONTEXT: PL/pgSQL function "f_savepoint" line 6 at SQL statement
If you try a SAVEPOINT inside a plain SQL function:
ERROR: SAVEPOINT is not allowed in a SQL function
CONTEXT: SQL function "f_savepoint2" during startup
As the error message instructs, use a BEGIN block inside a plpgsql function instead. Your demo could look like this:
CREATE OR REPLACE FUNCTION transact_test(boolean)
RETURNS boolean AS
$func$
BEGIN -- start a nested BEGIN block
UPDATE t SET i = i+1 WHERE i = 1;
UPDATE t SET i = i+1 WHERE i = 3;
IF $1 THEN
RAISE EXCEPTION 'foo'; -- cancels all of the above
END IF;
RETURN TRUE;
EXCEPTION WHEN OTHERS THEN
RETURN FALSE;
-- do nothing
END
$func$ LANGUAGE plpgsql;
-> SQLfiddle demonstrating it all.

Postgres - ERROR: syntax error at or near "IF"

I have a function and I need to do different select based on inputid. so I have added the condition as below.
....
begin
IF(iv_binrocess = 1 AND IsNull(iv_binno,'') != '') THEN
...//some process here
END If;
...
End
When I run the function, I got the error "
ERROR: syntax error at or near "IF"
I referred many sites and tutorials, nothing helped me.
Can anyone help me to fix this issue?
Thanks
WAG, since you deleted most of the important information: You're trying to create a function with a PL/pgsql body, but declaring it SQL.
craig=> CREATE FUNCTION test() RETURNS void LANGUAGE sql AS
$$ BEGIN IF TRUE THEN PERFORM 1; END IF; END; $$ ;
ERROR: syntax error at or near "IF"
LINE 1: ...TION test() RETURNS void LANGUAGE sql AS $$ BEGIN IF TRUE TH...
Declare PL/PgSQL functions as LANGUAGE plpgsql.
If you instead want to use an SQL function, use a CASE expression instead of IF.
CREATE FUNCTION test2() RETURNS integer LANGUAGE sql
AS $$ SELECT CASE WHEN TRUE THEN 1 ELSE 0 END; $$;
(Note, I haven't bothered to format these readably. Don't write functions all in one line like this in real code.)

How to use EXECUTE FORMAT ... USING in postgres function

CREATE OR REPLACE FUNCTION dummytest_insert_trigger()
RETURNS trigger AS
$BODY$
DECLARE
v_partition_name VARCHAR(32);
BEGIN
IF NEW.datetime IS NOT NULL THEN
v_partition_name := 'dummyTest';
EXECUTE format('INSERT INTO %I VALUES ($1,$2)',v_partition_name)using NEW.id,NEW.datetime;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION dummytest_insert_trigger()
OWNER TO postgres;
I'm trying to insert using
insert into dummyTest values(1,'2013-01-01 00:00:00+05:30');
But it's showing error as
ERROR: function format(unknown) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Context: PL/pgSQL function "dummytest_insert_trigger" line 8 at EXECUTE statement
I'm unable get the error.
Your function could look like this in Postgres 9.0 or later:
CREATE OR REPLACE FUNCTION dummytest_insert_trigger()
RETURNS trigger AS
$func$
DECLARE
v_partition_name text := quote_ident('dummyTest'); -- assign at declaration
BEGIN
IF NEW.datetime IS NOT NULL THEN
EXECUTE
'INSERT INTO ' || v_partition_name || ' VALUES ($1,$2)'
USING NEW.id, NEW.datetime;
END IF;
RETURN NULL; -- You sure about this?
END
$func$ LANGUAGE plpgsql;
About RETURN NULL:
To ignore result in BEFORE TRIGGER of PostgreSQL?
I would advice not to use mixed case identifiers. With format( .. %I ..) or quote_ident(), you'd get a table named "dummyTest", which you'll have to double quote for the rest of its existence. Related:
Are PostgreSQL column names case-sensitive?
Use lower case instead:
quote_ident('dummytest')
There is really no point in using dynamic SQL with EXECUTE as long as you have a static table name. But that's probably just the simplified example?
You need explicit cast to text:
EXECUTE format('INSERT INTO %I VALUES ($1,$2)'::text ,v_partition_name) using NEW.id,NEW.datetime;