This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Actually, The funda of $$ – double dollar is very simple, but Postgres new comers have always doubted about it.
When you are writing a PL/pgSQL function, the body of the function has to pass in the form of a string literal.
For example:
Create a sample table:
1 2 |
CREATE TABLE tbl_Students (StudID INT, StudName CHARACTER VARYING); |
Create a function without $$ – double dollar:
1 2 3 4 5 6 7 8 9 |
CREATE OR REPLACE FUNCTION fn_TestDelimiter() RETURNS BOOLEAN AS ' BEGIN INSERT INTO tbl_Students VALUES (1,''Anvesh''); RETURN TRUE; END; ' LANGUAGE plpgsql |
We can create a function using single quote and We do not require $$ – double dollar in above example.
But we should not do this, because It is not suggestible to write your function code in single quotes.
When you create your function using single quote, you have to escape all internal single quote in the body.
$$ – Double Dollar:
It is nothing, but just a substitute of a single quote. Using $$ – Double Dollar we can avoid all types of quoting issues.
We can also put token between $$ Double Dollar like: $rnd$ which makes it unique.
For Example:
Create a function with $$ – double dollar:
1 2 3 4 5 6 7 8 9 |
CREATE OR REPLACE FUNCTION fn_TestDelimiter() RETURNS BOOLEAN AS $$ BEGIN INSERT INTO tbl_Students VALUES (1,'Anvesh'); RETURN TRUE; END; $$ LANGUAGE plpgsql |