This is a SQL view that shows how many books are in the same shelf as the book with the ID ‘2’. Is there a simple way to turn this into a procedure where you can input the book ID, instead of the ID being ‘2’ by default? I haven’t ventured into procedures yet, would this particular procedure involve alot of coding?
SELECT COUNT(*) FROM books
WHERE shelf_id = (SELECT shelf_id FROM books WHERE book_id = 2)
The subquery is because the Book table has a shelf ID, but the shelf does not contain book ID’s.
If Oracle, Try something like below:
But better to make it a
functioninstead ofprocedureas: