Unterminated dollar-quoted string creating PostgreSQL function - postgresql

Using PostgreSQL 12.3, I am having some trouble trying to validate this simple chunk of plpgsql code
create or replace function test()
returns void
as $$
begin
prepare plan as select 1;
execute plan;
end;
$$ language plpgsql;
Error is
Unterminated dollar-quoted string at or near "$$ begin prepare plan as select 1;"
I have tried with and without ; after end. I have also tried with sql instead of plpgsql.
Any idea of whats is wrong?
This is a db-fiddle to quickly test the code:
https://www.db-fiddle.com/f/KgRZcxXqJs2Lwe284Mj5y/3

The issue is not with the $$ quoting:
create or replace function test()
returns void
as $$
begin
prepare plan as select 1;
execute plan;
end;
$$ language plpgsql;
CREATE FUNCTION
select test();
ERROR: column "plan" does not exist
LINE 1: SELECT plan
^
QUERY: SELECT plan
CONTEXT: PL/pgSQL function test() line 4 at EXECUTE
When you run this in the dbfiddle the full error output is:
Schema Error: error: unterminated dollar-quoted string at or near "$$ begin prepare plan as select 1;"
Schema Error: error: prepared statement "plan" does not exist
Schema Error: error: unterminated dollar-quoted string at or near "$$ language plpgsql;"
Query Error: error: function test() does not exist
The issue is that EXECUTE inside plpgsql is its own command:
https://www.postgresql.org/docs/12/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
EXECUTE command-string [ INTO [STRICT] target ] [ USING expression [, ... ] ];
I would use the plpgsql form. This works:
create or replace function test()
returns void
as $$
begin
prepare plan as select 1;
EXECUTE 'execute plan';
RAISE NOTICE 'Made it';
DEALLOCATE plan;
end;
$$ language plpgsql;
select test();
NOTICE: Made it
test
------
(1 row)

Related

psql pgadmin Procedure error column "trip" does not exist LINE 7: CALL example2(Trip)

I want to create a stored procedure in pgadmin that will output the number of rows from the "Trip" table. The table itself is output in cmd, the number of rows in this table is also output in cmd. But when writing a procedure and calling it, such an error comes out. I have psql version 15. How can I fix this error?
My code:
CREATE PROCEDURE example2(INOUT _name character varying) AS $$
BEGIN
SELECT count(*) FROM "_name";
END;
$$ LANGUAGE plpgsql;
CALL example2(Trip)
Error:
ERROR: ERROR: The "trip" column does not exist
LINE 7: CALL example2(Trip)
You are better off using a function then a procedure as it is easier to get value out. Using dynamic SQL along with the format function to properly quote the _name parameter.
CREATE OR REPLACE FUNCTION public.example2(_name character varying)
RETURNS integer
LANGUAGE plpgsql
AS $function$
DECLARE
ct integer;
BEGIN
EXECUTE format('SELECT count(*) FROM %I', _name) INTO ct;
RETURN ct;
END;
$function$
select example2('animals');
example2
----------
8

PostgreSQL using NO_DATA_FOUND exception in a sored Procedure

When I try to run a PostgreSQL stored procedure including the "STRICT" option and a record variable (rec) as I try to save (run the create statement) I get the following error:
ERROR: syntax error at or near "rec"
LINE 14: INTO STRICT rec
I' am following the guidelines stated on the official documentation https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
where the only reference missing is that this can or can't be used inside a function or a stored procedure. I guess there should be a reasonable answer to this issue I couldn't found. I was able to succeed running an equivalent code inside a Do block, so it seams there is some incompatibility declaring a record type variable and functions.
This is the function code I was running:
CREATE OR REPLACE FUNCTION test_nodata()
RETURNS TABLE(id int, active boolean)
LANGUAGE plpgsql
AS $$
DECLARE
rec record;
tab VARCHAR ='text_maintenance_news';
BEGIN
RETURN QUERY
SELECT tm.id, tm.active
INTO STRICT rec
FROM text_maintenance_news tm
WHERE tm.active
LIMIT 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE 'Query from % has no data', tab;
END;
$$
As mentioned at the official documents, postgreSQL doesn't consider no data as an error but as a warning that the programmer must handle. Main reason to include the STRICT option.
Do block code (It works!) and result:
DO
LANGUAGE plpgsql
$$
DECLARE
rec record;
tab varchar = 'text_maintenance_news';
BEGIN
SELECT *
INTO STRICT rec
FROM text_maintenance_news
WHERE active
LIMIT 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE 'Query from % return no data', tab;
END;
$$
ERROR: Query from text_maintenance_news return no data
CONTEXT: PL/pgSQL function inline_code_block line 13 at RAISE
SQL state: P0001

PostgreSQL: Create function has error

I write the function, but it occurs error " ERROR: syntax error at or near "\""
I want to create file, But it does not seem to work.
How do I do it ?
CREATE OR REPLACE FUNCTION sqlDB (fileName VARCHAR)
RETURNS void AS $$
DECLARE
BEGIN
\i /tmp/fileName;
END;
$$ language plpgsql;

'ERROR: syntax error at or near "AS"' for a PL/pgSQL function with FOR LOOP

I am getting an error for the below FOR loop with cursor in a function:
ERROR: syntax error at or near "AS"
CREATE OR REPLACE FUNCTION functionName(custom varchar(15)) RETURNS INTEGER AS $$
DECLARE
...
BEGIN
...
FOR loop AS cursor CURSOR FOR
SELECT column FROM table
DO
...
END FOR;
RETURN someValue;
END;
$$
LANGUAGE plpgsql;
This is wrong syntax - Postgres doesn't support declaration of CURSOR inside FOR statement. See documentation:
CREATE OR REPLACE FUNCTION foo()
RETURNS void AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT xx,yy FROM some_tab
LOOP
RAISE NOTICE 'row data: %', r;
END LOOP;
END;
$$ LANGUAGE plpgsql;
It looks so you are using ANSI SQL PSM syntax. PL/pgSQL is based on PL/SQL syntax (Oracle/ADA).

PostgreSQL: Select string_agg into variable

I have a string to execute with the string aggregate function within the postgresql function. Here is the following script for that.
Example:
create or replace function f(colvalue int,colnvalue varchar)
returns void as
$$
declare
sql varchar;
var varchar;
begin
sql := 'Select var:= string_agg(................) /* Error occurred here near var:= */
from tablename where cola ='|| colvalue || ' AND coln ='|| colnvalue;
raise notice '%'sql;
execute sql into var;
raise notice var;
end;
$$
language plpgsql;
Error:
ERROR: syntax error at or near ":="
Note: I want the result of string_agg into var.
Dynamic SQL can contains SQL statement only - but ":=" is a PL/pgSQL statement. Next, it is clean from your example, so it is useless there. Second issue is a SQL injection vulnerability (still this code should not work). Newer use a patter ' || varcharvar || ' for SQL used in dynamic SQL.
CREATE OR REPLACE FUNCTION f(colvalue int,colnvalue varchar)
RETURNS void AS $$
DECLARE
sql varchar;
var varchar;
BEGIN
sql := 'SELECT string_agg(..) FROM tablename WHERE cola=$1 AND coln=$2';
RAISE NOTICE '%', sql;
EXECUTE sql INTO var USING colvalue, colnvalue;
RAISE NOTICE '%', var;
END;
$$ LANGUAGE plpgsql;