Advertisement
rafibatam

PL/pgSQL Cursors

Aug 5th, 2019
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE Film
  2. (
  3. ID          SERIAL          PRIMARY KEY,
  4. Title       VARCHAR(250)    NOT NULL,
  5. Genre       VARCHAR(50)     NOT NULL,
  6. Released    INTEGER         NOT NULL,
  7. Description VARCHAR(2000)   NOT NULL
  8. );
  9.  
  10. ----------------------------------------------------------------------------------------------------------------------------
  11. INSERT INTO Film(ID, Title, Genre, Released, Description)
  12. VALUES(DEFAULT, 'Avengers: End Game', 'Fantasy/Sci-fi', 2019, 'Adrift in space with no food or water, Tony Stark sends a message to Pepper Potts as his oxygen supply starts to dwindle. Meanwhile, the remaining Avengers -- Thor, Black Widow, Captain America and Bruce Banner -- must figure out a way to bring back their vanquished allies for an epic showdown with Thanos -- the evil demigod who decimated the planet and the universe.');
  13.  
  14. INSERT INTO Film(ID, Title, Genre, Released, Description)
  15. VALUES(DEFAULT, 'Fast & Furious: Hobbs & Shaw', 'Mystery/Action', 2019, 'Ever since hulking lawman Hobbs (Johnson), a loyal agent of Americas Diplomatic Security Service, and lawless outcast Shaw (Statham), a former British military elite operative, first faced off in 2015’s Furious 7, the duo have swapped smack talk and body blows as they have tried to take each other down. But when cyber-genetically enhanced anarchist Brixton (Idris Elba) gains control of an insidious bio-threat that could alter humanity forever — and bests a brilliant and fearless rogue MI6 agent (The Crown’s Vanessa Kirby), who just happens to be Shaw’s sister — these two sworn enemies will have to partner up to bring down the only guy who might be badder than themselves.
  16. ');
  17.  
  18. INSERT INTO Film(ID, Title, Genre, Released, Description)
  19. VALUES(DEFAULT, 'Captain Marvel', 'Fantasy/Sci-fi', 2019, 'Captain Marvel is an extraterrestrial Kree warrior who finds herself caught in the middle of an intergalactic battle between her people and the Skrulls. Living on Earth in 1995, she keeps having recurring memories of another life as U.S. Air Force pilot Carol Danvers. With help from Nick Fury, Captain Marvel tries to uncover the secrets of her past while harnessing her special superpowers to end the war with the evil Skrulls.');
  20.  
  21. ----------------------------------------------------------------------------------------------------------------------------
  22. CREATE OR REPLACE FUNCTION Get_Title(film_release INTEGER)
  23.     RETURNS TEXT AS $$
  24. DECLARE
  25.     Titles      TEXT        DEFAULT '';
  26.     Record_Film RECORD;
  27.     Cursor_Film CURSOR(film_release INTEGER)
  28.         FOR SELECT Title, Released
  29.         FROM Film
  30.         WHERE Released = film_release;
  31. BEGIN
  32.     OPEN Cursor_Film(film_release);
  33.  
  34.     LOOP
  35.         FETCH Cursor_Film INTO Record_Film;
  36.         EXIT WHEN NOT FOUND;
  37.  
  38.         IF Record_Film.Title LIKE '%' THEN
  39.             Titles := Titles || ', ' || Record_Film.Title || ':' || Record_Film.Released;
  40.         END IF;
  41.     END LOOP;
  42.  
  43.     CLOSE Cursor_Film;
  44.  
  45.     RETURN Titles;
  46. END; $$
  47.  
  48. LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement