简体   繁体   English

临时表不存储更新的值

[英]Temp table doesn't store updated values

I've been trying to create a temp table and update it but when I go to view the temp table, it doesn't show any of the updated rows我一直在尝试创建一个临时表并对其进行更新,但是当我 go 查看临时表时,它没有显示任何更新的行

declare global temporary table hierarchy (
code varchar(5)
description varchar(30);

INSERT INTO session.hierarchy
SELECT code, 30_description
FROM table1
WHERE code like '_....';

SELECT *
FROM session.hierarchy;

This is a frequently asked question.这是一个经常被问到的问题。

When using DGTT with Db2 (declare global temporary table), you need to know that the default is to discard all rows after a COMMIT action.将 DGTT 与 Db2(声明全局临时表)一起使用时,您需要知道默认是在COMMIT操作后丢弃所有行。 That is the reason the table appears to be empty after you insert - the rows got deleted if autocommit is enabled.这就是插入后表看起来为空的原因 - 如果启用了自动提交,行将被删除。 If that is not what you want, you should use the on commit preserve rows clause when declaring the table.如果这不是您想要的,您应该在声明表时使用on commit preserve rows子句。

It is also very important to the with replace option when creating stored procedures, this is often the most friendly for development and testing, and it is not the default.在创建存储过程的时候, with replace选项也很重要,这对于开发和测试来说往往是最友好的,并不是默认的。 Otherwise, if the same session attempts to repeat the declaration of the DGTT the second and subsequent attempts will fail because the DGTT already exists.否则,如果相同的 session 尝试重复 DGTT 的声明,则第二次和后续尝试将失败,因为 DGTT 已经存在。

It can also be interesting for problem determination sometimes to use on rollback preserve rows but that is less often used.有时on rollback preserve rows使用问题确定也很有趣,但这种情况不太常用。

When using a DGTT, one of the main advantages is that you can arrange for the population of the table (inserts, updates ) to be unlogged which can give a great performance boost if you have millions of rows to add to the DGTT.使用 DGTT 时,主要优点之一是您可以安排取消记录表的填充(插入、更新),如果您有数百万行要添加到 DGTT,这可以极大地提高性能。

Suggestion is therefore:因此建议是:

declare global temporary table ... (   )...
not logged
on commit preserve rows
with replace;

For DPF installations, also consider using distribute by hash (...) for best performance.对于 DPF 安装,还可以考虑使用distribute by hash (...)获得最佳性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM