-使用python进行sql验证

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

我遵循本教程:https://docs.snowflake.com/en/sql-reference/functions/validate.html
尝试和“按查询id返回错误并将结果保存到表中以供将来参考”
但是对于无缝传输,我不希望总是放置作业id,因为它需要我转到snowflake控制台-转到history-获取作业id-复制并粘贴到python代码。
相反,我只想使用tablename(一个变量)和'last\u query\u id()',并给出错误列表。我有什么办法可以做到这一点吗?

  1. import snowflake.connector
  2. tableName='F58155'
  3. ctx = snowflake.connector.connect(
  4. user='*',
  5. password='*',
  6. account='*')
  7. cs = ctx.cursor()
  8. ctx.cursor().execute("USE DATABASE STORE_PROFILE_LANDING")
  9. ctx.cursor().execute("USE SCHEMA PUBLIC")
  10. try:
  11. ctx.cursor().execute("PUT file:///temp/data/{tableName}/* @%
  12. {tableName}".format(tableName=tableName))
  13. except Exception:
  14. pass
  15. ctx.cursor().execute("truncate table {tableName}".format(tableName=tableName))
  16. ctx.cursor().execute("COPY INTO {tableName} ON_ERROR = 'CONTINUE' ".format(tableName=tableName,
  17. FIELD_OPTIONALLY_ENCLOSED_BY = '""', sometimes=',', ERROR_ON_COLUMN_COUNT_MISMATCH = 'TRUE'))

我试过下面的验证函数…它给我这行的错误
错误为“sql编译错误:语法错误行1,位置74意外的'tablename'。语法错误行1位于位置83“}”

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

tsm1rwdh1#

线路

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

应该换成这两个

  1. job_id = ctx.cursor().execute("select last_query_id()").fetchone()[0]
  2. ctx.cursor().execute(f"create or replace table save_copy_errors as select * from
  3. table(validate({tableName},job_id=>'{job_id}'))");

相关问题