Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql concat error when used in function

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')

like image 710
ziu fas Avatar asked Apr 16 '26 09:04

ziu fas


2 Answers

  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;
like image 62
Orkhan Alikhanov Avatar answered Apr 19 '26 05:04

Orkhan Alikhanov


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.

like image 26
yaldum Avatar answered Apr 19 '26 04:04

yaldum