new with Lua but trying.
I have multiple "Create table" queries which I need to execute, what changes only is Schema and Table name. At the moment I am explicitly defining each query. I want to parametrize Lua script from the table below passing table name as argument, since there is 100+ tables which needs to be generated this way.
MappingTable
targetSchema | targetTable | originSchema | originTable |
---|---|---|---|
schema1 | table1 | schema3 | table3 |
schema2 | table2 | schema4 | table4 |
Current solution
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;
]])
What I've tried:
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..[[']]
Not sure about the syntax, also I guess I need to loop through the values of the table. Thank you
Here is a sample script:
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
/
You may want to read values from map_table
the other way.
In case when you have case-sensitive column names:
targetSchema = map_table[i]."targetSchema"
, targetTable = map_table[i]."targetTable"
, originSchema = map_table[i]."originSchema"
, originTable = map_table[i]."originTable"
In case when you are sure in column order and don't want to worry about column names:
targetSchema = map_table[i][1]
, targetTable = map_table[i][2]
, originSchema = map_table[i][3]
, originTable = map_table[i][4]