Postgres匿名代码块语法错误

时间:2014-02-11 15:43:54

标签: postgresql plpgsql

我在这里用pl-pgsql语法很难。

这是代码:

DO $$
DECLARE 
dia_semana INT := CAST(EXTRACT(DOW FROM CURRENT_DATE)as INT);
dia INT :=  CASE WHEN dia_semana = 0 THEN dia := 7;
         WHEN dia_semana = 1 THEN dia := 6;
         WHEN dia_semana = 2 THEN dia := 5;
         WHEN dia_semana = 3 THEN dia := 4;
         WHEN dia_semana = 4 THEN dia := 3;
         WHEN dia_semana = 5 THEN dia := 2;
         WHEN dia_semana = 6 THEN dia := 1;
BEGIN   
COPY(SELECT CURRENT_DATE + dia)
TO '/tmp/dump.sql';
END $$;

引发错误:

'LINE 4:dia INT:=例如dia_semana = 0 THEN dia:= 7;'

将光标放在“:”“dia:= 7”上。 已尝试正常分配“dia = 7”但没有成功。有人可以帮我一把吗?

TNX。

1 个答案:

答案 0 :(得分:2)

案例之间没有分号;,每个案例内都没有分配。这里case语法更好

dia int := case dia_semana

    when 0 then 7
    when 1 then 6
    when 2 then 5
    when 3 then 4
    when 4 then 3
    when 5 then 2
    when 6 then 1
    end;

但你可以简单地做到

dia int := 7 - dia_semana;