I am doing some changes on my table and I couldn't figure out the problem. This is my SQL script;
ALTER TABLE X ALTER COLUMN X_ID RESTART WITH (SELECT MAX(X_ID) FROM X);
Altough I used AS
instead of WITH
and tried other combinations, I couldn't find the exact syntax. (By the way, I cannot set this property in the initialization, I got to do it after creation of the table )
答案 0 :(得分:2)
查看ALTER TABLE的语法时,您会发现只能使用常量,例如" RESTART WITH 12345"。查询本身是不可能的。对于自动化,您需要将其分解,使用变量,生成ALTER语句并执行它。
答案 1 :(得分:0)
假设这是针对DB2 for LUW,您可以使用一些动态SQL自动执行重置身份值的过程:
begin
declare curmax bigint;
for r as select tabschema, tabname, colname, nextcachefirstvalue, seqid
from syscat.colidentattributes where tabschema = current_schema
do
prepare s from
'select max(' || r.colname || ') from ' ||
rtrim(r.tabschema) || '.' || r.tabname;
begin
declare c cursor for s;
open c;
fetch c into curmax;
close c;
end;
if curmax >= r.nextcachefirstvalue
then
execute immediate
'alter table ' || rtrim(r.tabschema) || '.' || r.tabname ||
' alter column ' || r.colname || ' restart with ' || varchar(curmax+1);
end if;
end for;
end
如果您的身份不是整数,则可能需要更改curmax
的数据类型,并针对syscat.colidentattributes
调整查询以使用相应的模式名称。