0

I have a function like this:

CREATE OR REPLACE FUNCTION current_name()
RETURNS text AS 'select foo;' LANGUAGE sql;

Except it doesn't work. Neither does RETURN TEXT "SELECT 'foo';"

How can I keep it written in SQL, but still return text?

1 Answer 1

1

I think this is the least change you need to make it work.

CREATE OR REPLACE FUNCTION current_name()
RETURNS text AS 
'select ''foo''::text;'
LANGUAGE sql;

You'll see that the SQL statement--the body of the function--is a string. Strings have to be quoted, and single quotes within a quoted string have to be escaped. Soon you have more quotation marks than actual text.

The usual way to write something like this is to use a dollar quoted string constant.

CREATE OR REPLACE FUNCTION current_name()
RETURNS text AS 
$$
  select 'foo'::text;
$$
LANGUAGE sql;
Sign up to request clarification or add additional context in comments.

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.