【问题标题】:Invalid Identifier programming error in snowflake雪花中的无效标识符编程错误
【发布时间】:2021-06-28 05:33:52
【问题描述】:

我在雪花中运行以下查询,它运行良好。

set id ='TEST_TABLE1';
set time_s = '2021-03-31 06:52:51+00:00';

merge into TEST_STATUS using (select column1 AS TABLENAME, 
                              column2 AS LASTUPDATED from values ($id,$time_s)) tt
                              on TEST_STATUS.TABLE_NAME = tt.TABLENAME 
                              when matched then update set TEST_STATUS.LAST_UPDATED = tt.LASTUPDATED 
                              when not matched then insert (TABLE_NAME, LAST_UPDATED) values (tt.TABLENAME, tt.LASTUPDATED) 

但是当我尝试通过以下 python 代码运行它时:


self.table = 'TEST_TABLE1'

self.timestamp='2021-03-31 06:52:51+00:00';

cmd = f"set id ={self.table};"
cmd2 = f"set time_s = str({timestamp});"


merge_cmd = f"merge into {self.table} using (select column1 AS TABLENAME, column2 AS LASTUPDATED from " \
f"values ($id,$time_s)) tt on {self.table}.TABLE_NAME = tt.TABLENAME when " \
f"matched then update set {self.status_tbl}.LAST_UPDATED = tt.LASTUPDATED when not matched then " \ 
f"insert (TABLE_NAME, LAST_UPDATED) values (tt.TABLENAME, tt.LASTUPDATED) "
        
self.snowflake_client.run(cmd)
self.snowflake_client.run(cmd2)
self.snowflake_client.run(merge_cmd)

我遇到了异常:

snowflake.connector.errors.ProgrammingError: 000904 (42000): SQL 编译错误: 位置 14 的错误行 1 无效标识符“TEST_TABLE1”

【问题讨论】:

    标签: python python-3.x snowflake-cloud-data-platform snowflake-schema


    【解决方案1】:

    给变量赋值时可以加单引号吗?

    self.table = 'TEST_TABLE1'
    
    self.timestamp='2021-03-31 06:52:51+00:00'
    
    cmd = f"set id = '{self.table}';"
    cmd2 = f"set time_s = 'str({timestamp})';"
    
    merge_cmd = f"merge into {self.table} using (select column1 AS TABLENAME, column2 AS LASTUPDATED from " \
    f"values ('$id','$time_s')) tt on {self.table}.TABLE_NAME = tt.TABLENAME when " \
    f"matched then update set {self.status_tbl}.LAST_UPDATED = tt.LASTUPDATED when not matched then " \ 
    f"insert (TABLE_NAME, LAST_UPDATED) values (tt.TABLENAME, tt.LASTUPDATED) "
            
    self.snowflake_client.run(cmd)
    self.snowflake_client.run(cmd2)
    self.snowflake_client.run(merge_cmd)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2021-12-27
      • 2021-04-22
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多