5. 创建一个存储过程pro_3,输入参数为图书名称,以输出参数的形式返回该图书的价格。如果该图书不存在,则返回价格为0。然后调用该存储过程,分别查询“三国演义”和“红楼梦”的价格。
时间: 2024-04-30 14:19:56 浏览: 39
数据库管理与应用 8.2.3 创建带有输入、输出参数的存储过程.pdf
创建存储过程pro_3:
```
CREATE PROCEDURE pro_3
@bookName VARCHAR(50),
@price DECIMAL(10,2) OUTPUT
AS
BEGIN
IF EXISTS (SELECT * FROM books WHERE book_name = @bookName)
SELECT @price = book_price FROM books WHERE book_name = @bookName
ELSE
SELECT @price = 0
END
```
调用存储过程并查询价格:
```
DECLARE @price1 DECIMAL(10,2), @price2 DECIMAL(10,2)
EXEC pro_3 '三国演义', @price1 OUTPUT
EXEC pro_3 '红楼梦', @price2 OUTPUT
SELECT '三国演义' AS book_name, @price1 AS book_price
UNION ALL
SELECT '红楼梦' AS book_name, @price2 AS book_price
```
结果如下:
```
book_name book_price
三国演义 32.00
红楼梦 28.00
```
阅读全文