简体   繁体   English

如何在mysql过程中创建临时表并生成不同的名称?

[英]How to create temporary table and generate different name in mysql procedure?

I want to create temp tables which have different table names. 我想创建具有不同表名的临时表。

The stored procedure takes no input arguments, create a temp table and return a table name of the table as T_1, T_2, T_3.... 该存储过程不使用任何输入参数,创建一个临时表并以T_1,T_2,T_3 ....返回该表的表名。

How can I implement this in mysql stored procedure? 我该如何在mysql存储过程中实现呢?

Following procedure should help you. 遵循以下步骤应该可以为您提供帮助。 But this kind of sequence generation will work in current connection session only. 但是这种序列生成将仅在当前连接会话中起作用。 But I hope it is OK as you are expecting it on temporary tables . 但是我希望它能如您期望的那样在临时表上运行

delimiter //

drop procedure if exists set_new_temp_table //

create procedure set_new_temp_table()
begin
  if( @temp_table_seq_num is null ) then 
    set @temp_table_seq_num := 1;
  end if;

  set @temp_table_name := Concat( 'T_', @temp_table_seq_num );

  set @sql := concat( 'create temporary table if not exists '
                      , @temp_table_name 
                      , '( col1 int, col2 varchar(10) )'
                    );

  prepare stmt from @sql;
  execute stmt;
  drop prepare stmt;

  set @temp_table_seq_num := ( cast( @temp_table_seq_num as decimal ) + 1 );
  set @sql := null;
end;
//

delimiter ;

select @temp_table_name; -- should return a NULL before first ever call to SP
call set_new_temp_table(); select @temp_table_name;

Demo @ MySQL 5.5.32 Fiddle 演示 @ MySQL 5.5.32 Fiddle

This is full example 这是一个完整的例子

SELECT UUID() INTO @RandomName;
SET @TempTableNameWithSpecialCharectors := CONCAT('TEMP', @RandomName);
SET @TempTableName := REPLACE(@TempTableNameWithSpecialCharectors, '-', '');
SET @tempTable := CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS ', @TempTableName, ' (InteractionRequestId bigint(20), SendCount int)');

-- SELECT @tempTable;
PREPARE createStmt FROM @tempTable;
EXECUTE createStmt;
DROP PREPARE createStmt;


SET @enqueueDate := '2017-05-28';
SET @insertIntoTable := CONCAT('
INSERT INTO ', @TempTableName, '(InteractionRequestId, SendCount) SELECT InReq.Id, COUNT(*) AS SendCount FROM InteractionRequests AS InReq INNER JOIN InteractionResponses InRes ON InReq.Id = InRes.InteractionRequestId
WHERE InReq.EnqueuedRequest > "', @enqueueDate, 
'" GROUP BY InReq.Id');

SELECT @insertIntoTable;
PREPARE insertStmt FROM @insertIntoTable;
EXECUTE insertStmt;
DROP PREPARE insertStmt;

SET @SelctResult := CONCAT('SELECT * FROM ', @TempTableName);
PREPARE selectStmt FROM @SelctResult;
EXECUTE selectStmt;
DROP PREPARE selectStmt;

You can use the TEMPORARY keyword when creating a table. 您可以在创建表时使用TEMPORARY关键字。 A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. TEMPORARY表仅对当前会话可见,并且在关闭会话时会自动将其删除。 This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. 这意味着两个不同的会话可以使用相同的临时表名称,而不会彼此冲突或与现有的具有相同名称的非TEMPORARY表冲突。 (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege. (在删除临时表之前,现有表一直处于隐藏状态。)要创建临时表,您必须具有CREATE TEMPORARY TABLES特权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM