首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oracle11g变量声明

Oracle11g变量声明
EN

Stack Overflow用户
提问于 2015-08-11 22:33:28
回答 2查看 135关注 0票数 3

我尝试通过id列(numeric类型而不是int类型)中的grater值来递增序列。为此,我尝试声明一个int变量,并从表中选择最大值,如下所示:

代码语言:javascript
复制
declare
        last_id int;
begin
        select max(id) into last_id from my_table;
        alter sequence my_table_seq increment by last_id;    
end;

然而,我得到了这个错误

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2015-08-11 22:43:32

代码语言:javascript
复制
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

anonymous block中的任何DDN使用execute immediate

票数 1
EN

Stack Overflow用户

发布于 2015-08-12 06:07:08

创建了类似的对象,并按预期工作。

建议您尝试以下操作:

1.将列从ID更改为something_id

2.删除模式和表名称中的双引号(只需提供schema.table_name)

3.确保语句末尾没有遗漏分号,并且正确声明了所有变量。如果仍然不起作用,那么使用试错法。

  1. 只需使用简单的块,并尝试单独打印ID值。然后,
  2. 继续添加您的逻辑(为last_id赋值,并尝试print last_id).
  3. Then,最后添加您的execute immediate语句,看看它是否有效。

代码语言:javascript
复制
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

输出

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31944682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档