创建临时表存储过程时出现Mysql错误

时间:2013-11-16 07:57:41

标签: mysql sql

我有一个存储过程查询并在临时表中插入值..但显示错误

delimiter //
create procedure AccountLedgerViewUnderSundryDebtorCreditorCash()
begin
    create temporary table temp_kk(accountGroupID varchar(50),HierarchyLevel varchar(50));

    insert into temp_kk(accountGroupID,HierarchyLevel)
        select accountGroupId,  1 as HierarchyLevel from tbl_AccountGroup 
        where accountGroupId='27'or accountGroupId = '28'or 
        accountGroupId = '11';

    create temporary table temp_kk2(accountGroupID varchar(50),HierarchyLevel varchar(50));     

    insert into temp_kk2(accountGroupID,HierarchyLevel)
        select e.accountGroupId, G.HierarchyLevel + 1 AS HierarchyLevel 
        from tbl_AccountGroup as e, temp_kk G 
        where e.groupUnder=G.accountGroupId  ;

    create temporary table temp_kk3(accountGroupID varchar(50),HierarchyLevel varchar(50));     

    insert into temp_kk3(accountGroupID,HierarchyLevel)
        select * from temp_kk union all temp_kk2 ;

    SELECT  
ledgerId AS 'Account Ledger Id',  
accountLedgerName AS 'Account Ledger Name',  
accountGroupId AS 'Account Group Id',  
openingBalance AS 'Opening Balance',  
debitOrCredit AS 'Credit Or Debit',  
defaultOrNot AS 'Editable Or Not',  
description AS 'Description'  


FROM tbl_AccountLedger  
where accountGroupId IN (select accountGroupId from temp_kk3);

end //
delimiter ;
  

1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'temp_kk2附近使用正确的语法; SELECT ledgerId AS [Account Ledger Id],acccountLedgerName'在第20行

1 个答案:

答案 0 :(得分:1)

这一行:

    select * from temp_kk union all temp_kk2 ;

需要:

    select * from temp_kk union all select * from temp_kk2 ;

至少,最明显的是什么。

尽管如此,我倾向于避免使用select *。如果更改临时表,它将会中断。如果你不确定你选择的表中的所有列是什么,它最终会使代码更难阅读。是的,它现在可以为您节省一些打字,但是如果您不得不继续回顾表格定义以确定您选择的是什么,那么您将在以后节省时间。

尽管如此,通过这些简单的临时表,它并没有那么重要。