0

I'm quite new to pl/pgsql and I have one issue: I'm trying to create function that will calculate tax based on pensja in:

create table pracownicy (
  id_pracownika int unique
, imie varchar(20)
, nazwisko varchar(20)
, miasto varchar(20)
, pensja real);

insert into pracownicy values
  (1, 'John', 'Smith', 'New York', 150)
, (2, 'Ben', 'Johnson', 'New York', 250)
, (3, 'Louis', 'Armstrong', 'New Orleans', 75)
, (4, 'John', 'Lennon', 'London', 300)
, (5, 'Peter', 'Gabriel', 'London', 100);

I've created function:

CREATE OR REPLACE FUNCTION pit(pensja real)
  RETURNS real AS
$BODY$
DECLARE
    dochod REAL;
    podatek REAL;
BEGIN
    dochod = 12*pensja;
      IF dochod <= 85528 THEN
         podatek = dochod*0,18-556,02;
         RETURN podatek;
ELSE
      podatek = 14839 + 0,32*(dochod - 85528);
      RETURN podatek;
END IF;
END;
$BODY$
  LANGUAGE plpgsql;

I'm trying to run it with:

select nazwisko, pensja, pit(pensja) from pracownicy;

Receiving:

ERROR: query "SELECT * dochod 0,18-556,02" returned 3 columns
SQL state: 42601
Context: function PL / pgSQL pit (real), line 8 in the assignment

And I'm not quite sure where the error is?

2 Answers 2

2

You have to use a dot as decimal separator:

  podatek = dochod*0.18-556.02;
Sign up to request clarification or add additional context in comments.

3 Comments

And a link to the description of a DECIMAL: postgresql.org/docs/current/interactive/…
heh, I didn't see that coming ;) Thanks
just want to add that using comma sepeartor isntead of dot separator, totally changes the view of the sql enginne of the expression, which 'thinks' you are returning 3 columns instead of one, because the column separator is ,. The error you recieve shows it: ERROR: query "SELECT * dochod 0,18-556,02" returned 3 columns - dochod 0 - first column, 18-556 - second column, 02 - third column
0

Besides the primary issue with the decimal separator that @Jens pointed out, you can also largely simplify your function. Assignments are comparatively more expensive in plpgsql than in other popular languages.

CREATE OR REPLACE FUNCTION pit(pensja real)
  RETURNS real AS
$func$
DECLARE
   dochod real := 12 * pensja;
BEGIN
   IF dochod <= 85528 THEN
      RETURN dochod * 0.18 - 556.02;
   ELSE
      RETURN (dochod - 85528) * 0.32 + 14839;
   END IF;
END
$func$  LANGUAGE plpgsql;

Comments

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.