I am trying to create a function in postgres 9.4 using psql as follows:
CREATE OR REPLACE FUNCTION function_name()
RETURNS VOID AS $$
DECLARE
et_ag "table_name"."ismask"%TYPE;
BEGIN
SET SEARCH_PATH = overlay;
SELECT ismask INTO et_ag FROM OVERLAY.table_name WHERE component_name = 'et_ag';
SELECT case when et_ag != 0 then 'et_ag is not 0' else 'et_ag is 0' end;
END;
$$ LANGUAGE plpgsql;
On running this procedure, I am getting the following error:
ERROR: invalid type name ""table_name"."ismask"%TYPE"
LINE 10: et_ag "table_name"."ismask"%TYPE;
^
********** Error **********
ERROR: invalid type name ""table_name"."ismask"%TYPE"
SQL state: 42601
Character: 167
I referred this post, but am not able to resolve the error. I will greatly appreciate and let me know what wrong I am doing while creating this function.
Related
I'm learning PosgreSQL, i'm trying to call function get_color in a SELECT statement inserting the column name of the table as parameter, but PostgreSQL version 9.6 (i've tried on 13.0 with the same result) returns me an UndefinedFunction error.
Here's the complete query code:
SELECT n
FROM pianokeys pn,
LATERAL get_color(pn.n) AS res;
CREATE OR REPLACE FUNCTION get_color(n integer) RETURNS text AS $$
BEGIN
IF((n % 88) % 2)
THEN
RETURN $b$black$b$;
ELSE
RETURN $w$white$w$;
END IF;
END;
$$
LANGUAGE plpgsql;
`
Here's the compiler error:
There was an error with the SQL query:
PG::UndefinedFunction: ERROR: function get_color(integer) does not exist
LINE 8: lateral get_color(pn.n) as res) AS "t1" LIMIT 1
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
What am i missing?
I'm trying to write a sql query to pick some columns based on predetermined conditions. I removed the ELSEIF THEN following block from the code pasted below for which I get the following error:
ERROR: syntax error at or near "END" Position: 799
Could you please help?
Do $$
BEGIN
IF {{f_add_cols_check}} THEN
SELECT colA,colB
FROM"XXX"
END IF;
END;
$$
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)
https://www.db-fiddle.com/f/3dpbCYK7FeCMFcWRedBz17/2
I am trying to run my function using postgresql database.but I am getting this error
Schema Error: error: unterminated dollar-quoted string at or near "$total$ declare total integer;"
Schema Error: error: syntax error at or near "SELECT"
Schema Error: error: syntax error at or near "RETURN"
Schema Error: error: unterminated dollar-quoted string at or near "$total$ LANGUAGE plpgsql;"
Query Error: error: function totalrecords() does not exist
can I check my function online ?
CREATE TABLE test (
id INT
);
INSERT INTO test (id) VALUES (1);
INSERT INTO test (id) VALUES (2);
CREATE OR REPLACE FUNCTION totalRecords ()
RETURNS integer AS $total$
declare
total integer;
BEGIN
SELECT count(*) into total FROM test;
RETURN total;
END;
$total$ LANGUAGE plpgsql;
I want to create a table in my database.
I use postgresql.
Here is my code :
CREATE OR REPLACE FUNCTION log_last_name_changes() RETURNS trigger as $BODY$
BEGIN
IF NEW.last_name <> OLD.last_name THEN
INSERT INTO empolyee_audits(employee_id,last_name,changed_on)
values(OLD.id,OLD.last_name,now());
END IF
RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;
give me error:
ERROR: syntax error at or near "RETURN"
LINE 6: RETURN NEW;
^
********** Error **********
ERROR: syntax error at or near "RETURN"
SQL state: 42601
Character: 230
You need semicolon ; after END IF