雪花中的无效标识符编程错误

时间:2021-04-01 08:15:02

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

我在雪花中运行以下查询并且运行良好。

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”

1 个答案:

答案 0 :(得分:0)

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

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)