这是一个SQL视图,显示与ID为“2”的书籍在同一个书架中的书籍数量。是否有一种简单的方法可以将其转换为可以输入书籍ID的过程,而不是默认情况下ID为“2”的过程?我还没有尝试过程序,这个特殊的程序是否涉及很多编码?
SELECT COUNT(*) FROM books
WHERE shelf_id = (SELECT shelf_id FROM books WHERE book_id = 2)
子查询是因为Book表有一个货架ID,但货架上没有书ID。
答案 0 :(得分:0)
您的代码可能如下所示: -
CREATE PROCEDURE spBooks(@BookID integer)
AS
SELECT COUNT(*) FROM books
WHERE shelf_id = (SELECT shelf_id FROM books WHERE book_id = @BookID)
GO;
那应该创建你想要的存储过程。
答案 1 :(得分:0)
如果是甲骨文,请尝试以下内容:
PROCEDURE GET_BOOK_COUNT(
i_book_id IN books.book_id%TYPE,
o_count OUT NUMBER)
AS
BEGIN
SELECT COUNT(*) INTO o_count FROM books
WHERE shelf_id =
(SELECT shelf_id FROM books WHERE book_id = i_book_id );
END GET_BOOK_COUNT;
但最好将其设为function
而不是procedure
:
FUNCTION GET_BOOK_COUNT(
i_book_id IN books.book_id%TYPE)
RETURN NUMBER
IS
l_count Number;
SELECT COUNT(*) INTO l_count FROM books
WHERE shelf_id =
(SELECT shelf_id FROM books WHERE book_id = i_book_id );
RETURN l_count;
END GET_BOOK_COUNT;