Skip to content

Instantly share code, notes, and snippets.

@Samuyi
Created January 7, 2019 12:16
Show Gist options
  • Save Samuyi/8662b3cde41cbb5e616bab703ba4660f to your computer and use it in GitHub Desktop.
Save Samuyi/8662b3cde41cbb5e616bab703ba4660f to your computer and use it in GitHub Desktop.
a sample postgresql function
CREATE FUNCTION car_bonus_func(vehicle_id int, bonus int)
RETURNS cars
AS $$
DECLARE
car cars;
BEGIN
PERFORM model FROM cars WHERE car_id = vehicle_id;
IF NOT FOUND THEN
RAISE EXCEPTION 'car with id of % not found', vehicle_id;
END IF;
IF bonus > 13 THEN
RAISE EXCEPTION 'sales bonus cannot be greater than 13';
END IF;
UPDATE cars SET sales_bonus = bonus
WHERE car_id = vehicle_id;
SELECT * INTO car FROM cars WHERE car_id = vehicle_id;
RETURN car;
END;
$$
LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment