PostgreSQL: Create function has error - postgresql

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;

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;

Unterminated dollar-quoted string creating PostgreSQL function

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)

How to concat two string in postgresql function?

I want a function which will return concated string. I am getting following error after execute this function in postgresql.
CREATE OR REPLACE FUNCTION getTableName ()
RETURNS text AS $$
DECLARE
state_short_name text;
BEGIN
state_short_name := (select lower(state_short_name) from mst_state where state_code in (SELECT substr(entity_code,1,2) FROM shg_detail_share WHERE entity_code = '3420006002001'))
RETURN (CONCAT(state_short_name, '_shg_detail'));
END;
$$ LANGUAGE plpgsql
I expect the output like 'jh_shg_detail' but I am getting error like this
ERROR: syntax error at or near "("
LINE 9: RETURN (CONCAT(state_short_name, '_shg_detail'));
You should use a select into in PL/pgSQL. And to avoid a name clash, don't name variables the same as columns:
CREATE OR REPLACE FUNCTION gettablename()
RETURNS text AS $$
DECLARE
l_state_short_name text;
BEGIN
select lower(state_short_name)
into l_state_short_name
from mst_state
where state_code in (SELECT substr(entity_code,1,2)
FROM shg_detail_share
WHERE entity_code = '3420006002001'));
RETURN CONCAT(state_short_name, '_shg_detail');
END;
$$ LANGUAGE plpgsql;
But you don't need PL/pgSQL for a simple SQL query like that. Your sub-query isn't really necessary as well. You can simplify that to where state_code = '34'
CREATE OR REPLACE FUNCTION gettablename()
RETURNS text
AS $$
select concat(lower(state_short_name), '_shg_detail')
from mst_state
where state_code = '34';
$$
LANGUAGE sql;
Your problem is a missing semicolon at the line with the assignment statement :=.
This makes the line that starts with RETURN (CONCAT a continuation line of the statement, and so you get the syntax error reported in that line.

Dollar-quotes failing with JDBC

I have to write a function in PLPGSQL but I have problem with the function body quoted with dollar-quoting. Using the first tutorial:
CREATE FUNCTION inc(val integer)
RETURNS integer AS
$BODY$
BEGIN
RETURN val + 1;
END;
$BODY$
LANGUAGE PLPGSQL;
I get an error:
unterminated dollar-quoted string at or near $$
Searching on google I just found it's a JDBC Driver problem but I cannot update it.
So I have tried to change the DELIMITER to remove $$:
DELIMITER ++;
CREATE FUNCTION inc(val integer)
RETURNS integer AS
++BODY++
BEGIN
RETURN val + 1;
END;
++BODY++
LANGUAGE PLPGSQL;
DELIMITER ;
The command doesn't return any error but function doesn't exists when I try to call it:
select inc(4);
What am I missing?
The underlying problem is JDBC's inability to deal with dollar-quotes.
I think this was fixed with JDBC version 9.4.1208 (2016-02-16).
See:
Exceptions when creating a trigger in PostgreSQL 9.1
How to execute plpgsql anonymous block in Oracle SQL Developer?
You can avoid the problem by using plain quotes for the simple case:
CREATE FUNCTION inc(val integer)
RETURNS integer AS
'
BEGIN
RETURN val + 1;
END
' LANGUAGE plpgsql;
See:
What are '$$' used for in PL/pgSQL
Insert text with single quotes in PostgreSQL

syntax error when running postgresql function/stored procedure

Trying to perform a set of complex Postgresql DB operations by using function, but even a simple function gets me error:
ERROR: syntax error at or near "text"
LINE 3: tmp text := info;
^
Here is SQL
CREATE or REPLACE FUNCTION createme (info text) RETURNS text AS $$
DECLARE
tmp text := info;
BEGIN
select :tmp
end
$$ LANGUAGE SQL;
Any idea why? Thx!
You procedure is not in SQL language, but it is in plpgsql language.
CREATE or REPLACE FUNCTION createme (info text) RETURNS text AS $$
DECLARE
tmp text := info;
BEGIN
RETURN tmp;
end
$$ LANGUAGE plpgsql;
SELECT :tmp is nonsense in this content. Functions returns a value with command RETURN - it is similar to any other environments.