2

I created a function using postgresql. But this function returns error when i try to execute it. error "ERROR: syntax error at or near "||"

CREATE OR REPLACE FUNCTION search_address_book(address_ids character)
  RETURNS void AS
$BODY$ 
SET sqlStatement = 'select * from addressbook';
SET address_ids = 'Post' || 'greSQL';
$BODY$
LANGUAGE 'sql' STABLE
COST 100;

My Server version is 8.3.14. Is this wrong? how do i concat two strings in postgresql?

4
  • 1
    Note that using quoted language names 'sql' or 'plpgsql'is unsupported and will not work any longer when moving to 9.1 or higher. You should specify the language without single quotes. Commented Jan 10, 2013 at 13:09
  • @a_horse_with_no_name: Good advice. But to be precise, I quote the manual: For backward compatibility, the name can be enclosed by single quotes. Commented Jan 10, 2013 at 14:15
  • @ErwinBrandstetter: it seems it will go away with the next version: postgresql.1045698.n5.nabble.com/… Commented Jan 10, 2013 at 14:24
  • I would love to see strings disallowed for the identifier, and I know they really want to. But I doubt it will happen for 9.3. Too many people would whine about broken code. Check out the devel manual here. Still the same disclaimer. The case you link to deals with the fact that 'PLpgSQL' no longer works, because lower casing has been removed. Commented Jan 10, 2013 at 14:34

1 Answer 1

6

To use variables you need a plpgsql function:

CREATE OR REPLACE FUNCTION search_address_book(address_ids character)
  RETURNS void AS
$BODY$
declare
   sqlStatement text;
   address_ids text;
begin
   sqlStatement := 'select * from addressbook';
   address_ids := 'Post' || 'greSQL';
end
$BODY$
LANGUAGE plpgsql STABLE;
Sign up to request clarification or add additional context in comments.

2 Comments

Use LANGUAGE plpgsql without single quotes. It's an identifier, quoting is discouraged and only tolerated for backward compatibility.
Upvoted. :) And I took the liberty to remove the redundant declare key word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.