when to use = and := in postgreSQL?
Asked Answered
D

2

11

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;
Delighted answered 20/6, 2015 at 16:29 Comment(1)
#7462822Elderberry
P
15

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.

Pendergast answered 22/6, 2015 at 13:18 Comment(1)
Can we use := for comparisons?Telemeter
K
6

= is for comparison. := is for assignment.

Kazoo answered 20/6, 2015 at 16:31 Comment(4)
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????Delighted
= also works for assignment. Which is a little confusing. Better to stick with :=.Elderberry
@NickBarnes: = for assignment is deprecated and should not be used.Turnheim
@a_horse: That's what I thought, but as of 9.4, it seems that it's officially supported. I agree that it should not be used, though.Elderberry

© 2022 - 2024 — McMap. All rights reserved.