【发布时间】:2020-07-23 16:03:55
【问题描述】:
我正在关注本教程:https://docs.snowflake.com/en/sql-reference/functions/validate.html
尝试“通过查询 ID 返回错误并将结果保存到表中以供将来参考”
但是,为了实现无缝传输,我不想总是输入作业 ID,因为它需要我转到雪花控制台 - 转到历史记录 - 获取作业 ID - 复制并将其粘贴到 python 代码中。
相反,我只想使用作为变量的表名和“last_query_id()”并给我列表错误。有什么办法可以实现吗?
import snowflake.connector
tableName='F58155'
ctx = snowflake.connector.connect(
user='*',
password='*',
account='*')
cs = ctx.cursor()
ctx.cursor().execute("USE DATABASE STORE_PROFILE_LANDING")
ctx.cursor().execute("USE SCHEMA PUBLIC")
try:
ctx.cursor().execute("PUT file:///temp/data/{tableName}/* @%
{tableName}".format(tableName=tableName))
except Exception:
pass
ctx.cursor().execute("truncate table {tableName}".format(tableName=tableName))
ctx.cursor().execute("COPY INTO {tableName} ON_ERROR = 'CONTINUE' ".format(tableName=tableName,
FIELD_OPTIONALLY_ENCLOSED_BY = '""', sometimes=',', ERROR_ON_COLUMN_COUNT_MISMATCH = 'TRUE'))
我已经尝试了下面的验证功能....它在这一行上给了我错误
错误是“SQL 编译错误: 位置 74 处的语法错误第 1 行意外的“tableName”。 位置 83 处的第 1 行语法错误意外'}'。”
ctx.cursor().execute("create or replace table save_copy_errors as select * from
table(validate({tableName},'select last_query_id()'))");
ctx.close()
【问题讨论】:
-
错误信息是什么?
-
@demircioglu SQL 编译错误:位置 74 处的语法错误第 1 行意外'tableName'。第 1 行语法错误,位置 83 意外'}'。
-
您的 SQL 构造不正确。您要么需要
.format(tableName=tableName)来绑定变量,要么使用更易于阅读的 f 字符串表示法。所以它看起来像这样ctx.cursor().execute(f"create or replace table save_copy_errors as select * from table(validate({tableName},'select last_query_id()'))");。我也认为你需要job_id=>在tableName之后` -
@demircioglu 试过这个查询 --- ctx.cursor().execute(f"create or replace table save_copy_errors as select * from table(validate({tableName}, job_id=> 'select last_query_id( );'))") 。它给了我一个错误——“表函数的参数无效[提供了无效的作业 UUID。]。表函数参数必须是一个常量。”
-
在这种情况下运行 sql 来获取作业 UUID 并将其传递给 validate sql 以获取 job_id
job_id = ctx.cursor().execute("select last_query_id()").fetchone()[0]你可以运行 validate sql 为ctx.cursor().execute(f"create or replace table save_copy_errors as select * from table(validate({tableName},hob_id=>'{job_id}'))");
标签: python sql python-3.x validation snowflake-cloud-data-platform