-使用python进行sql验证

ojsjcaue  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(325)

我遵循本教程:https://docs.snowflake.com/en/sql-reference/functions/validate.html
尝试和“按查询id返回错误并将结果保存到表中以供将来参考”
但是对于无缝传输,我不希望总是放置作业id,因为它需要我转到snowflake控制台-转到history-获取作业id-复制并粘贴到python代码。
相反,我只想使用tablename(一个变量)和'last\u query\u 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编译错误:语法错误行1,位置74意外的'tablename'。语法错误行1位于位置83“}”

ctx.cursor().execute("create or replace table save_copy_errors as select * from 
table(validate({tableName},'select last_query_id()'))");

ctx.close()
tsm1rwdh

tsm1rwdh1#

线路

ctx.cursor().execute("create or replace table save_copy_errors as select * from 
table(validate({tableName},'select last_query_id()'))");

应该换成这两个

job_id = ctx.cursor().execute("select last_query_id()").fetchone()[0] 

ctx.cursor().execute(f"create or replace table save_copy_errors as select * from
table(validate({tableName},job_id=>'{job_id}'))");

相关问题