数据库系统实训——实验六——游标
第一部分:樣例庫(kù)的應(yīng)用
1)創(chuàng)建游標(biāo)
語(yǔ)句:
CREATE PROCEDURE processorders()
BEGIN
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
END;
截圖:
2)打開(kāi)和關(guān)閉游標(biāo)
語(yǔ)句:
OPEN ordernumbers;
CLOSE ordernumbers;
3)使用游標(biāo)數(shù)據(jù)
語(yǔ)句:
CREATE PROCEDURE processorders()
BEGIN
– Declare local variables
DECLARE o INT;
– Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
– Open the cursor
OPEN ordernumbers;
– Get order number
FETCH ordernumbers INTO o;
SELECT O;
– Close the cursor
CLOSE ordernumbers;
END;
CALL processorders();
截圖:
4) 循環(huán)檢索數(shù)據(jù)
語(yǔ)句:
CREATE PROCEDURE processorders()
BEGIN
– Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
– Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
– Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000’ SET done=1;
– Open the cursor
OPEN ordernumbers;
– Loop through all rows
REPEAT
– Get order number
FETCH ordernumbers INTO o;
SELECT o;
– End of loop
UNTIL done END REPEAT;
– Close the cursor
CLOSE ordernumbers;
END;
截圖:
5) 循環(huán)處理數(shù)據(jù)
語(yǔ)句:
CREATE PROCEDURE processorders()
BEGIN
– Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
– Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
– Declare continue handler,SQLSTATE ‘02000’ 是一個(gè)“未找到”條件
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000’ SET done=1;
– Create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals
(order_num INT, total DECIMAL(8,2));
– Open the cursor
OPEN ordernumbers;
– Loop through all rows
REPEAT
– Get order number
FETCH ordernumbers INTO o;
– Get the total for this order
CALL ordertotal(o, 1, t);
– Insert order and total into ordertotals
INSERT INTO ordertotals(order_num, total)
VALUES(o, t);
– End of loop
UNTIL done END REPEAT;
– Close the cursor
CLOSE ordernumbers;
END;
CALL processorders();
SELECT DISTINCTROW *
FROM ordertotals order by order_num;
截圖:
第二部分:所選課題數(shù)據(jù)庫(kù)的應(yīng)用
1)創(chuàng)建游標(biāo)
語(yǔ)句:
CREATE PROCEDURE movieorders()
BEGIN
DECLARE ordernumbers CURSOR
FOR
SELECT year FROM movies;
END;
截圖:
2)打開(kāi)和關(guān)閉游標(biāo)
語(yǔ)句:
OPEN ordernumbers;
CLOSE ordernumbers;
3)使用游標(biāo)數(shù)據(jù)
語(yǔ)句:
CREATE PROCEDURE movieorders()
BEGIN
– Declare local variables
DECLARE o INT;
– Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT year FROM movies;
– Open the cursor
OPEN ordernumbers;
– Get order number
FETCH ordernumbers INTO o;
SELECT O;
– Close the cursor
CLOSE ordernumbers;
END;
CALL movieorders();
截圖:
4) 循環(huán)檢索數(shù)據(jù)
語(yǔ)句:
CREATE PROCEDURE movieorders()
BEGIN
– Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
– Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT year FROM movies;
– Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000’ SET done=1;
– Open the cursor
OPEN ordernumbers;
– Loop through all rows
REPEAT
– Get order number
FETCH ordernumbers INTO o;
SELECT o;
– End of loop
UNTIL done END REPEAT;
– Close the cursor
CLOSE ordernumbers;
END;
截圖:
5) 循環(huán)處理數(shù)據(jù)
語(yǔ)句:
CREATE PROCEDURE movieorders()
BEGIN
– Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
– Declare the cursor
DECLARE ordernumbers CURSOR
FOR
SELECT year FROM movies;
– Declare continue handler,SQLSTATE ‘02000’ 是一個(gè)“未找到”條件
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000’ SET done=1;
– Create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals
(year INT, total DECIMAL(8,2));
– Open the cursor
OPEN ordernumbers;
– Loop through all rows
REPEAT
– Get order number
FETCH ordernumbers INTO o;
– Get the total for this order
CALL ordertotal(o, 1, t);
– Insert order and total into ordertotals
INSERT INTO ordertotals(year, total)
VALUES(o, t);
– End of loop
UNTIL done END REPEAT;
– Close the cursor
CLOSE ordernumbers;
END;
CALL movieorders();
SELECT DISTINCTROW *
FROM ordertotals order by year;
截圖:
總結(jié)
以上是生活随笔為你收集整理的数据库系统实训——实验六——游标的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++ 关键字 (try-finally
- 下一篇: array of const