0

I'm having a hard time here with pl-pgsql syntax.

here's the code:

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 $$;

raises the error:

'LINE 4: dia INT := CASE WHEN dia_semana = 0 THEN dia := 7;'

with cursor on ":" "dia:= 7". Already tried a normal assignment "dia = 7" without success. Can someone please lend me a hand here?

tnx.

1 Answer 1

2

There is no semicolon ; between the cases neither the assignment inside each case. This case syntax is better here

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;

But you can simple do

dia int := 7 - dia_semana;
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Clodoaldo, tnx for the help got that "case" issue handled now. But the code still doesn't work. PGSQL is treating my variable as a column... heres the message: "ERROR: column "dia" don't exist LINE 1: COPY(SELECT CURRENT_DATE + dia)"
@XVirtusX: I think you'll need to build your COPY as a string and EXECUTE it: stackoverflow.com/a/19970705/479863
@XVirtusX I can't reproduce the error. Show the modified code
@muistooshort How should I concatenate the variable with the string? I tried the following... didnt work out: "execute 'COPY(SELECT CURRENT_DATE + ' || dia ||')' || 'TO /home/tmp/dump.sql';
@XVirtusX That error does not happen to me in 9.3. What is your version?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.