Keboola WIKI‎ > ‎Transformations‎ > ‎MySQL‎ > ‎

Creating and Executing Query On The Fly

by tomas.trnka@keboola.com
--

1) First, create a query as a result of a query :-) and store it into variable 

SELECT
CONCAT('CREATE TABLE `out` (' , GROUP_CONCAT(CONCAT('
`',`outbound_column_name`,'`',' text NOT NULL')),') 
AS SELECT ',
GROUP_CONCAT(
CASE WHEN LENGTH(`inbound_column_name`)>0 
THEN CONCAT('
`',`inbound_column_name`,'`',' AS `',`outbound_column_name`,'`')
ELSE CONCAT('
'''' AS `',`outbound_column_name`,'`') END),',
`project` AS `project`',' FROM `input`; ') AS `query`
INTO @var 
FROM `column_mapping`;

2) Execute it during the same transformation

PREPARE temp_query FROM @var; 
EXECUTE temp_query; 
Comments