0

So I saw this example in the documentation:

CREATE TABLE emp (
    empname text,
    salary integer,
    last_date timestamp,
    last_user text
);

CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
    BEGIN
        -- Check that empname and salary are given
        IF NEW.empname IS NULL THEN
            RAISE EXCEPTION 'empname cannot be null';
        END IF;
        IF NEW.salary IS NULL THEN
            RAISE EXCEPTION '% cannot have null salary', NEW.empname;
        END IF;

        -- Who works for us when they must pay for it?
        IF NEW.salary < 0 THEN
            RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
        END IF;

        -- Remember who changed the payroll when
        NEW.last_date := current_timestamp;
        NEW.last_user := current_user;
        RETURN NEW;
    END;
$emp_stamp$ LANGUAGE plpgsql;

CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
    FOR EACH ROW EXECUTE PROCEDURE emp_stamp();

where RETURNS trigger AS $emp_stamp$ use the same name of the trigger. But in another example it was left empty $$.

What's the use of the name between the two $s?

Thanks!

Jasper_Li
  • 234
  • 2
  • 9
  • 2
    https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING –  Oct 02 '17 at 23:07
  • After reading the documentation and the linked post, I'm still confused: when is the tag in the double dollar sign used? - My current guess is that tag captures the returned value of the function which can be used to substitute text later on. – Jasper_Li Oct 02 '17 at 23:42
  • 1
    @Jasper_Li: I added another original to clarify how dollar-quoting works - not like your current guess. – Erwin Brandstetter Oct 03 '17 at 01:32
  • OK. So in an example of `$tag$blablabal$tag$`, the tag is just for clarification – Jasper_Li Oct 03 '17 at 21:23

0 Answers0