【问题标题】:Transaction in snowflake stored procedures雪花存储过程中的事务
【发布时间】:2021-03-17 16:05:10
【问题描述】:

我在雪花中的第一个存储过程,尝试在其中一个示例存储过程中使用事务,请帮助修复错误,为什么? “未捕获的 SyntaxError:‘BEGIN TRANSACTION;’的 GET_ROW_COUNT 中出现意外标识符;位置 8" 尝试关注文档:https://docs.snowflake.com/en/sql-reference/transactions.html

这是过程。

  create or replace procedure get_row_count(table_name VARCHAR)
  returns float not null
  language javascript
  as
  $$
  var row_count = 0;
  BEGIN TRANSACTION;
  // Dynamically compose the SQL statement to execute.
  var sql_command = "select count(*) from " + TABLE_NAME;
  // Run the statement.
  var stmt = snowflake.createStatement(
         {
         sqlText: sql_command
         }
      );
  var res = stmt.execute();
  // Get back the row count. Specifically, ...
  // ... get the first (and in this case only) row from the result set ...
  res.next();
  // ... and then get the returned value, which in this case is the number of
  // rows in the table.
  row_count = res.getColumnValue(1);
  return row_count;
  COMMIT ;
  $$
  ;

【问题讨论】:

    标签: snowflake-cloud-data-platform


    【解决方案1】:

    您需要使用 snowflake.execute() 调用 COMMIT 和 BEGIN TRANSACTION,因为它们是 SQL 命令:

     create or replace procedure get_row_count(table_name VARCHAR)
      returns float not null
      language javascript
      as
      $$
      var row_count = 0;
      snowflake.execute({ sqlText: 'BEGIN TRANSACTION' });
      // Dynamically compose the SQL statement to execute.
      var sql_command = "select count(*) from " + TABLE_NAME;
      // Run the statement.
      var stmt = snowflake.createStatement(
             {
             sqlText: sql_command
             }
          );
      var res = stmt.execute();
      // Get back the row count. Specifically, ...
      // ... get the first (and in this case only) row from the result set ...
      res.next();
      // ... and then get the returned value, which in this case is the number of
      // rows in the table.
      row_count = res.getColumnValue(1);
    
      snowflake.execute({ sqlText: 'COMMIT' }); 
      return row_count;
      $$
      ;
    

    【讨论】:

    • 非常感谢,在 Snowflake 文档中,它仅被提及为 BEGIN 和 COMMIT,因为该语言不是基于 java 脚本的,它是普通的雪花。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2021-10-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多