I'm very new in postgresql. I read many posts in this questions, but still don't get correct answer in my simple problem and keep receiving syntax error. I'm trying declare new string variable named parents_d and in the following lines trying to assign new value as well. Please help me!
CREATE OR REPLACE FUNCTION retrieve_parents(cid integer) RETURNS text AS $$
BEGIN
DECLARE pd text;
pd:= 'function';
RETURN concat(cid,pd);
END;
$$ LANGUAGE plpgsql;
ERROR: duplicate declaration at or near "pd" LINE 4: pd:= 'function'; ^
********** Error **********
ERROR: duplicate declaration at or near "pd" SQL state: 42601 Character: 104
Variables can be assigned default values within the declaration section of a PL/pgSQL code block. This is known as default value assignment, and is done by using the assignment operator (:=) on the same line as the variable's declaration.
In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.
The first time a variable is assigned a value, it is said to be initialised. The = symbol is known as the assignment operator. It is also possible to declare a variable and assign it a value in the same line, so instead of int i and then i = 9 you can write int i = 9 all in one go.
We can declare a record type variable by simply using a variable name followed by the record keyword. Syntax: variable_name record; We can use the dot notation (.) to access any field from the record type variable.
try like this
SQL Fiddle Demo
CREATE FUNCTION retrieve_parents(cid integer) RETURNS text AS $$
DECLARE pd text;
BEGIN
pd:= 'function';
RETURN concat(cid,pd);
END; $$
LANGUAGE PLPGSQL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With