Javascript 在 Snowflake 中创建表的过程
Javascript Procedure to create tables in Snowflake
我已尝试使用以下代码创建表数组中列出的表,但出现错误:
JavaScript compilation error: Uncaught SyntaxError: Unexpected end of input in CREATE_TABLES_SP at '
CREATE OR REPLACE PROCEDURE create_tables_sp() //Procedure to create tables
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var tables = ['table_a', 'table_b', 'table_c'];
for (var table of tables){
try {
snowflake.execute (
{sqlText: "create or replace table PROD_DB.schema1.${table} (ename varchar);"}
);
return "Succeeded."; // Return a success/error indicator.
}
catch (err) {
return "Failed: " + err; // Return a success/error indicator.
}
$$;
有人知道我错过了什么吗?
使用 IDENTIFIER 提供 table 名称作为“变量”并使用 binds
设置实际值:
CREATE OR REPLACE PROCEDURE create_table_sp()
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var tables = ['table_a', 'table_b', 'table_c'];
for (var table of tables) {
try {
snowflake.execute(
{
sqlText: `CREATE OR REPLACE TABLE IDENTIFIER(?)(ename VARCHAR);`
,binds: ['PROD_DB.schema1.' + table]
}
);
}
catch(err){
return "Failed: " + err;
};
};
return "Succeded.";
$$;
检查:
CALL create_table_sp();
SELECT * FROM PROD_DB.schema1.table_a;
SELECT * FROM PROD_DB.schema1.table_b;
SELECT * FROM PROD_DB.schema1.table_c;
${table}
(字符串插值)的建议用法也适用:
CREATE OR REPLACE PROCEDURE create_table_sp()
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var tables = ['table_a', 'table_b', 'table_c'];
for (var table of tables) {
try {
var sql = `CREATE OR REPLACE TABLE PROD_DB.schema1.${table} (ename VARCHAR);`;
snowflake.execute(
{ sqlText: sql }
);
}
catch(err){
return "Failed: " + err;
};
};
return "Succeded.";
$$;
但它不太安全并且容易受到 SQL 注入攻击。
我已尝试使用以下代码创建表数组中列出的表,但出现错误:
JavaScript compilation error: Uncaught SyntaxError: Unexpected end of input in CREATE_TABLES_SP at '
CREATE OR REPLACE PROCEDURE create_tables_sp() //Procedure to create tables
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var tables = ['table_a', 'table_b', 'table_c'];
for (var table of tables){
try {
snowflake.execute (
{sqlText: "create or replace table PROD_DB.schema1.${table} (ename varchar);"}
);
return "Succeeded."; // Return a success/error indicator.
}
catch (err) {
return "Failed: " + err; // Return a success/error indicator.
}
$$;
有人知道我错过了什么吗?
使用 IDENTIFIER 提供 table 名称作为“变量”并使用 binds
设置实际值:
CREATE OR REPLACE PROCEDURE create_table_sp()
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var tables = ['table_a', 'table_b', 'table_c'];
for (var table of tables) {
try {
snowflake.execute(
{
sqlText: `CREATE OR REPLACE TABLE IDENTIFIER(?)(ename VARCHAR);`
,binds: ['PROD_DB.schema1.' + table]
}
);
}
catch(err){
return "Failed: " + err;
};
};
return "Succeded.";
$$;
检查:
CALL create_table_sp();
SELECT * FROM PROD_DB.schema1.table_a;
SELECT * FROM PROD_DB.schema1.table_b;
SELECT * FROM PROD_DB.schema1.table_c;
${table}
(字符串插值)的建议用法也适用:
CREATE OR REPLACE PROCEDURE create_table_sp()
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var tables = ['table_a', 'table_b', 'table_c'];
for (var table of tables) {
try {
var sql = `CREATE OR REPLACE TABLE PROD_DB.schema1.${table} (ename VARCHAR);`;
snowflake.execute(
{ sqlText: sql }
);
}
catch(err){
return "Failed: " + err;
};
};
return "Succeded.";
$$;
但它不太安全并且容易受到 SQL 注入攻击。