mysql 存储过程 代码_mysql存储过程代码

DROP DATABASE IF EXISTS Trader_Day_Db; CREATE DATABASE IF NOT EXISTS Trader_Day_Db; USE Trader_Day_Db; DROP PROCEDURE IF EXISTS procedure_create_table; DELIMITER $$ CREATE PROCEDURE procedure_create_table(IN table_name VARCHAR(50)) BEGIN     DECLARE recordNum INT default 0;     set @sqlcreatetable = concat('CREATE TABLE IF NOT EXISTS ',                                   table_name,                                  '(id int auto_increment primary key not null,',                                  'trade_date char(11),',                                  'open_quotation DEC(10,2) not null,',                                  'high_quotation DEC(10,2) not null,',                                  'low_quotation DEC(10,2) not null,',                                  'close_quotation DEC(10,2) not null,',                                  'vol int8 not null,',                                  'open_interest int8 not null,',                                  'position_quotation DEC(10,2) not null,',                                  'ma5 DEC(10,2) DEFAULT 0.0,',                                  'ma10 DEC(10,2) DEFAULT 0.0,',                                  'ma20 DEC(10,2) DEFAULT 0.0,',                                  'macd DEC(10,2) DEFAULT 0.0,',                                  'cci DEC(10,2) DEFAULT 0.0,',                                  'emashort DEC(10,2) DEFAULT 0.0,',                                  'emalong DEC(10,2) DEFAULT 0.0,',                                  'dea DEC(10,2) DEFAULT 0.0,',                                  'typ DEC(10,2) DEFAULT 0.0);');     prepare stmt from @sqlcreatetable;     execute stmt;     DEALLOCATE PREPARE stmt;      END$$ DELIMITER ; call procedure_create_table('ma1808'); USE Trader_Day_Db; DROP PROCEDURE IF EXISTS procedure_insert_record; DELIMITER $$ CREATE PROCEDURE procedure_insert_record(in table_name char(50),                                           in ptrade_date char(11),                                          in open_quotation float,                                           in high_quotation float,                                           in low_quotation float,                                           in close_quotation float,                                           in vol int8,                                           in open_interest int8,                                           in position_quotation float) BEGIN     DECLARE recordNum INT default 0;     /*DECLARE CCI dec(10, 3) default 0.0;*/     DECLARE TYP dec(11, 3) default 0.0;     DECLARE lemashort dec(11, 3) default 0.0;     DECLARE lemalong dec(11, 3) default 0.0;     DECLARE ldea dec(11, 3) default 0.0;     DECLARE ldif dec(11, 3) default 0.0;     DECLARE MACD dec(11, 3) default 0.0;          set @EMAShort = 0.0;     set @EMALong = 0.0;     set @DEA = 0.0;     set @MA_TYP = 0.0;     set @AVEDEV_TYP = 0.0;          set TYP = (high_quotation + low_quotation + close_quotation) / 3;          /*Begin-Determine whether there is the same condition record */     set @sqlselect = concat("SELECT count(*) into @recordNum FROM ",                              table_name,  " WHERE trade_date = '",                              ptrade_date,                              "';");     prepare stmt from @sqlselect;     execute stmt;     DEALLOCATE PREPARE stmt;     /*End-Determine whether there is the same condition record */          IF (@recordNum < 1)     THEN         /*Begin- Get the total number of the records */         set @sqlselect = concat("SELECT count(*) INTO @recordNum FROM ",                                  table_name,                                   ";"); prepare stmt from @sqlselect;         execute stmt;         DEALLOCATE PREPARE stmt;         /*End- Get the total number of the records */                  /* Begin- MACD */         IF (@recordNum = 0)         THEN             set lemashort = close_quotation;             set lemalong = close_quotation;             set ldea = 0.0;             set MACD = 0.0;         END IF;                  IF (@recordNum > 0)         THEN             set @sqlselect = concat("SELECT emashort into @EMAShort FROM ",                                      table_name,                                      " order by id desc limit 1;");    prepare stmt from @sqlselect;             execute stmt;             DEALLOCATE PREPARE stmt;             set @sqlselect = concat("SELECT emalong into @EMALong FROM ",                                      table_name,                                      " order by id desc limit 1;");    prepare stmt from @sqlselect;             execute stmt;             DEALLOCATE PREPARE stmt;                         set @sqlselect = concat("SELECT DEA into @DEA FROM ",                                      table_name,                                      " order by id desc limit 1;");    prepare stmt from @sqlselect;             execute stmt;             DEALLOCATE PREPARE stmt;             set lemashort = @EMAShort * 11 / 13 + close_quotation * 2 / 13;             set lemalong = @EMALong * 25 / 27 + close_quotation * 2 / 27;             select lemashort;             select lemalong;             set ldif = lemashort - lemalong;             select ldif;             set ldea = @DEA * 8 / 10 + ldif * 2 / 10;             set MACD = (ldif - ldea) * 2;             select ldif;             select ldea;             select MACD;         END IF;                  /* End- MACD */                  /*Begin-insert one record */         set @sqlinsertrecord = concat("INSERT INTO ",                                        table_name,                                        " (trade_date, open_quotation, high_quotation, low_quotation, close_quotation, vol, open_interest, position_quotation, macd, emashort, emalong, dea, typ) VALUES ('",                                       ptrade_date,                                        "', ",                                        open_quotation,                                        ", ",                                        high_quotation,                                        ", ",                                        low_quotation,                                        ", ",                                        close_quotation,                                        ", ",                                        vol,                                        ", ",                                        open_interest,                                        ", ",                                       position_quotation,                                        ", ",                                        MACD,                                        ", ",                                       lemashort,                                        ", ",                                        lemalong,                                        ", ",                                        ldea,                                        ", ",                                       TYP,                                        ");");         prepare stmt from @sqlinsertrecord;         execute stmt;         DEALLOCATE PREPARE stmt;         /*End-insert one record */                  /* Begin-MA5 */         IF (@recordNum > 4)         THEN             set @sqlupdaterecord = concat("update ",                                            table_name,                                            " set ma5 = (select avg(close_quotation) from (select close_quotation FROM ",                                            table_name,                                            " order by id desc limit 5) as t) where 1 order by id desc limit 1;");             prepare stmt from @sqlupdaterecord;             execute stmt;             DEALLOCATE PREPARE stmt;         END IF;         /* End-MA5 */                  /* Begin-MA10 */         IF (@recordNum > 9)         THEN             set @sqlupdaterecord = concat("update ",                                            table_name,                                            " set ma10 = (select avg(close_quotation) from (select close_quotation FROM ",                                            table_name,                                            " order by id desc limit 10) as t) where 1 order by id desc limit 1;");             prepare stmt from @sqlupdaterecord;             execute stmt;             DEALLOCATE PREPARE stmt; END IF;         /* End-MA10 */                  /* Begin-MA20 */         IF (@recordNum > 19)         THEN             set @sqlupdaterecord = concat("update ",                                            table_name,                                            " set ma20 = (select avg(close_quotation) from (select close_quotation FROM ",                                            table_name,                                            " order by id desc limit 20) as t) where 1 order by id desc limit 1;");             prepare stmt from @sqlupdaterecord;             execute stmt;             DEALLOCATE PREPARE stmt; END IF;         /* End-MA20 */                  /* Begin-Calculate the CCI value */         IF (@recordNum > 13)         THEN /*Begin- Average value of typ for 14 days */             set @sqlselect = concat("SELECT AVG(TYP) INTO @MA_TYP FROM ",                                      table_name,                                      " order by id desc limit 14;"); prepare stmt from @sqlselect;             execute stmt;             DEALLOCATE PREPARE stmt;             /*End- Average value of typ for 14 days */                          /*Begin- AVEDEV value of typ for 14 days */             set @sqlselect = concat("SELECT avg(abs(TYP - @MA_TYP)) INTO @AVEDEV_TYP FROM ",                                      table_name,                                      " order by id desc limit 14;");             prepare stmt from @sqlselect;             execute stmt;             DEALLOCATE PREPARE stmt;             /*End- Average value of typ for 14 days */             set @sqlupdaterecord = concat("update ",                                            table_name,                                            " set CCI = ",                                            (TYP - @MA_TYP) / (0.015 * @AVEDEV_TYP),                                            " where 1 order by id desc limit 1;");             prepare stmt from @sqlupdaterecord;             execute stmt;             DEALLOCATE PREPARE stmt;                      END IF;         /* End-Calculate the CCI value */              END IF; END$$ DELIMITER ; /* call procedure_insert_record('Trader_Day_Db.MA1808', '2014/08/26', '2800', '2877', '2800', '2877', '4', '0', '2839'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/09/16', '2953', '2953', '2940', '2940', '20', '0', '2947'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/11/03', '2696', '2765', '2696', '2765', '4', '0', '2731'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/10', '2382', '2382', '2382', '2382', '10', '10', '2382'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/12', '2293', '2293', '2293', '2293', '10', '2', '2293'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/18', '2099', '2099', '2099', '2099', '2', '4', '2099'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/19', '2141', '2141', '2015', '2118', '6', '0', '2091'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/22', '2112', '2168', '2112', '2140', '8', '0', '2140'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/23', '2155', '2155', '2067', '2099', '84', '2', '2111'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/24', '2057', '2168', '2026', '2168', '48', '14', '2101'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/25', '2120', '2146', '2107', '2117', '86', '20', '2116'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/26', '2123', '2149', '2109', '2145', '128', '20', '2123'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/29', '2113', '2113', '2095', '2101', '14', '20', '2104'); call procedure_insert_record('Trader_Day_Db.MA1808', '2014/12/30', '2104', '2112', '2104', '2112', '12', '20', '2107'); */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值