我无法将存储过程从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服务器版本 在&#39; CALL GET_EOMONTH(v_month,v_year)附近使用的语法; 从@sql选择LASTDATE到v_lastm&#39;在第63行*
任何帮助或建议都会表示赞赏 感谢。
编辑-----------------------------
我想将我的代码修改为:
SET @output_variable = 0;
call GET_EOMONTH(v_month, v_year, @output_variable);
SELECT LASTDATE from @output_variable into v_lastmonthdate;
但错误,我想&#34; FROM {来自GET EOMONTH的动态输出}&#34; ... 我怎么能这样做?
答案 0 :(得分:0)
您可能正在调用存储过程,在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_variable中有表名,则必须使用预准备语句:
-- 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