调用另一个存储过程时未执行sql

lrpiutwd  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(344)

我在将firebird存储过程转换为mysql时遇到了问题,我是mysql存储过程的新手,所以请提供示例帮助:(。
下面是我当前转换的代码:

DELIMITER $$

CREATE PROCEDURE INSERT_MRR_DETAIL (
    IN in_transactionindex INT,
    OUT monthyear INT,
    OUT day INT,
    OUT amount DOUBLE PRECISION,
    OUT acmountcumm DOUBLE PRECISION,
    OUT selisih DOUBLE PRECISION)
BEGIN
DECLARE v_customerindex INT;
DECLARE v_invoiceid INT;
DECLARE v_subscribe_date TIMESTAMP;
DECLARE v_subscribe_date_end TIMESTAMP;
DECLARE v_subscribe_sales DOUBLE PRECISION;
DECLARE v_month_days INT;
DECLARE v_month_effective_days INT;
DECLARE v_daily_amount_average DOUBLE PRECISION;
DECLARE v_lastmonthdate TIMESTAMP;
DECLARE v_month_start INT;
DECLARE v_year_start INT;
DECLARE v_month INT;
DECLARE v_year INT;
DECLARE v_month_end INT;
DECLARE v_year_end INT;
DECLARE v_amountcummulative DOUBLE PRECISION;
DECLARE v_month_amount DOUBLE PRECISION;
DECLARE v_subscribe_period_days INT;

SELECT  mrr_transaction.custid,
        mrr_transaction.invoiceid,
        mrr_transaction.datestart,
        mrr_transaction.dateend,
        mrr_transaction.amount
INTO    v_customerindex,
        v_invoiceid,
        v_subscribe_date,
        v_subscribe_date_end,
        v_subscribe_sales
FROM mrr_transaction
WHERE mrr_transaction.noindex = in_transactionindex;

SET v_subscribe_period_days = v_subscribe_date_end - v_subscribe_date + 1;

IF (v_subscribe_period_days > 0) THEN  -- Trapping Period not Zero / Null
BEGIN

  -- Define Variable Value
  SET v_month                       = extract(month from v_subscribe_date);
  SET v_year                        = extract(year from v_subscribe_date);
  SET v_month_start                 = v_month;
  SET v_year_start                  = v_year;
  SET v_month_end                   = extract(month from v_subscribe_date_end);
  SET v_year_end                    = extract(year from v_subscribe_date_end);
  SET v_daily_amount_average        = round( v_subscribe_sales / v_subscribe_period_days , 0);

  SET v_amountcummulative           = 0;

  WHILE ((v_year * 100 + v_month) <= (v_year_end * 100 + v_month_end)) DO
  BEGIN
      SET @sql = GET_EOMONTH(v_month, v_year);
      SELECT LASTDATE from @sql into v_lastmonthdate;
      SET monthyear             = v_year * 100 + v_month;
      SET v_month_days           = extract(day from v_lastmonthdate);
      SET v_month_effective_days = v_month_days;

      if ( (v_year * 100 + v_month) = (v_year_Start * 100 + v_month_start) ) then --Same with first month
            BEGIN
              v_month_effective_days = v_month_days - extract(day from v_subscribe_date) + 1;
            end
      END IF
      if ( (v_year * 100 + v_month) = (v_year_end * 100 + v_month_end) ) then    -- Same with last month
            BEGIN
              SET v_month_effective_days = extract(day from v_subscribe_date_end);
            END
      END IF

      SET v_month_amount        = v_daily_amount_average * v_month_effective_days;
      SET v_amountcummulative   = v_amountcummulative + v_month_amount;

      if ( (v_year * 100 + v_month) = (v_year_end * 100 + v_month_end) ) then    -- Same with last month
            BEGIN
              SET v_month_amount = v_month_amount + v_subscribe_sales - v_amountcummulative ;

            END
      END IF

   update mrr_Detail set isactive='F'
        where yearmonth= v_year*100 + v_month and
              transactionid = IN_TRANSACTIONINDEX;
   insert into mrr_detail(
                        custid,
                        invoiceid,
                        transactionid,
                        day,
                        month,
                        year,
                        amount,
                        yearmonth,
                        isactive)
                values(
                        V_CUSTOMERINDEX,
                        v_invoiceid,
                        IN_TRANSACTIONINDEX,
                        V_MONTH_EFFECTIVE_DAYS,
                        v_month,
                        v_year,
                        v_month_amount,
                        v_year*100 + v_month,
                        'T');

      -- Temporary Output Checking
      SET day = v_month_effective_days;
      SET amount = v_month_amount;
      SET AMOUNTCUMM = v_amountcummulative;
      SET selisih =   v_subscribe_sales - v_amountcummulative ;

      -- next month
      SET v_month = v_month+1;
      if (v_month = 13) then
        BEGIN
          SET v_month = 1;
          SET v_year = v_year + 1;
        END
      END IF

  END
  END WHILE;
END
END$$
DELIMITER ;

当我在我的heidisql中运行上面的代码时,会出现错误并说:
sql错误(1064):您的sql语法有错误;查看与您的mysql服务器版本对应的手册,以获得使用“call get\u eomonth(v\u month,v\u year)”的正确语法;在第63行的v_lastm'中从@sql中选择lastdate*
任何帮助或建议都将不胜感激。
编辑

lh80um4z

lh80um4z1#

您可能正在调用存储过程,在mysql中,过程不直接返回值(但函数返回值),您可以添加 OUT 参数作为中的第三个参数 GET_EOMONTH 程序。
之后,可以使用该变量调用存储过程:

SET @output_variable = 0;
  call GET_EOMONTH(v_month, v_year, @output_variable);
  -- do something with @output_variable
  if @output_variable > 0 then
    set v_lastmonthdate = @output_variable; 
  end if;

但是,如果您有一个函数,代码将更改为:

set @output_variable = GET_EOMONTH(v_month, v_year);
  -- do something with @output_variable
  if @output_variable > 0 then
    set v_lastmonthdate = @output_variable; 
  end if;

编辑:
如果@output\变量中有表名,则必须使用准备好的语句:

-- use a session variable to get value from prepared statement 

set @lasmontdate_session_var = ''; -- or any value of you choose
set @sql = concat('SELECT LASTDATE from ',@output_variable,' into  @lasmontdate_session_var LIMIT 1 '); -- add limit to prevent error
prepare stmt from @sql;
execute stmt ;
deallocate prepare stmt;

-- at this point @lasmontdate_session_var contains the result of prepared query
v2g6jxz6

v2g6jxz62#


我想将我的代码修改为:

SET @output_variable = 0;
  call GET_EOMONTH(v_month, v_year, @output_variable);

  SELECT LASTDATE from @output_variable into v_lastmonthdate;

但是错误,我想要“从{dynamic output from get eomonth}”。。。我该怎么做?

相关问题