すべてのテーブルの主キー列を、ALWAYSではなくGENERATEDBYDEFAULTに一時的に設定する必要があります。もちろん自分でリストをコンパイルすることもできますが、「ワンゴー」で特定のスキーマのすべてのテーブルにこれを設定することは可能ですか?ありがとう!
2 に答える
0
カタログからデータを照会することにより、照会を生成できます。つまり、alter コマンドを返す Select を作成すると、この出力がファイルに送信されます。そのファイルの内容を実行するだけで済みます
(Linux/UNIX/Cywgin)
db2 -x "select 'alter table ' || trim(tabschema) || '.' || trim(tabname) || ' alter column ' || colname || ' set generated by default as identity ;' from syscat.columns where tabschema like 'DB2INS%' and keyseq >= 0" > test.sql
db2 -tvf file.sql
ただし、生成されたコマンドを確認する必要があります。実行された場合は、返された値を確認してください。
db2 create table t1 \(c1 int not null primary key, c2 int, c3 int\)
db2 create table t2 \(c21 int references t1 \(c1\), c22 int\)
db2 create table t3 \(c30 int, c31 int not null, c32 int not null, c33 int\)
db2 alter table t3 add primary key \(c31, c32\)
db2 create table t4 \(c40 int, c41 int, c42 int, c43 int\)
db2 alter table t4 add constraint fk foreign key \(c42, c43\) references t3
db2 -x "select 'alter table ' || trim(tabschema) || '.' || trim(tabname) || ' alter column ' || colname || ' set generated by default as identity ;' from syscat.columns where tabschema like 'DB2INS%' and keyseq >= 0" > test.sql
db2 -tvf test.sql
alter table DB2INS01.T1 alter column C1 set generated by default as identity
DB20000I The SQL command completed successfully.
alter table DB2INS01.T3 alter column C31 set generated by default as identity
DB20000I The SQL command completed successfully.
alter table DB2INS01.T3 alter column C32 set generated by default as identity
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0372N A column with data type or attribute ROWID, IDENTITY, security label,
or row change timestamp can only be specified once for a table.
SQLSTATE=428C1
于 2013-03-01T15:44:38.687 に答える
0
メインフレーム DB2 を使用している場合は、このコマンドが機能するはずです (これは、キーが常に生成される ID 列であることを意味すると想定しています。デフォルトには他のコードがあり、情報センター ページで確認できます)。
SELECT
'ALTER TABLE ' ||
RTRIM(TBCREATOR) || '.' || RTRIM(TBNAME) ||
'ALTER COLUMN ' ||
RTRIM(NAME) ||
'SET GENERATED BY DEFAULT;'
FROM SYSIBM.SYSCOLUMNS
WHERE DEFAULT = 'I'
AND TBCREATOR = @schema
DB2 for LUW を使用している場合は、次のようにします。
SELECT
'ALTER TABLE ' ||
RTRIM(TABSCHEMA) || '.' || RTRIM(TABNAME) ||
'ALTER COLUMN ' ||
RTRIM(COLNAME) ||
'SET GENERATED BY DEFAULT;'
FROM SYSCAT.COLUMNS
WHERE GENERATED = 'A'
AND TABSCHEMA = @schema
これにより、必要なコマンドのみが生成されます。自分で実行する必要があります。
于 2013-03-01T14:21:32.647 に答える