我想使用UNION_ALL组合来自不同模式的表。这些表具有相同的模式,就像在这个玩具示例中一样:
class1.names
+----------+
| id | name|
+----------+
| 1 | jon |
| 2 | ann |
| 3 | rob |
class2.names
+----------+
| id | name|
+----------+
| 1 | rav |
| 2 | meg |
| 3 | mat |
我可以将类列表硬编码到数组中,或者更优选地,使用如下查询获取它们:
SELECT DISTINCT(TABLE_SCHEMA)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
我想像这样组合表格:
SELECT *, 'class1' as class FROM class1.names
UNION_ALL
SELECT *, 'class2' as class FROM class2.names
UNION_ALL
etc.
但是模式级查询中会有比SELECT *, 'class1'...
更多的内容,所以我想使用循环或其他系统方法来实现这一点。
我正在查看动态sql或使用GROUP_CONCAT与' UNION_ALL'作为分隔符,但我无法取得进展。
附录:我知道这是糟糕的架构设计,但我现在无法做任何事情。
答案 0 :(得分:0)
如果我对您的理解正确:
select listagg('select ' || f.value, ' union all ') from table(flatten(input => parse_json(
'[1, ,77]'))) f;
+----------------------------------------------+
| LISTAGG('SELECT ' || F.VALUE, ' UNION ALL ') |
|----------------------------------------------|
| select 1 union all select 77 |
+----------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.739s
和:
select 1 union all select 77;
+----+
| 1 |
|----|
| 1 |
| 77 |
+----+
2 Row(s) produced. Time Elapsed: 0.638s
答案 1 :(得分:0)
也许像这样:
var grid = $("#tblPresentationsTable").data('kendoGrid')
if(grid){
grid.destroy();
$("#tblPresentationsTable").empty()
}
$("#tblPresentationsTable tbody").html(tbodyPresentations)
$("#tblPresentationsTable").kendoGrid({
height: 550,
sortable: true,
toolbar: ["search"],
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
}
});
答案 2 :(得分:0)
在Snowflake中,此动态SQL:
with a as (
select * from information_schema.tables
where table_catalog like 'CLASS%' and table_name = 'NAMES' and table_type = 'BASE TABLE'
),
b as (
select *,
$$SELECT *, 'SCHEMA' as class FROM SCHEMA.names$$ as t,
replace(t,'SCHEMA',lower(table_schema)) as sql,
from a
)
select listagg(sql,'\nUNION ALL\n') within group (order by table_schema, table_catalog)
from b;
会产生:
SELECT *, 'class1' as class FROM class1.names
UNION ALL
SELECT *, 'class2' as class FROM class2.names
UNION ALL
etc.
$$$$$是字符串文字的单引号的替代方法。您也可以将单引号加倍来转义。