Postgre Function

Sziasztok!

Életemben most írok először postgre functiont és ebben kérnék egy kis segítséget..A feladat az lenne, hogy egy számot adnánk vissza és azt pedig a lengthből számoljuk, én CASE-zel próbáltam megírni de nem tudom, hogy pontosan ezt hogy lehet... íme:

CREATE OR REPLACE FUNCTION r4u_graph(cost DOUBLE PRECISION, meta_steep DOUBLE PRECISION, meta_curb_height DOUBLE PRECISION, max_steep_up DOUBLE PRECISION,
max_steep_down DOUBLE PRECISION, max_curb_up DOUBLE PRECISION, meta_width DOUBLE PRECISION, length DOUBLE PRECISION)
RETURNS DOUBLE PRECISION AS $$
DECLARE
--declaration
BEGIN
PERFORM
inner_function(CASE
WHEN meta_steep > max_steep_up then (length*10000)
WHEN meta_steep < max_steep_down then (length*10000)
WHEN meta_curb_height > max_curb_up then (length*10000)
WHEN meta_curb_height < max_curb_down then (length*10000)
WHEN -meta_steep > max_steep_up then (length*10000)
WHEN -meta_steep < max_steep_down then (length*10000)
WHEN -meta_curb_height > max_curb_up then (length*10000)
WHEN -meta_curb_height < max_steep_down then (length*10000)
ELSE cost
END)
END;
$$ LANGUAGE plgsql;

Hozzászólások

42
---
"A megoldásra kell koncentrálni nem a problémára."

hát ez bizony több sebből vérzik.

ehhez nem kell plpgsql (nem plgsql) elég a sima sql

akkor viszont nem kell declare, begin, end sem.
a plpgsql -hez viszont kell return

de sql -el:

CREATE OR REPLACE FUNCTION public.proba(
cost double precision,
meta_steep double precision,
meta_curb_height double precision,
max_steep_up double precision,
max_steep_down double precision,
max_curb_up double precision,
meta_width double precision,
length double precision,
max_curb_down double precision)
RETURNS double precision AS
$BODY$select CASE
WHEN meta_steep > max_steep_up then (length*10000.0)
WHEN meta_steep < max_steep_down then (length*10000.0)
WHEN meta_curb_height > max_curb_up then (length*10000.0)
WHEN meta_curb_height < max_curb_down then (length*10000.0)
WHEN -meta_steep > max_steep_up then (length*10000.0)
WHEN -meta_steep < max_steep_down then (length*10000.0)
WHEN -meta_curb_height > max_curb_up then (length*10000.0)
WHEN -meta_curb_height < max_steep_down then (length*10000.0)
ELSE cost
END$BODY$
LANGUAGE sql VOLATILE;

Mivel nem latom a kerdest, ezert csak annyit tennek hozza, hogy ha mar curb_height, akkor valoszinuleg step_up.