在过去的一个小时里,我一直处于这种状态,无法将此MSSQL Server存储过程转换为“ MySQL查询”:

DECLARE @LedgerTbl TABLE (PARTY_ID VARCHAR(100),VRDATE VARCHAR(200),
                          VRNOA VARCHAR(200),ETYPE VARCHAR(50),
                          DESCRIPTION VARCHAR(500),DEBIT DECIMAL,
                          CREDIT DECIMAL, RunningTotal decimal)

DECLARE @RunningTotal decimal

SET @RunningTotal = 0

INSERT INTO @LedgerTbl
SELECT PARTY_ID,VRDATE,DCNO VRNOA,ETYPE,DESCRIPTION,DEBIT,CREDIT, null
FROM PLEDGER WHERE PARTY_ID=@partyId AND VRDATE BETWEEN @from AND @to
ORDER BY VRDATE,ETYPE,VRNOA

UPDATE @LedgerTbl
SET @RunningTotal = RunningTotal = @RunningTotal + (DEBIT-CREDIT)
FROM @LedgerTbl

SELECT * FROM @LedgerTbl


如何将其转换为单个MySQL查询或MySQL存储过程?

更新

我试图将其转换为,但它给了我下面给出的这些错误:

DELIMETER //

CREATE PROCEDURE `Acc_Ledger` ()
BEGIN
    DECLARE RunningTotal DECIMAL;
    SET RunningTotal = 0;
    CREATE TEMPORARY TABLE LedgerTbl (PARTY_ID VARCHAR(100),VRDATE VARCHAR(200),VRNOA VARCHAR(200),ETYPE VARCHAR(50),DESCRIPTION VARCHAR(500),DEBIT DECIMAL,RTotal decimal);
    INSERT INTO LedgerTbl
    SELECT PARTY_ID,VRDATE,DCNO VRNOA,ETYPE,DESCRIPTION,DEBIT,CREDIT, null
    FROM PLEDGER WHERE PARTY_ID=17 AND VRDATE BETWEEN '2013/12/02' AND '2010/12/02'
    ORDER BY VRDATE,ETYPE,VRNOA;

    UPDATE LedgerTbl
    SET RunninTotal = RTotal = RunningTotal + (DEBIT-CREDIT)
    FROM LedgerTbl;

    SELECT * FROM LedgerTbl;
END//
DELIMETER;


错误如下:

您的SQL查询中似乎有一个错误。下面的MySQL服务器错误输出(如果有的话)也可以帮助您诊断问题


  错误:未知标点字符串@ 10 STR:// SQL:DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal DECIMAL; DELIMETER //
  
  创建步骤Acc_Ledger()开始
      DECLARE RunningTotal十进制;
  
  SQL查询:
  
  DELIMETER //创建过程Acc_Ledger()开始声明
  RunningTotal十进制;
  
  MySQL说:文档
  
  #1064-您的SQL语法有误;查看与您的MySQL服务器版本相对应的手册以使用正确的语法
  靠近“ DELIMETER //
  
  创建步骤Acc_Ledger()开始
      第1行的DECLARE RunningTotal'


有人可以复习吗?

最佳答案

看来,MSSQL过程会计算运行总计,
我已经在sqlfiddle上测试了此过程(稍作修改),它给出了以下结果:
http://www.sqlfiddle.com/#!6/0e909/1

MSSQL过程可能在此处包含一个拼写错误:SELECT PARTY_ID,VRDATE,DCNO VRNOA,ETYPE,尚不清楚DCNO VRNOA是两个单独的列,还是一个DCNO_VRNOA列之间带有“缺少”下划线的列。
我假设它们是两个单独的列。

要在MySql中计算运行总计,无需使用临时表。
这个简单的查询可以完成以下任务:

SELECT PARTY_ID,VRDATE,DCNO, VRNOA,ETYPE,DESCRIPTION,
       DEBIT,
       CREDIT,
       @RunningTotal := @RunningTotal + (DEBIT-CREDIT) RunningTotal
FROM PLEDGER ,
     ( SELECT @RunningTotal:=0) init_variables
WHERE PARTY_ID=1
  AND VRDATE BETWEEN '2013-11-11' AND '2013-11-11'
ORDER BY VRDATE,ETYPE,VRNOA;


在此处查看演示:-> http://www.sqlfiddle.com/#!2/daa6e/1

该过程可能类似于:

DELIMITER /
CREATE PROCEDURE `Acc_Ledger` ()
BEGIN
    SELECT PARTY_ID,VRDATE,DCNO, VRNOA,ETYPE,DESCRIPTION,
           DEBIT,
           CREDIT,
           @RunningTotal := @RunningTotal + (DEBIT-CREDIT) RunningTotal
    FROM PLEDGER ,
         ( SELECT @RunningTotal:=0) init_variables
    WHERE PARTY_ID=1
      AND VRDATE BETWEEN '2013-11-11' AND '2013-11-11'
    ORDER BY VRDATE,ETYPE,VRNOA;
END /
DELIMITER ;

10-07 15:02