----------
http://stackoverflow.com/questions/12144284/what-are-used-for-in-pl-pgsql
In fact if you look under "4.1.2.2. Dollar-Quoted String Constants" in the manual, you will see that you can even use characters in between the dollar symbols and it will all count as one delimiter.
OSEA QUE ANTES TENIAN QUE SER VACIOS esas cadenas constantes $$
-----------
The dollar signs are used for dollar quoting and are in no way specific to function definitions. It can be used to replace single quotes practically anywhere in SQL scripts.
(yo vi en ebook antiguo de posgresql,que para delimitar el cuerpo de una función utilizaban comillas simples)
The body of a function happens to be a string literal which has to be enclosed in single quotes. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid quoting issues inside the function body. You could write your function definition with single-quotes just as well. But then you'd have to escape all single-quotes in the body:
CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean AS
'
BEGIN
IF NOT $1 ~ e''^\\+\\d{3}\\ \\d{3} \\d{3} \\d{3}$'' THEN
RAISE EXCEPTION ''Malformed string "%". Expected format is +999 999'';
END IF;
RETURN true;
END;
' LANGUAGE plpgsql STRICT IMMUTABLE;
This isn't such a good idea. Use dollar-quoting instead, more specifically also put a token between the
$$
to make it unique - you might want to use $-quotes inside the function body, too. I do that a lot, actually.CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean LANGUAGE plpgsql STRICT IMMUTABLE AS
$func$
BEGIN
...
END;
$func$;
***********
The SQL needed to create a new function is CREATE FUNCTION, which has the following basicsyntax:CREATE FUNCTION name ( [ ftype [, ...] ] )RETURNS rtype
AS definition
LANGUAGE 'langname'
The parts of the function definition do not need to be in this order, and a popular choice isto state the language being used before the definition, like this:
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
LANGUAGE 'langname'
AS definition
No hay comentarios:
Publicar un comentario