3

PostgreSQL syntax error when using CONCAT in user defined function. Using DbVisualizer as SQL client.

CREATE FUNCTION t() RETURNS TEXT 
AS ' SELECT CONCAT('some', '_text') ; ' 
LANGUAGE plpgsql;

[Code: 0, SQL State: 42601] ERROR: syntax error at or near "some" Position: 55 [Script position: 54 - 61]

Query works as expected SELECT CONCAT('some_', '_text')

2 Answers 2

5
  1. You've used ' as the escaping sequence but also tried to use it in the string. I suggest using $$ instead.
  2. With plpgsql lang, you are missing BEGIN .. END

So the corrected SQL is:

CREATE FUNCTION t() RETURNS TEXT
AS $$
BEGIN
    SELECT CONCAT('some', '_text');
END;
$$ LANGUAGE plpgsql;

Or with sql as language:

CREATE FUNCTION t() RETURNS TEXT
AS $$
    SELECT CONCAT('some', '_text');
$$ LANGUAGE sql;
Sign up to request clarification or add additional context in comments.

2 Comments

Then you get another error (either language): [Code: 0, SQL State: 42601] Unterminated dollar quote started at position 0 in SQL $$ LANGUAGE plpgsql. Expected terminating $$ [Script position: 180 - 182]
Seems this is DbVisualizer problem. When runnig Dbeaver sql client, using $ as proposed, all's OK
2

Consider this an addon to the answer by @Orkhan Alikhanov.

The reason it doesn't work in DbVisualizer is because that program (for some unknown reason) does not allow (difficult?) statements. You can fix this by turning on what they call "dialects" in the settings:

Tool Properties

Then, both suggestions in the answer by Orkhan should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.