I have two tables "books" and "bookOrder" and they look something like this:
bookOrder
| orderID | book name | required | availability |
|---|---|---|---|
| 1 | Harry Potter | 9 | yes |
| 2 | Twilight | 8 | yes |
| 3 | Bible | 8 | yes |
books
| book name | quantity |
|---|---|
| Harry Potter | 10 |
| Twilight | 5 |
| Bible | 8 |
I want to create a trigger that every time the books table is updated, it will update the bookorder availability column based on the book quantity column.
I have created a query as follows that does what I need:
UPDATE bookOrder bo
SET avalability = CASE WHEN b.quantity < bo.required THEN 'NO' ELSE 'YES' END
FROM books b
WHERE b.bookName = bo.bookName
However, would be better if this was automatic so a trigger was my first thought. To create a trigger, I would need a function to execute. This is where I have been trouble creating the function. My attempt is as below:
CREATE OR REPLACE FUNCTION update_books() RETURNS TRIGGER AS $$
BEGIN
UPDATE bookOrder bo
SET avalability = CASE WHEN b.quantity < bo.required THEN 'NO' ELSE 'YES' END
FROM books b
WHERE b.bookName = bo.bookName
END
$$ LANGUAGE plpgsql;
Why do I get an error at the last line where the ($$) is at?
psqlor some other client to create the trigger function? Not all SQL clients support Postgres' dollar quoting.FROMin your UPDATE statement:WHERE bo.bookname = new.bookname;would be enough.