4

Im using plpgsql to write triggers n Im wondering when to use = and when to use := in postgreSQL, what is the difference???

for example:

CREATE OR REPLACE FUNCTION on_ai_myTable() RETURNS TRIGGER AS $$
DECLARE
t_ix real;
n int;

BEGIN
IF NEW.time_type = 'Start' THEN
    SELECT t.time_index FROM table_ebscb_spa_log02 t WHERE t.fn_name = NEW.fn_name AND t.time_type = 'Start' ORDER BY t.timestamp02 DESC LIMIT 1 INTO t_ix;
      GET DIAGNOSTICS n = ROW_COUNT;
        IF (n = 0) THEN 
        t_ix = 1;
        ELSE 
        t_ix = t_ix + 1;
        END IF;
END IF;
NEW.time_index = t_ix;
return NEW;
END
$$
LANGUAGE plpgsql;
moodymudskipper
  • 42,696
  • 10
  • 102
  • 146
Natysiu16
  • 176
  • 5
  • 11

2 Answers2

7

In version 9.4, the documentation was updated to make it clear that there is no difference.

Version 9.4:

40.5.1. Assignment

An assignment of a value to a PL/pgSQL variable is written as:

variable { := | = } expression;

[...]

Equal (=) can be used instead of PL/SQL-compliant :=

In previous versions, := alone was mentioned as the assignment operator, but = has been working since the beginning.

Daniel Vérité
  • 53,594
  • 14
  • 124
  • 144
4

= is for comparison. := is for assignment.

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Ed Heal
  • 57,599
  • 16
  • 82
  • 120
  • so the example above is incorrect, it should say... IF (n=0) THEN t_ix := 1; ELSE t_ix := t_ix + 1; END IF; ain't it???? – Natysiu16 Jun 20 '15 at 16:43
  • `=` also works for assignment. Which is a little confusing. Better to stick with `:=`. – Nick Barnes Jun 20 '15 at 16:50
  • 2
    @NickBarnes: `=` for assignment is deprecated and should not be used. – a_horse_with_no_name Jun 20 '15 at 17:38
  • @a_horse: That's what I thought, but [as of 9.4](https://github.com/postgres/postgres/commit/7e1955b861a1be9ef2dfd6acdd50d0c6b5a75794), it seems that it's officially supported. I agree that it should not be used, though. – Nick Barnes Jun 21 '15 at 01:10