A
A
Anton Artyomov2017-08-24 09:22:35
PostgreSQL
Anton Artyomov, 2017-08-24 09:22:35

Why does it work with CASE but not with IF?

Hello. I create a function for the trigger.
Why is it not created through IF?

CREATE FUNCTION my_trigger_finc() RETURNS trigger AS 
'DECLARE 
    myVar numeric DEFAULT 0;
BEGIN
    myVar     := IF NEW."TYPE_SCHET" = ''d8b24c5b-dd1f-488f-9f75-af7bbc45cb5a'' THEN 1 ELSE 0 END IF;
return NEW;
END;' LANGUAGE  plpgsql;

But through CASE norms
CREATE FUNCTION my_trigger_finc() RETURNS trigger AS 
'DECLARE 
    myVar numeric DEFAULT 0;
BEGIN
    myVar     := CASE WHEN NEW."TYPE_SCHET" = ''d8b24c5b-dd1f-488f-9f75-af7bbc45cb5a'' THEN 1 ELSE 0 END;
return NEW;
END;' LANGUAGE  plpgsql;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rickkk, 2017-08-28
@Rickkk

Well, in general, in a case after then or else, you cannot specify anything except the result for this condition -
here is the link A in IF, you can write
any expressions and operators. IF does not have to return a result, but CASE does. Since IF is ambiguous, therefore its output cannot be assigned to a variable, as you do in the 1st option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question