Plpgsql function error at or near Create syntax - postgresql

I have just started using postgres and am new to the whole thing, I am using postgres version 9.6 and am trying to create a new function however I keep getting the error
ERROR: syntax error at or near "CREATE"
LINE 6: CREATE OR REPLACE FUNCTION time_passed(created text)
my function is this
CREATE OR REPLACE FUNCTION time_passed(created text)
RETURNS text AS
BEGIN
created :='Now';
RETURN created;
END;
$$ LANGUAGE plpgsql
What could be wrong with the Create syntax?

You're missing the opening $$ to match the closing ones:
CREATE OR REPLACE FUNCTION time_passed(created text)
RETURNS text AS $$
-- Here --------^
BEGIN
created :='Now';
RETURN created;
END;
$$ LANGUAGE plpgsql

Related

PostgreSQL INSERT INTO syntax error when creating function

I'm getting a syntax error but having issues identifying what exactly the issue is. I'm not sure if I'm misunderstanding things here -- but it looks like the INSERT INTO is breaking, but only when it's in the function.
I'm running the below through Hasura:
CREATE OR REPLACE FUNCTION custom_q_into_selected()
RETURNS trigger AS $BODY$
BEGIN
IF NEW.is_custom = true THEN
INSERT INTO selected_question(book_id,question_id)
VALUES(NEW.custom_for_book_id,NEW.id);
END IF;
RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;
Keep getting this error when I run this:
postgres-error: syntax error at or near "\"
Database logs:
CREATE OR REPLACE FUNCTION custom_q_into_selected() \r
RETURNS trigger AS $BODY$\r
BEGIN\r
IF NEW.is_custom THEN \r
INSERT INTO selected_question(book_id,question_id) VALUES(NEW.custom_for_book_id,NEW.id);\r
END IF;\r
RETURN NEW;\r
END;\r
$BODY$ LANGUAGE plpgsql;\r
Any help is appreciated.

Can you change an IN parameter inside of a plpgsql function?

Can you change a parameter that is set in the function header to something else with inside the function itself? The way I am reading this article it would seem like you can't postgres tutorial link
I am going to answer this one myself.
The short answer is Yes. You can change the default IN parameters in a plpgsql function.
Please see the following example:
--CREATE SCHEMA it
CREATE FUNCTION it.foo (_animal TEXT ) RETURNS TEXT LANGUAGE plpgsql AS
$$
BEGIN
IF _animal = 'cow' THEN
_animal = 'steak';
END IF;
RETURN _animal;
END;
$$;
SELECT it.foo('cow');

ERROR: syntax error at or near "$$

I am a newbie on Postgres.
I try to convert an Oracle PL/SQL script to Postgres.
At this, it's not a succes !
Here is a simple code with errors :
$$
DECLARE
Err_Exec numeric; -- Erreur d'execution.
BEGIN
Err_Exec := 0;
END;
$$ LANGUAGE plpgsql;
which reports:
:13: ERROR: syntax error at or near "$$
Any idea to this error when executing ?
That isn't a complete statement.
You need a CREATE FUNCTION statement if you're trying to create a function, or a DO block if you're trying to run a one-off procedure.
For one-off DO blocks, it should look like
DO LANGUAGE plpgsql
$$
BEGIN
.... plpgsql code here ...
END;
$$;

Function argument not interpreted inside body

I am trying to create a PL/pgSQL function in PostgreSQL 9.3.6, but there is weird behavior when using the passed argument inside function body. Here is the 'very simple' function:
CREATE OR REPLACE FUNCTION myschema.test (myarg text) RETURNS text AS $$
DECLARE
entity text;
buffer text;
BEGIN
CREATE ROLE myarg;
RETURN myarg;
END;
$$ LANGUAGE plpgsql;
So, if for instance myarg equals 'test':
A role named 'myarg' is created (WRONG)
'test' is returned (CORRECT)
I searched for hours why this could be and no clue... security parameter? Why is myarg not interpreted for creating roles?
Testing with phpPgAdmin through sql files if this has any impact.
You should use:
EXECUTE FORMAT('CREATE ROLE %I', myarg);
Here you can find an explanation (especially read Craig's answer).
As Erwin stated (thanks), %I is safer than %s. Anyway, myarg should be verified before the function call. Try for example
SELECT myschema.test('something; stupid; here;')

How can use custom_variable_class

i am create custom_variable_class= myapp in postgresql.conf. And set the value is inside of function like
CREATE OR REPLACE FUNCTION fn_purchase(xmode text, xuserno integer)
RETURNS text AS
set myapp.user_no=xuserno
..........
..........
END;
Now i am using myapp.user in my trigger
CREATE OR REPLACE FUNCTION public.delete_history()
RETURNS trigger AS
$BODY$
DECLARE userno text;
BEGIN
SELECT current_setting('myapp.user_no') into userno;
END;
$BODY$
If set userno is integer then it show error message.
invalid input syntax for integer: "xuserno".
and
SELECT current_setting('myapp.user_no') is show xuserno not xuserno value. That means xuserno=5 it show xuserno not 5. I am doing any thing wrong?
Your problem is a very subtle one:
According to the docs instead of:
custom_variable_class= myapp
You should have
custom_variable_classes='myapp'
Note that custom_variable_classes is intended to be used to set multiple classes for multiple addons.
From there, with the comments above, this should work.