我使用的是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中工作?
1条答案
按热度按时间djmepvbi1#
ON COMMIT DELETE ROWS
是DECLARE GLOBAL TEMPORARY TABLE statement的默认选项。使用
ON COMMIT PRESERVE ROWS
选项可在显式或隐式COMMIT
时保留行。就像这样: