Reputation: 3
I'm trying to create a stored procedure that will refresh two tables in my database. The first table (detail_report) has a trigger on it that will insert rows into the second table (summary_report). This is for a class and the instructions asked us to use the same statements we used to extract and load data into the first table. The queries I included in the procedure work fine outside but when the procedure is called it just runs forever, so I know I must have missed something. Here is what I have:
CREATE OR REPLACE PROCEDURE refresh_tables()
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM detail_report;
DELETE FROM summary_report;
COPY
(SELECT title, rating, convert_to_money(amount) AS rental_amount, name AS genre
FROM film f
INNER JOIN inventory i
ON f.film_id = i.film_id
INNER JOIN rental r
ON i.inventory_id = r.inventory_id
INNER JOIN payment p
ON p.rental_id = r.rental_id
INNER JOIN film_category f_c
ON f_c.film_id = f.film_id
INNER JOIN category c
ON c.category_id = f_c.category_id
ORDER BY title) TO 'D:\CodingAndSoftware.detail_report.csv' WITH CSV HEADER;
COPY detail_report
FROM 'D:\CodingAndSoftware.detail_report.csv'
DELIMITER ','
CSV HEADER;
RETURN;
END;
$$;
The convert_to_money is a function I added with the help of another user. I doubt that is what is causing the issue, however. I've tried writing it without using this function, with just the column names and it still doesn't work, so I thought it had to do with the COPY command. I've also tried to write it by removing the COPY commands and simply using INSERT INTO detail_report followed by the SELECT query that gets the info from the required table joins, but that still runs nonstop when called. This latter method was recommended by my instructor in the course materials. It's probably something simple but my brain is too fried to see it.
CREATE OR REPLACE PROCEDURE refresh_tables()
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM detail_report;
DELETE FROM summary_report;
INSERT INTO detail_report
SELECT title, rating, convert_to_money(amount) AS rental_amount, name AS genre
FROM film f
INNER JOIN inventory i
ON f.film_id = i.film_id
INNER JOIN rental r
ON i.inventory_id = r.inventory_id
INNER JOIN payment p
ON p.rental_id = r.rental_id
INNER JOIN film_category f_c
ON f_c.film_id = f.film_id
INNER JOIN category c
ON c.category_id = f_c.category_id
ORDER BY title;
RETURN;
END;
$$;
Upvotes: 0
Views: 41