一個簡單的儲存過程的學習以及使用
阿新 • • 發佈:2019-02-16
我這邊所做的儲存過程的功能主要是,對一些表或一個表的資料 進行查詢 ,之後將所查詢的資料賦予給新的表中.
操作如下:
1.建立表
2.建立存儲存過程
3.執行儲存過程
4.比對錶之間的資料
下面的是參考別人的一個儲存過程:
create procedure my_procedure() -- 建立儲存過程 begin -- 開始儲存過程 declare my_id varchar(32); -- 自定義變數1 declare my_name varchar(50); -- 自定義變數2 DECLARE done INT DEFAULT FALSE; -- 自定義控制遊標迴圈變數,預設false DECLARE My_Cursor CURSOR FOR ( SELECT id, name FROM t_people ); -- 定義遊標並輸入結果集 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 繫結控制變數到遊標,遊標迴圈結束自動轉true OPEN My_Cursor; -- 開啟遊標 myLoop: LOOP -- 開始迴圈體,myLoop為自定義迴圈名,結束迴圈時用到 FETCH My_Cursor into my_id, my_name; -- 將遊標當前讀取行的資料順序賦予自定義變數12 IF done THEN -- 判斷是否繼續迴圈 LEAVE myLoop; -- 結束迴圈 END IF; -- 自己要做的事情,在 sql 中直接使用自定義變數即可 UPDATE t_user SET c_name = my_name WHERE id = my_id and rtrim(ltrim(c_name)) = ''; -- 左右去空格 COMMIT; -- 提交事務 END LOOP myLoop; -- 結束自定義迴圈體 CLOSE My_Cursor; -- 關閉遊標 END; -- 結束儲存過程
下面是之前需要寫的一個SQl 包含建表的sql
DROP TABLE IF EXISTS `report_seller_settle`; CREATE TABLE IF NOT EXISTS `report_seller_settle` ( `id` BIGINT ( 20 ) NOT NULL, `seller_id` BIGINT ( 20 ) DEFAULT '0' COMMENT '商家ID', `seller_name` VARCHAR ( 200 ) DEFAULT '' COMMENT '商家名稱', `money_amount` DECIMAL ( 10, 2 ) DEFAULT '0.00' COMMENT '結算現金總額', `efenbao_integral_amount` INT ( 11 ) DEFAULT '0' COMMENT 'e分寶積分總額', `icon_integral_amount` INT ( 11 ) DEFAULT '0' COMMENT '惠金幣總額', `voucher_amount` DECIMAL ( 10, 2 ) DEFAULT '0.00' COMMENT '代金券總額', `gift_amount` DECIMAL ( 10, 2 ) DEFAULT '0.00' COMMENT '禮品卡總額', `comm_amount` DECIMAL ( 10, 2 ) DEFAULT '0.00' COMMENT '佣金總額', `promotion_paid_amount` DECIMAL ( 10, 2 ) DEFAULT '0.00' COMMENT '活動優惠金額', `settle_amount` DECIMAL ( 10, 2 ) DEFAULT '0.00' COMMENT '結算金額', `year` INT ( 11 ) DEFAULT '0' COMMENT '結算日期-年', `month` INT ( 11 ) DEFAULT '0' COMMENT '結算日期-月', `day` INT ( 11 ) DEFAULT '0' COMMENT '結算日期-日', `report_time` date DEFAULT '3000-12-31' COMMENT '結算日期', `create_time` datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ( `id` ) USING BTREE ) ENGINE = INNODB AUTO_INCREMENT = 3333 DEFAULT CHARSET = utf8mb4 COMMENT = '商家結算報表'; DROP PROCEDURE IF EXISTS `SELECT_SELLER_SETTLE`; DELIMITER // CREATE PROCEDURE `SELECT_SELLER_SETTLE` ( ) BEGIN -- 宣告變數 DECLARE NO_MORE_RECORD INT DEFAULT 0; DECLARE `r_id` BIGINT ( 20 ); DECLARE `r_sellerId` BIGINT ( 20 ); DECLARE `r_sellerName` VARCHAR ( 200 ); DECLARE `r_moneyAmount` DECIMAL ( 10, 2 ); DECLARE `r_efenbaoIntegralAmount` INT ( 11 ); DECLARE `r_iconIntegralAmount` INT ( 11 ); DECLARE `r_voucherAmount` DECIMAL ( 10, 2 ); DECLARE `r_giftAmount` DECIMAL ( 10, 2 ); DECLARE `r_commAmount` DECIMAL ( 10, 2 ); DECLARE `r_promotionPaidAmount` DECIMAL ( 10, 2 ); DECLARE `r_sellerAmount` DECIMAL ( 10, 2 ); DECLARE `r_year` INT ( 11 ); DECLARE `r_month` INT ( 11 ); DECLARE `r_day` INT ( 11 ); DECLARE `r_reportTime` date; DECLARE `r_createTime` date; -- 宣告遊標 DECLARE r_cursor CURSOR FOR SELECT id, seller_id, seller_name, money_amount, efenbao_integral_amount, icon_integral_amount, voucher_amount, gift_amount, comm_amount, promotion_paid_amount, settle_amount, `year`, `month`, `day`, report_time, create_time FROM emateshop.seller_settle_report ORDER BY report_time DESC, `id` ASC; DECLARE CONTINUE HANDLER FOR NOT FOUND SET NO_MORE_RECORD = 1; TRUNCATE table report_seller_settle; OPEN r_cursor; REPEAT FETCH r_cursor INTO r_id, r_sellerId, r_sellerName, r_moneyAmount, r_efenbaoIntegralAmount, r_iconIntegralAmount, r_voucherAmount, r_giftAmount, r_commAmount, r_promotionPaidAmount, r_sellerAmount, r_year, r_month, r_day, r_reportTime, r_createTime; IF NOT NO_MORE_RECORD THEN REPLACE INTO report_seller_settle ( id, seller_id, seller_name, money_amount, efenbao_integral_amount, icon_integral_amount, voucher_amount, gift_amount, comm_amount, promotion_paid_amount, settle_amount, `year`, `month`, `day`, report_time, create_time ) VALUES ( r_id, r_sellerId, r_sellerName, r_moneyAmount, r_efenbaoIntegralAmount, r_iconIntegralAmount, r_voucherAmount, r_giftAmount, r_commAmount, r_promotionPaidAmount, r_sellerAmount, r_year, r_month, r_day, r_reportTime, r_createTime ); END IF; UNTIL NO_MORE_RECORD END REPEAT; CLOSE r_cursor; END// DELIMITER ;
另外呼叫儲存過程
call 儲存過程的名字();