Tuesday 17 July 2018

How to create temporary table in MySQL

How to create temporary table in MySQL

  • It is faster to get data from temporary table instead of getting data form DB tables with several filter conditions.
  • Temporary tables are connection specific. When the current client session terminates all the temporary tables are automatically deleted.
  • Temporary table with same name can be created with in different connections.
Here is the syntax to create temporary table by getting data from another table with in a stored procedure.
SET @TEMPQUERY = ‘CREATE TEMPORARY TABLE TEMP_EMP_TABLE’;
SET @TEMPQUERY = CONCAT (@TEMPQUERY, ‘ AS SELECT EMP_ID, EMP_NAME, EMP_ADDRS, EMP_PHONE FROM EMPLOYEE ‘);
SET @TEMPQUERY = CONCAT (@TEMPQUERY, ‘ WHERE EMP_ID IN (SELECT EMP_ID FROM EMP_COMPANY)’);
 
PREPARE MYSQLQUERY FROM @TEMPQUERY;
EXECUTE MYSQLQUERY;

0 comments:

Post a Comment