如何在MySQL中声明内部表?

时间:2011-01-13 14:04:07

标签: mysql sql

我想知道如何在MySQL中定义或声明内部表

我是MySQL的新手,我不知道语法

如您所见,我创建了存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `MySP`(
    actioncode VARCHAR(5),
    TNewsID BIGINT
)

BEGIN

IF actioncode = 1 then -- Retrive all from the database --
    select *
    from emp.tbnews;
elseIF actioncode = 2 then -- Retrive all from the database By NewsID --
    select NewsID,NewsSubject,NewsSubjectAR,NewsDetails,NewsDetailsAR,CreatedOn,DisplayOrder,
           AllowDisplay,img  
    from emp.tbnews
    Where NewsID=TNewsID;
elseIF actioncode = 3 then -- fkjskldfjklsdf --
    select NewsID,NewsSubject,NewsSubjectAR,NewsDetails,NewsDetailsAR,CreatedOn,DisplayOrder,
           AllowDisplay,img  
    from emp.tbnews;

 END IF;
 END

我真正想要的是在IF语句之前声明内部表

在Sql Server中我这样做

declare  @tbTemp table (
 a as int,
 b as char...etc.
)

因为我想在

之后插入insert语句
IF actioncode = 1
    Insert into @tbTemp

所以,如果你知道如何告诉我如何

对每个人都表示最好的问候。

2 个答案:

答案 0 :(得分:4)

create temporary table tmp
(
id int unsigned not null,
name varchar(32) not null
)
engine=memory; -- change engine type if required e.g myisam/innodb

insert into tmp (id, name) select id, name from foo... ;

-- do more work...

select * from tmp order by id;

drop temporary table if exists tmp;

create temporary table tmp engine=memory select id, name from foo... ;

-- do more work...

select * from tmp order by id;

drop temporary table if exists tmp;

答案 1 :(得分:0)

你的意思是CREATE TEMPORARY TABLE?它是一个特定于运行语句的连接的内存表,因此除非您运行持久连接,否则不必担心名称冲突。