mysql存储过程无法检索临时表

时间:2019-05-26 05:39:01

标签: mysql

我正在创建一个存储过程,如果有检索到的数据,我将检索它。我将第一个查询结果存储在临时表中,但是当我在临时表中检索数据时,它将返回错误#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM tempt END' at line 40。但是当我尝试查询时,它工作正常。这是写的命令

CREATE PROCEDURE test()
BEGIN
CREATE TEMPORARY TABLE tempt
SELECT userInput.ap_id,
       b.accountName,
       b.accountNumber,
       userInput.date,
       p.payeeName,
       t.taxType,
       t.value businessTax,
       userInput.grossAmount,
       userInput.taxableBase,
       CAST(userInput.taxableBase * t.value as decimal(11,2)) as taxAmount,
       userInput.grossAmount - (CAST(userInput.taxableBase * t.value as decimal(11,2))) as netOfVAT,
       particulars,
       accounts,
       NOW(),
       1,
       e.type,
       e.value ewtTax,
       ewtBase
FROM
(
    SELECT CONCAT('AP-',YEAR(NOW()),MONTH(NOW()),DAY(NOW()),'-',(COUNT(*) + 1)) as ap_id,
           paccountNumber as accountNumber,
           pDate as date,
           ppayee as payee,
           pgross as grossAmount,
           ptaxBase as taxableBase,
           pparticulars as particulars,
           paccounts as accounts,
           pewtBase as ewtBase
    FROM ap_entry
) userInput
INNER JOIN bnks b ON userInput.accountNumber = b.accountNumber
INNER JOIN pyee p ON userInput.payee = p.payeeName
INNER JOIN txtype t ON p.taxType = t.taxType
INNER JOIN ewt e ON p.ewt = e.id

SELECT COUNT(*) FROM tempt
END

1 个答案:

答案 0 :(得分:1)

在您的查询中缺少半冒号。

DELIMITER $$

CREATE PROCEDURE test()
BEGIN
CREATE TEMPORARY TABLE tempt
SELECT userInput.ap_id,
       b.accountName,
       b.accountNumber,
       userInput.date,
       p.payeeName,
       t.taxType,
       t.value businessTax,
       userInput.grossAmount,
       userInput.taxableBase,
       CAST(userInput.taxableBase * t.value as decimal(11,2)) as taxAmount,
       userInput.grossAmount - (CAST(userInput.taxableBase * t.value as decimal(11,2))) as netOfVAT,
       particulars,
       accounts,
       NOW(),
       1,
       e.type,
       e.value ewtTax,
       ewtBase
FROM
(
    SELECT CONCAT('AP-',YEAR(NOW()),MONTH(NOW()),DAY(NOW()),'-',(COUNT(*) + 1)) as ap_id,
           paccountNumber as accountNumber,
           pDate as date,
           ppayee as payee,
           pgross as grossAmount,
           ptaxBase as taxableBase,
           pparticulars as particulars,
           paccounts as accounts,
           pewtBase as ewtBase
    FROM ap_entry
) userInput
INNER JOIN bnks b ON userInput.accountNumber = b.accountNumber
INNER JOIN pyee p ON userInput.payee = p.payeeName
INNER JOIN txtype t ON p.taxType = t.taxType
INNER JOIN ewt e ON p.ewt = e.id; -- semi colon missing

SELECT COUNT(*) FROM tempt; -- semi colon missing
END
$$
DELIMITER ; -- changed back to default