我正在尝试通过id列(数字类型但不是int)的grater值递增序列。为此,我试图声明一个int变量并从表中选择最大值,如下所示:
declare
last_id int;
begin
select max(id) into last_id from my_table;
alter sequence my_table_seq increment by last_id;
end;
但是,我收到此错误
ORA-06550: line 2, column 19:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
:= . ( @ % ; not null range default character
我正在使用Oracle 11g。这是一个screenshot
答案 0 :(得分:1)
declare
last_id number;
begin
select max(id) into last_id from my_table;
execute immediate 'alter sequence my_table_seq increment by ' || to_char(last_id);
end;
使用oracle Numeric Data Types中的任何一个而不是int
对execute immediate
DDN
使用anonymous block
答案 1 :(得分:1)
创建了类似的对象,它按预期工作。
建议您尝试以下方法:
1.将您的专栏从
ID
更改为something_id
。2.从架构和表名中删除双引号(只需给
schema.table_name
)3.确保在语句结尾处不会遗漏半结肠并且所有变量都被正确声明。
如果仍然无效,请选择试错法。
只需使用简单的块并尝试单独打印
ID
值。然后继续添加您的逻辑(将值分配给
last_id
并尝试打印last_id
)。- 醇>
然后最后添加您的
execute immediate
语句,看看它是否有效。
Create table general_discount_type
(id number);
-- Table created.
Begin
insert into general_discount_type values(101);
insert into general_discount_type values(102);
insert into general_discount_type values(103);
insert into general_discount_type values(104);
End;
-- Statement processed.
Create sequence general_discount_type_seq
start with 1
increment by 1
NOCACHE
NOCYCLE;
-- Sequence created.
Declare
last_id number;
Begin
select max(id) into last_id from MAHI_SCHEMA.general_discount_type;
execute immediate 'alter sequence MAHI_SCHEMA.general_discount_type_seq increment by '
|| to_char(last_id);
dbms_output.put_line('Value in last_id is : ' || last_id);
End;
-- Value in last_id is : 104
<强>输出强>