db2 Perl DBI和临时表

cu6pst1q  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(167)

我使用的是Perl DBI和DB2。
当我运行这段代码时:

sub MergePolygonNameTable()
{
   my $table = "THESCHEMA.NAME";

   print "Merging into ${table} table. ", scalar localtime, "\n";
   eval
   {
      $DbHandle->do("
      declare global temporary table session.TEMP_NAME
      (POLICY_MASTER_ID INT
      )
      ;
      ");

      $DbHandle->do("
         CREATE UNIQUE INDEX session.TEMP_NAME_IDX1 ON session.TEMP_NAME
         (POLICY_MASTER_ID ASC
         )");
     $DbHandle->do("
      insert into session.TEMP_NAME
      (POLICY_MASTER_ID
      )
      SELECT pm.ID as POLICY_MASTER_ID
        FROM THESCHEMA.POLICY_MASTER pm

      ");
      $DbHandle->do("
      MERGE INTO THESCHEMA.NAME as t
      USING session.TEMP_NAME as s
         ON t.POLICY_MASTER_ID = s.POLICY_MASTER_ID
      WHEN MATCHED
      ) THEN
      UPDATE SET t.UPDATED_DATETIME = CURRENT_TIMESTAMP
      WHEN NOT MATCHED THEN
      INSERT
      (POLICY_MASTER_ID
      ) VALUES
      (s.POLICY_MASTER_ID
      )
        ;
      ");
   };
   if ($@)
   {
      print STDERR "ERROR: $ExeName: Cannot merge into ${table} table.\n$@\n";
      ExitProc(1);
   }
}

问题是运行后THESCHEMA.NAME为空。
我怀疑DBI在do()之后不保存临时表的内容,但是DBI不允许我在do()中放置多个语句。
如何使临时表在DBI中工作?

djmepvbi

djmepvbi1#

ON COMMIT DELETE ROWSDECLARE GLOBAL TEMPORARY TABLE statement的默认选项。
使用ON COMMIT PRESERVE ROWS选项可在显式或隐式COMMIT时保留行。
就像这样:

declare global temporary table session.TEMP_NAME
      (POLICY_MASTER_ID INT
      )
      ON COMMIT PRESERVE ROWS
      ;

相关问题