syntax error at or near create when creating a function - sql

So I am creating a function in PostgreSQL for the first time and I am having a problem with the syntax
CREATE OR REPLACE FUNCTION uni() RETURNS INTEGER AS $$
DECLARE
mviews RECORD;
BEGIN
FOR mviews IN SELECT id,universite_adi FROM proaca.a LOOP
INSERT INTO proaca.universite (id,ad,ulke_id)
VALUES (mviews.id,mviews.universite_adi,1)
END LOOP;
RETURN 1;
END;
$$ LANGUAGE plpgsql;
ERROR: syntax error at or near "create"
Line 2: $BODY$CREATE OR REPLACE FUNCTION uni() RETURNS INTEGER AS $$

The syntax error was solved by a_horse in the comments:
missing a ; to terminate the INSERT statement.
You don't need that loop. Just plain sql
create or replace function uni()
returns integer as $$
insert into proaca.universite (id, ad, ulke_id)
select id, universite_adi, 1
from proaca.a
returning 1
;
$$ language sql;

Related

SQL Error [42601]: ERROR: syntax error at or near "ALTER" [duplicate]

This is my function:
CREATE OR REPLACE FUNCTION addData (x INT,y INT)
RETURNS void AS $$
BEGIN
INSERT INTO TABLE temp(id,name,pass) VALUES(y,"ABC","XYZ");
END;
$$ LANGUAGE SQL;
but the output is
$$ LANGUAGE SQL
ERROR: syntax error at or near "INSERT"
LINE 4: INSERT INTO TABLE temp(id,name,pass)
I tried by changing data type, double quotes to single quotes, please help me with this. I am currently using pSQL
INSERT INTO TABLE... no need TABLE keyword
VALUES(y,"ABC","XYZ")... You need single quotes instead of double
Since you use BEGIN..END, you need plpgsql language
CREATE OR REPLACE FUNCTION addData (x INT,y INT)
RETURNS void AS $$
BEGIN
INSERT INTO temp(id,name,pass) VALUES(y,'ABC','XYZ');
END;
$$ LANGUAGE plpgSQL;

Postgres syntax error with creat when use $$

I want execute next script migration.
But i catch syntax error.
How can i resolve it?
Script example:
CREATE OR REPLACE FUNCTION __execute(TEXT) RETURNS VOID AS $$
BEGIN EXECUTE $1; END;
$$ LANGUAGE plpgsql STRICT;
CREATE OR REPLACE FUNCTION __index_exists(TEXT, TEXT, TEXT) RETURNS bool as $$
SELECT exists(SELECT 1 FROM pg_indexes WHERE (schemaname, tablename, indexname) = ($1, $2, $3));
$$ language sql STRICT;
DO $$ BEGIN IF __my_function('true') THEN
SELECT __execute($$
CREATE INDEX "test_idx"
ON test_table
USING btree (column_name);
$$)
WHERE NOT __index_exists('public', 'test_table', 'test_idx');
END IF; END; $$;
Error:
SQL State : 42601
Error Code : 0
Message : ERROR: syntax error at or near "CREATE"
I think postgresql get confused between dollar quoting inside function, probably if you give it a tag name will solve the issue
DO $$ BEGIN IF __my_function('true') THEN
perform __execute($param$ CREATE INDEX "test_idx"
ON test_table
USING btree (column_name);$param$)
WHERE NOT __index_exists('public', 'test_table', 'test_idx');
END IF; END; $$;

Postgresql Syntax Error in Trigger Function

I want to create the following trigger function in my Postgresql DB :
CREATE FUNCTION attribute_edit_history()
RETURNS TRIGGER AS
$BODY$
BEGIN
Select
CASE
WHEN NOT EXISTS
(SELECT * FROM public."TB02_MDD_KEY" where "ENCODED_ID" =ENCODE(CONVERT_TO(NEW."ATTRIBUTE_NAME", 'UTF-8'), 'base64'))
THEN
CASE
WHEN OLD."ATTRIBUTE_NAME" is distinct from NEW."ATTRIBUTE_NAME"
THEN
INSERT INTO public."TB08_ATTRIBUTE_EDIT_HISTORY"(
"ENCODED_ID_OLD","ENCODED_ID_NEW" , "VERSION", "ATTRIBUTE_OLD", "ATTRIBUTE_NEW", "ATTRIBUTE_NEW_ID")
VALUES ( OLD."ENCODED_ID", NEW."ENCODED_ID", NEW."VERSION", OLD."ATTRIBUTE_NAME", NEW."ATTRIBUTE_NAME", ENCODE(CONVERT_TO(NEW."ATTRIBUTE_NAME", 'UTF-8'), 'base64'));
END;
END;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER "attribute_edit_history" BEFORE UPDATE ON "TB02_MDD_KEY"
FOR EACH ROW EXECUTE PROCEDURE attribute_edit_history();
I am getting the following syntax error :
ERROR: syntax error at or near "INTO"
LINE 13: INSERT INTO public."TB08_ATTRIBUTE_EDIT_HISTORY"(
^
SQL state: 42601
Character: 352
I dont know where I am going wrong.
Thanks in advance!
Most likely, you want a simple IF statement rather than nested CASE expressions:
CREATE FUNCTION attribute_edit_history()
RETURNS TRIGGER AS
$BODY$
BEGIN
IF
NOT EXISTS(
SELECT 1
FROM public.TB02_MDD_KEY
WHERE ENCODED_ID = ENCODE(CONVERT_TO(NEW.ATTRIBUTE_NAME, 'UTF-8'), 'base64')
)
AND OLD.ATTRIBUTE_NAME IS DISTINCT FROM NEW.ATTRIBUTE_NAME
THEN
INSERT INTO public.TB08_ATTRIBUTE_EDIT_HISTORY(
ENCODED_ID_OLD,
ENCODED_ID_NEW ,
VERSION,
ATTRIBUTE_OLD,
ATTRIBUTE_NEW,
ATTRIBUTE_NEW_ID
) VALUES (
OLD.ENCODED_ID,
NEW.ENCODED_ID,
NEW.VERSION,
OLD.ATTRIBUTE_NAME,
NEW.ATTRIBUTE_NAME,
ENCODE(CONVERT_TO(NEW.ATTRIBUTE_NAME, 'UTF-8'), 'base64')
);
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
Note that I removed the double quotes around the tables and columns identifiers; you don't need those (unless you have case-sensitive identifiers, which does not seem to be the case here).

pgSQL show an error in its function before the SELECT query

This is my function:
CREATE OR REPLACE FUNCTION addData (x INT,y INT)
RETURNS void AS $$
BEGIN
INSERT INTO TABLE temp(id,name,pass) VALUES(y,"ABC","XYZ");
END;
$$ LANGUAGE SQL;
but the output is
$$ LANGUAGE SQL
ERROR: syntax error at or near "INSERT"
LINE 4: INSERT INTO TABLE temp(id,name,pass)
I tried by changing data type, double quotes to single quotes, please help me with this. I am currently using pSQL
INSERT INTO TABLE... no need TABLE keyword
VALUES(y,"ABC","XYZ")... You need single quotes instead of double
Since you use BEGIN..END, you need plpgsql language
CREATE OR REPLACE FUNCTION addData (x INT,y INT)
RETURNS void AS $$
BEGIN
INSERT INTO temp(id,name,pass) VALUES(y,'ABC','XYZ');
END;
$$ LANGUAGE plpgSQL;

SQL (Postgres) function definition - RETURNS

I am trying to create a very simple PostgreSQL function, though I keep getting a very strange syntax error. The syntax I use is different from anything I have seen online (though this is the one the textbook uses), and thus I can't figure out why it fails...
This is the SQL:
CREATE OR REPLACE FUNCTION gsum(graphID integer)
RETURNS integer
BEGIN
DECLARE total integer DEFAULT 0
SELECT sum(weight) INTO total
FROM Edge
WHERE gno = graphID
RETURN total;
END;
The error is:
ERROR: syntax error at or near "BEGIN"
LINE 3: BEGIN
^
********** Error **********
ERROR: syntax error at or near "BEGIN"
SQL state: 42601
Character: 68
Your basic mistakes:
DECLARE must come before BEGIN.
Statements need to be terminated with ;.
Function body of a plpgsql function is a string and needs to be quoted. Use dollar-quoting to avoid complications with quotes in the body.
Missing keyword AS.
Missing language declaration LANGUAGE plpgsql.
Type mismatch.
You don't need a default.
This would still return NULL if the sum is NULL.
CREATE OR REPLACE FUNCTION gsum(graphID integer)
RETURNS integer AS
$func$
DECLARE
total integer;
BEGIN
SELECT sum(weight)::int INTO total
FROM edge
WHERE gno = graphID;
RETURN COALESCE(total, 0);
END
$func$ LANGUAGE plpgsql;
And you'd better use a simple SQL function for this like #Clodoaldo advised. Just add COALESCE().
It can be plain SQL in instead of plpgsql
create or replace function gsum(graphid integer)
returns bigint as $$
select sum(weight) as total
from edge
where gno = graphid;
$$ language sql;
Notice that if weight is integer sum will return bigint not integer.
CREATE OR REPLACE FUNCTION gsum(graphID integer)
RETURNS integer AS
$BODY$
DECLARE
total integer DEFAULT 0;
BEGIN
SELECT sum(weight) INTO total
FROM Edge
WHERE gno = graphID;
RETURN total;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;