如何在 DB2/400 中创建生成的列
How to create generated columns in DB2/400
我想创建一个将两列连接成 1 的虚拟列。我的尝试是:
--result sqlstate 42601 -104 (Token not valid: (. Valid token: IDENTITY)
alter table schema.table
add column name1_v generated always as (trim(name1) || ' ' || trim(vt_alt))
add column vtKuTx_v generated always as (trim(vtKuTx) || ' ' || trim(vt_alt))
;
根据文档,这应该有效。 ( https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/db2/rbafzpdf.pdf?view=kc )(第 851 页)。
有人知道如何完成这项工作吗?
提前致谢。
编辑:
我仔细检查了一下。这也不行。
create or replace table table.schema (
number int default 2,
square int generated always as (number * number)
);
尝试:
ALTER TABLE schema.table
ADD COLUMN name1_v DEFAULT trim(name1) || ' ' || trim(vt_alt)
ADD COLUMN vtKuTx_v DEFAULT trim(vtKuTx) || ' ' || trim(vt_alt)
;
在这里,我找到了一个教程......
https://www.db2tutorial.com/db2-basics/db2-alter-table-add-column/
注意:此答案适用于 DB2 LUW,而非 DB2-400。也许同样的道理也适用。
DB2 会阻止您无意中添加列,因为这可能是一项非常繁重的操作。相反,DB2 会强制您:
- 禁用约束。
- 添加列。
- 再次启用约束。
作为 DB2 SQL 编译器开发人员声明(原文如此):
Adding/altering a generated column is the only table action
which physically updates the table. To be more precise it will likely
update all zillion rows of it (which can be a lot in Viper with range
partitioning).
So rather than quitely filling up peoples logspace (or stressing the
auto-archival option) we decided it would be appropriate to perform such
heavy operations while the table is in check pending.
因此,您可以这样做:
set integrity for schema.table off;
alter table schema.table
add column name1_v generated as (trim(name1) || ' ' || trim(vt_alt))
add column vtKuTx_v generated as (trim(vtKuTx) || ' ' || trim(vt_alt))
;
set integrity for schema.table immediate checked force generated;
请参阅 db<>fiddle 中的 运行 示例。
寻求帮助时始终提供您的 Db2 版本(或在本例中为 i 系列的版本)。
当您使用 Db2 for i 时,您应该研究 ALTER TABLE i 系列平台的文档,然后在该页面上选择正确的 i 系列软件版本。
对于 ALTER TABLE
的 GENERATED 子句,i 系列文档在注释 5 中指定了以下限制:
5 GENERATED can be specified only if the column has a ROWID data type
(or a distinct type that is based on a ROWID data type), the column is
an identity column, identity-options are specified,
as-row-transaction-timestamp-clause is specified,
as-row-transaction-start-id-clause is specified, or the column is a
row change timestamp.
这可能是您的 -104 异常的原因。所以你需要找到一个替代方法来实现你的目标。
我想创建一个将两列连接成 1 的虚拟列。我的尝试是:
--result sqlstate 42601 -104 (Token not valid: (. Valid token: IDENTITY)
alter table schema.table
add column name1_v generated always as (trim(name1) || ' ' || trim(vt_alt))
add column vtKuTx_v generated always as (trim(vtKuTx) || ' ' || trim(vt_alt))
;
根据文档,这应该有效。 ( https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/db2/rbafzpdf.pdf?view=kc )(第 851 页)。 有人知道如何完成这项工作吗?
提前致谢。
编辑: 我仔细检查了一下。这也不行。
create or replace table table.schema (
number int default 2,
square int generated always as (number * number)
);
尝试:
ALTER TABLE schema.table
ADD COLUMN name1_v DEFAULT trim(name1) || ' ' || trim(vt_alt)
ADD COLUMN vtKuTx_v DEFAULT trim(vtKuTx) || ' ' || trim(vt_alt)
;
在这里,我找到了一个教程...... https://www.db2tutorial.com/db2-basics/db2-alter-table-add-column/
注意:此答案适用于 DB2 LUW,而非 DB2-400。也许同样的道理也适用。
DB2 会阻止您无意中添加列,因为这可能是一项非常繁重的操作。相反,DB2 会强制您:
- 禁用约束。
- 添加列。
- 再次启用约束。
作为 DB2 SQL 编译器开发人员声明(原文如此):
Adding/altering a generated column is the only table action which physically updates the table. To be more precise it will likely update all zillion rows of it (which can be a lot in Viper with range partitioning).
So rather than quitely filling up peoples logspace (or stressing the auto-archival option) we decided it would be appropriate to perform such heavy operations while the table is in check pending.
因此,您可以这样做:
set integrity for schema.table off;
alter table schema.table
add column name1_v generated as (trim(name1) || ' ' || trim(vt_alt))
add column vtKuTx_v generated as (trim(vtKuTx) || ' ' || trim(vt_alt))
;
set integrity for schema.table immediate checked force generated;
请参阅 db<>fiddle 中的 运行 示例。
寻求帮助时始终提供您的 Db2 版本(或在本例中为 i 系列的版本)。
当您使用 Db2 for i 时,您应该研究 ALTER TABLE i 系列平台的文档,然后在该页面上选择正确的 i 系列软件版本。
对于 ALTER TABLE
的 GENERATED 子句,i 系列文档在注释 5 中指定了以下限制:
5 GENERATED can be specified only if the column has a ROWID data type (or a distinct type that is based on a ROWID data type), the column is an identity column, identity-options are specified, as-row-transaction-timestamp-clause is specified, as-row-transaction-start-id-clause is specified, or the column is a row change timestamp.
这可能是您的 -104 异常的原因。所以你需要找到一个替代方法来实现你的目标。