zoukankan      html  css  js  c++  java
  • Mysql必知必会-样例

    第23章 使用存储过程

    DELIMITER ;;
    
    CREATE PROCEDURE ordertotal(
        IN onumber INT,
        IN taxable DECIMAL(8,2),
        OUT ototal DECIMAL(8,2)
    ) COMMENT 'Obtain order total, optionally adding tax'
    BEGIN
        -- Declare variable for total
        DECLARE total DECIMAL(8,2);
        -- Declare tax percantage
        DECLARE taxrate INT DEFAULT 6;
    
        -- Get the order total
        SELECT SUM(item_price*quantity)
        FROM orderitems
        WHERE order_num = onumber
        INTO total;
    
        -- Is this taxable?
        IF taxable THEN
            SELECT total+(total/100*taxrate) INTO total;
        END IF;
    
        SELECT total INTO ototal;
    END;;
    
    DELIMITER ;
    
    
    CALL ordertotal(20005, 1, @total);
    SELECT @total;

    第24章

    DELIMITER ;;
    
    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
        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);
        UNTIL done END REPEAT;
        
        -- Close the cursor
        CLOSE ordernumbers;
    END;;
    
    DELIMITER ;
    
    
    CALL processorders();
    SELECT * FROM ordertotals;
  • 相关阅读:
    Windows 08 R2_组策略
    Nginx常用配置实例(4)
    Nginx日常维护操作(3)
    Nginx配置文件(2)
    Nginx概述和安装(1)
    Zabbix实战-简易教程--通过公众平台企业号发送短信
    HDFS ZKFC自动切换原理分析
    HDFS ZKFC自动切换原理分析
    DataNode启动优化改进:磁盘检测并行化
    DataNode启动优化改进:磁盘检测并行化
  • 原文地址:https://www.cnblogs.com/baokang/p/15497095.html
Copyright © 2011-2022 走看看