0

Lua 的新手,但正在尝试。

我有多个需要执行的“创建表”查询,仅更改架构和表名称。目前我正在明确定义每个查询。我想从下表中参数化 Lua 脚本,将表名作为参数传递,因为需要以这种方式生成 100 多个表。

映射表

目标模式 目标表 起源模式 原点表
架构1 表格1 图式3 表3
架构2 表2 图式4 表4

当前解决方案

CREATE LUA SCRIPT "ScriptName" () RETURNS ROWCOUNT AS
query([[
Create or replace table schema1.table1 as
select * from schema3.table3;
]])
query([[
Create or replace table schema2.table2 as
select * from schema4.table4;
]])

我试过的:

CREATE OR REPLACE LUA SCRIPT "ScriptName"('MappingTable') RETURNS ROWCOUNT AS
map_table = execute[[ SELECT * FROM .."'MappingTableName'"..;]] -- passing argument of the script, mapping table name

-- passing values from the columns

load =   [[Create or replace table ]]..
                  [[']]..targetSchema..[['.']].. 
                  [[']]..targetTable..]]..
                  [[as select * from]]..
                  [[']]..originSchema..[['.']]..
                  [[']]..originTable..[[']]

不确定语法,我想我也需要遍历表的值。谢谢

4

1 回答 1

0

这是一个示例脚本:

create or replace lua script ScriptName (
      t_MappingTable
    , s_ConditionColumn
    , s_ConditionValue
)
returns rowcount as

-- passing argument of the script, mapping table name
local map_table = query ([[
select * from ::MappingTable where ::ConditionColumn = :ConditionValue
]],{
      MappingTable = t_MappingTable
    , ConditionColumn = s_ConditionColumn
    , ConditionValue = s_ConditionValue
});

-- passing values from the columns
for i = 1, #map_table do
    query ([[
    create or replace table ::targetSchema.::targetTable as
    select * from ::originSchema.::originTable
    ]],{
          targetSchema = map_table[i].TARGETSCHEMA
        , targetTable  = map_table[i].TARGETTABLE
        , originSchema = map_table[i].ORIGINSCHEMA
        , originTable  = map_table[i].ORIGINTABLE
    });
end
/

您可能希望从map_table其他方式读取值。

如果您有区分大小写的列名:

  targetSchema = map_table[i]."targetSchema"
, targetTable  = map_table[i]."targetTable"
, originSchema = map_table[i]."originSchema"
, originTable  = map_table[i]."originTable"

如果您确定列顺序并且不想担心列名:

  targetSchema = map_table[i][1]
, targetTable  = map_table[i][2]
, originSchema = map_table[i][3]
, originTable  = map_table[i][4]
于 2021-06-14T20:50:32.317 回答