雪花将所有列转换为大写

snowflake convert all columns to uppercase

我有 SP 查看信息模式列 table 并将小写列转换为大写。无法在对象周围包裹双重代码。

ALTER TABLE SCHEMA_S.TBLNAME RENAME COLUMN ColName to COLNAME; //Won't work
ALTER TABLE "SCHEMA_S"."TBLNAME" RENAME COLUMN "ColName" to "COLNAME"; //this works

  select ('ALTER TABLE ' || TABLE_NAME || ' RENAME COLUMN "' || COLUMN_NAME || '" to "' || 
  UPPER(COLUMN_NAME) || '";') AS COL1 
  from information_schema.columns 
  WHERE TABLE_NAME = 'TBLNAME' AND TABLE_SCHEMA = 'SCHEMA_S'
  AND COLUMN_NAME != UPPER(COLUMN_NAME)  //this works in Snowflake

但在javascript内,不能使用双引号 javascript

    var table_control = " SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.' ,TABLE_NAME, ' RENAME COLUMN ', COLUMN_NAME, ' to ', UPPER(COLUMN_NAME), ';') AS COL1 " 
        table_control += "  FROM INFORMATION_SCHEMA.COLUMNS "
        table_control += " WHERE TABLE_SCHEMA = " + String.fromCharCode(39)  + CURRENT_SCHEMA + String.fromCharCode(39)
        table_control += "   AND TABLE_NAME   = " + String.fromCharCode(39)  + CURRENT_TABLE + String.fromCharCode(39)
        table_control += "   AND COLUMN_NAME != UPPER(COLUMN_NAME) " ; //this works

    var table_control = " SELECT CONCAT('ALTER TABLE "', TABLE_SCHEMA,'"."' ,TABLE_NAME, '" RENAME COLUMN "', COLUMN_NAME, '" to "', UPPER(COLUMN_NAME), '";') AS COL1 " 
        table_control += "  FROM INFORMATION_SCHEMA.COLUMNS "
        table_control += " WHERE TABLE_SCHEMA = " + String.fromCharCode(39)  + CURRENT_SCHEMA + String.fromCharCode(39)
        table_control += "   AND TABLE_NAME   = " + String.fromCharCode(39)  + CURRENT_TABLE + String.fromCharCode(39)
        table_control += "   AND COLUMN_NAME != UPPER(COLUMN_NAME) " ; //won't work

我在下面, JavaScript 编译错误:未捕获语法错误:CONVERT_TBL_COLUMNS_UPPERCASE 中的意外字符串,位于 'var table_control = " SELECT CONCAT('ALTER TABLE "', TABLE_SCHEMA,'" ."' ,TABLE_NAME, '" RENAME COLUMN "', COLUMN_NAME, '" to "', UPPER(COLUMN_NAME), '";') AS COL1 " ' 位置 58

底线 - 需要通过双重检查连接对象 (columns/schema/table),以便我可以通过 javascript.

动态重命名列名大写

请帮忙,我漏掉了一些简单的东西!

我创建了一个 JS UDF,它将列重命名为大写。

它使用 JS 模板字符串来避免转义引号的问题:

create or replace procedure alter_table_to_uppercase(
    TABLE_SCHEMA string,
    TABLE_NAME string
) returns variant
language javascript as $$

var select_cols = `
    select column_name
    from information_schema.columns
    where table_catalog = current_database()
    and table_schema = '${TABLE_SCHEMA}'
    and table_name = '${TABLE_NAME}'
    and column_name != upper(column_name)
`;

var statement1 = snowflake.createStatement( {sqlText: select_cols} );
var result_set1 = statement1.execute();

var cols_changed = [];

while (result_set1.next())  {
    var col_name = result_set1.getColumnValue(1);
    var alter_sql = `
        alter table ${TABLE_SCHEMA}.${TABLE_NAME}
        rename column "${col_name}" to "${col_name.toUpperCase()}"
    `
    snowflake.createStatement({sqlText: alter_sql}).execute();
    
    cols_changed.push(col_name);
}

return cols_changed;
$$

测试很简单:

create or replace table lowercased
as select 1 "a", 2 "b", 3 "c"
;

call alter_table_to_uppercase(current_schema(), 'LOWERCASED');

一些可能有助于解决您的问题的额外想法。