Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when using function in postgres

Tags:

sql

postgresql

I've been trying to execute the following sql in postgres 9.6. But I get the error: syntax error at or near "row". Which would be on the line where CREATE AND REPLACE FUNCTION starts.

I've made a sqlfiddle too if you prefer that: http://sqlfiddle.com/#!17/48a30/1

CREATE TEMPORARY TABLE input (
    id serial, certified boolean
);

CREATE TEMPORARY TABLE tests_person (
  id serial, certified boolean
);

INSERT INTO input (id, certified) VALUES (DEFAULT, True), (DEFAULT, False), (DEFAULT, True), (DEFAULT, False), (DEFAULT, True), (DEFAULT, False);

CREATE OR REPLACE FUNCTION update_record(row input) RETURNS RECORD AS $$
    UPDATE "tests_person"
    SET certified=row.certified
    WHERE certified=row.certified
    RETURNING *
$$ LANGUAGE SQL;
like image 308
Jonathan Avatar asked May 24 '26 06:05

Jonathan


1 Answers

row is a reserved word and you cannot use it as your parameter name. Try this instead

CREATE OR REPLACE FUNCTION update_record(_input input) RETURNS RECORD AS 
$$
    UPDATE tests_person
    SET certified=_input.certified
    WHERE certified=_input.certified
    RETURNING *
$$ LANGUAGE SQL;
like image 105
Kamran Avatar answered May 26 '26 18:05

Kamran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!