1

Is there a difference between these two functions?

1st:

CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$
BEGIN
    RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;

2nd:

CREATE OR REPLACE FUNCTION sales_tax(subtotal real) RETURNS real  AS
$BODY$
begin
    RETURN subtotal * 0.06;
end;
$BODY$
  LANGUAGE plpgsql

Why does one have $$ and the other one have $body$? (PostgreSQL) What are '$$' used for in PL/pgSQL is not an answer to my question. is explains what is $ in general, and it doesn't refer to $body$

maybe one of them was the way to go in older versions and it might be depricit in future releases?

Community
  • 1
  • 1
java
  • 1,124
  • 2
  • 14
  • 33
  • @choz this is not answering my question. there is nothing about $body$ there. – java Jan 13 '16 at 06:53
  • Read [Erwin's answer](http://stackoverflow.com/a/12172353/1627271), might give you some clue. – choz Jan 13 '16 at 06:54
  • @java: `$$` and `$body$` are different versions of the same thing –  Jan 13 '16 at 07:20
  • 2
    @java: Yes there is; the second answer (the one with the highest score, but not the accepted answer) explicitly says why you might well want to use a word such as `$BODY$` in place of a plain `$$`. – Jonathan Leffler Jan 13 '16 at 07:25

3 Answers3

4

They are equivalent; both $$...$$ and $foo$...$foo$ are dollar-quoted string constants. (The foo part is optional; it just lets you end the string exactly where you want to, in case your string actually contains $$.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
2

There's no real difference. You choose your 'end of function' marker after the keyword AS with '$$ quoting'. In the first case, the chosen marker is $$; in the second, it is $BODY$. There's no other significant difference; the language is case-insensitive for keywords.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

The functions themselves are identical: as has been pointed out, the only difference is the closing tag.

The declarations of the functions are different though: the CREATE FUNCTION declaration will not overwrite an existing function with the same name, while CREATE OR REPLACE FUNCTION will.

Lav
  • 2,204
  • 12
  • 23