Created
January 7, 2019 12:16
-
-
Save Samuyi/8662b3cde41cbb5e616bab703ba4660f to your computer and use it in GitHub Desktop.
a sample postgresql function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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