I tried already multiple ways to achieve the follwing in spoon, learned a lot, but eventually failed:
The task is to perform the same query to a list of databases (all in the same connection).
I simply created a new table "list" with the output of "Show databases like '%prd%' ; "
What I tried, and what failed up to now:
1) using ? parameters in the query and configure it to use the input of list to resolve the ?s worked fine, but the DB doesn't allow to to a select x,z from ?.tablename. The reason is clear and understandable.
2) following tips in the web, I attempted to prepare a statement, in which the db name is resolved prior to preparation:
example:
set @db_name = ?;
SET @sql_text = concat('SELECT \'',@db_name, '\' AS db_name, DATE_ADD(LAST_DAY(DATE_SUB(current_date, INTERVAL 2 MONTH)), INTERVAL 1 DAY) as ts,',
'max(nb_sessions_per_collection_time) as max_session FROM ( select sum(value) as nb_sessions_per_collection_time from ',@db_name,'.stats_per_host_hist ',
'where stats_name_id = 86 and collection_time BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(current_date, INTERVAL 2 MONTH)), INTERVAL 1 DAY) ',
'AND DATE_ADD(LAST_DAY(DATE_SUB(current_date, INTERVAL 1 MONTH)), INTERVAL 1 DAY) ',
'group by collection_time ) as dummy ;');
select @sql_text ;
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
That works for me in the plain (my)sql editor as a workaround, but it produces an error when using
-> it seems this element can deal with "select..." only.
3) "Execute SQL statement" usage failed too, since it doesn't allow output rows
4) stored procedure on dB is not feasible (access rights)
5) so the story with using variables started, again following tips in the web
I created two transformatinos, one to get the list from the table and store it with "Set Variables" and a second one with "Get Variables" - Query - "Insert / Update". Additionally a job to execute them in sequence. While the variables and parameters topic is a bit crude, I finally got it and had to face the following problem: under each circumstances and variations i tried, only one row of input can be handled to Set variables. The step name is misleading. Looping over each row is not possible.
My feeling is, the "execute per row" feature was hooked on later, for some of the element is not cleanly thought through. missing is a a kind of fork or loop statement, which generally enables loops for all elements - did I overlook that?
There were smaller attempts and variations in between, but that is where I finally had to give up.
I can of course use i.e. a perl script to do this quite easy. Looping over the dbs would be easy. But it's not elegant to "outsource" - i.e. due to connection and scheman duplication.
Also, because I cannot believe nobody else found a solution for my problem. Is it really true, that
"select dbname from dblist; (or show tables;)" -> "select x,z,'hello' from $dbname.table"
is not doable with spoon?
Thanks
The task is to perform the same query to a list of databases (all in the same connection).
I simply created a new table "list" with the output of "Show databases like '%prd%' ; "
What I tried, and what failed up to now:
1) using ? parameters in the query and configure it to use the input of list to resolve the ?s worked fine, but the DB doesn't allow to to a select x,z from ?.tablename. The reason is clear and understandable.
2) following tips in the web, I attempted to prepare a statement, in which the db name is resolved prior to preparation:
example:
set @db_name = ?;
SET @sql_text = concat('SELECT \'',@db_name, '\' AS db_name, DATE_ADD(LAST_DAY(DATE_SUB(current_date, INTERVAL 2 MONTH)), INTERVAL 1 DAY) as ts,',
'max(nb_sessions_per_collection_time) as max_session FROM ( select sum(value) as nb_sessions_per_collection_time from ',@db_name,'.stats_per_host_hist ',
'where stats_name_id = 86 and collection_time BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(current_date, INTERVAL 2 MONTH)), INTERVAL 1 DAY) ',
'AND DATE_ADD(LAST_DAY(DATE_SUB(current_date, INTERVAL 1 MONTH)), INTERVAL 1 DAY) ',
'group by collection_time ) as dummy ;');
select @sql_text ;
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
That works for me in the plain (my)sql editor as a workaround, but it produces an error when using
3) "Execute SQL statement" usage failed too, since it doesn't allow output rows
4) stored procedure on dB is not feasible (access rights)
5) so the story with using variables started, again following tips in the web
I created two transformatinos, one to get the list from the table and store it with "Set Variables" and a second one with "Get Variables" - Query - "Insert / Update". Additionally a job to execute them in sequence. While the variables and parameters topic is a bit crude, I finally got it and had to face the following problem: under each circumstances and variations i tried, only one row of input can be handled to Set variables. The step name is misleading. Looping over each row is not possible.
My feeling is, the "execute per row" feature was hooked on later, for some of the element is not cleanly thought through. missing is a a kind of fork or loop statement, which generally enables loops for all elements - did I overlook that?
There were smaller attempts and variations in between, but that is where I finally had to give up.
I can of course use i.e. a perl script to do this quite easy. Looping over the dbs would be easy. But it's not elegant to "outsource" - i.e. due to connection and scheman duplication.
Also, because I cannot believe nobody else found a solution for my problem. Is it really true, that
"select dbname from dblist; (or show tables;)" -> "select x,z,'hello' from $dbname.table"
is not doable with spoon?
Thanks