我如何参数化 Lua 脚本以通过 table 值执行查询

How do I parametrize Lua script to go through table values executing queries

新 Lua 但正在尝试。

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

映射Table

目标架构 目标Table 来源架构 产地Table
架构 1 table1 架构 3 table3
架构 2 table2 架构4 table4

当前解决方案

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..[[']]

不确定语法,而且我想我需要遍历 table 的值。 谢谢

这是一个示例脚本:

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]