postgresql - POSTGRES: problems on functions -
so, im trying work on simple book loans system , im having problems on creating , using function.
i have loans 'table', copies 'table' , available 'view'.
"available view" looks this:
book_id | available_copies ---------+------------------ bi6 | 1
wherein 'available_copies' column
count(copy_id) available_copies copies copy_id not in (select copy_id loans)
this "copies table"
copy_id | book_id | copy_no | copy_code ---------+---------+---------+----------- ci8 | bi6 | 8 | ci ci9 | bi6 | 9 | ci ci7 | bi7 | 7 | ci ci10 | bi7 | 10 | ci
and "loans table"
loan_id | copy_id | user_id | borrow_date | due_date | loan_no | loan_code ---------+---------+---------+-------------+------------+---------+----------- li10 | ci10 | ui4 | 2013-05-21 | 2013-05-26 | 10 | li li11 | ci8 | ui4 | 2013-05-21 | 2013-05-26 | 11 | li li12 | ci7 | ui4 | 2013-05-22 | 2013-05-27 | 12 | li
what wanted is.. if available_copies 0 (like in "available view" above, bi7 not in available view anymore because copies borrowed) postgres prompt cannot borrow books in loans anymore since book out of copies.
im kinda new plpgsql. please help. :(
i don't know pg version has, older. see lot of bugs in example - don't believe accepted postgres
create or replace function try(copyid text) returns boolean $$ begin select available_copies available -- missing ??? -- undeclared variable "available_copies" , -- collision column named "available_copies" if available_copies > 0 insert loans(copy_id) values(copyid); return boolean; --- return true or false, boolean?? else return boolean; end if; end; $$ language plpgsql;
Comments
Post a Comment