Oracle包中的自动增量

发布于 2024-11-13 01:40:21 字数 430 浏览 6 评论 0原文

我正在尝试在我的包上使用自动增量而不使用触发器..有人可以解释我如何在我的包中使用它..我这样做并没有起作用,它抱怨 abt 变量未声明或类型未分配..我看到其他自动增量问题,但没有人使用过在包上不触发的自动增量

 PROCEDURE insertExample   (  
user_id_in IN sample.seq_user_id.nextval,   
name_in IN sample.name%TYPE,     
age_in IN sample.age%TYPE   )   
IS    
BEGIN      
INSERT INTO sample    
 (seq_user_id.nextval, name, age)     
VALUES     
(user_id_in, name_in, age_in);    
END insertExample; 

I am trying to use autoincrement on my package without using trigger.. Can someone explain me how I have to use it in my package.. I did this way didnt work its complaining abt variable not being declared or type not assigned.. I saw other autoincrement questions but nobody has used auto increment without trigger on package

 PROCEDURE insertExample   (  
user_id_in IN sample.seq_user_id.nextval,   
name_in IN sample.name%TYPE,     
age_in IN sample.age%TYPE   )   
IS    
BEGIN      
INSERT INTO sample    
 (seq_user_id.nextval, name, age)     
VALUES     
(user_id_in, name_in, age_in);    
END insertExample; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假装爱人 2024-11-20 01:40:21

您想要允许传入自定义 user_id 还是始终使用该序列?

在第一种情况下,您需要类似的内容:

CREATE OR REPLACE PROCEDURE insertexample (
   user_id_in   in   sample.user_id%type,
   name_in      in   sample.name%type,
   age_in       in   sample.age%type
)
IS
BEGIN
   insert into sample
               (user_id, name, age
               )
        values (nvl(user_id_in, seq_user_id.nextval), name_in, age_in);
END insertexample; 

如果您总是想使用序列(这可能是正确的选择),只需取出该输入参数和 NVL:

CREATE OR REPLACE PROCEDURE insertexample (
   name_in      in   sample.name%type,
   age_in       in   sample.age%type
)
IS
BEGIN
   insert into sample
               (user_id, name, age
               )
        values (seq_user_id.nextval, name_in, age_in);
END insertexample; 

Are you wanting to allow a custom user_id to be passed in or always use the sequence?

In the first case, you would need something like:

CREATE OR REPLACE PROCEDURE insertexample (
   user_id_in   in   sample.user_id%type,
   name_in      in   sample.name%type,
   age_in       in   sample.age%type
)
IS
BEGIN
   insert into sample
               (user_id, name, age
               )
        values (nvl(user_id_in, seq_user_id.nextval), name_in, age_in);
END insertexample; 

If you always want to use the sequence (which is probably the right choice), just take out that input parameter and the NVL:

CREATE OR REPLACE PROCEDURE insertexample (
   name_in      in   sample.name%type,
   age_in       in   sample.age%type
)
IS
BEGIN
   insert into sample
               (user_id, name, age
               )
        values (seq_user_id.nextval, name_in, age_in);
END insertexample; 
丑疤怪 2024-11-20 01:40:21

你想要类似的东西

PROCEDURE insertExample   (  
name_in IN sample.name%TYPE,     
age_in IN sample.age%TYPE   )   
IS    
BEGIN      
  INSERT INTO sample    
   (user_id, name, age)     
  VALUES     
   (seq_user_id.nextval, name_in, age_in);    
END insertExample; 

You want something like

PROCEDURE insertExample   (  
name_in IN sample.name%TYPE,     
age_in IN sample.age%TYPE   )   
IS    
BEGIN      
  INSERT INTO sample    
   (user_id, name, age)     
  VALUES     
   (seq_user_id.nextval, name_in, age_in);    
END insertExample; 
救赎№ 2024-11-20 01:40:21

您距离可行的解决方案并不遥远:

PROCEDURE insertExample   (
  name_in IN sample.name%TYPE,     
  age_in  IN sample.age%TYPE   )   
IS    
BEGIN      
  INSERT INTO sample (user_id, name, age)     
  VALUES (seq_user_id.nextval, name_in, age_in);    
END insertExample;

我假设该表至少有三列,分别为 user_idnameage。此外,我假设您已经创建了一个名为 seq_user_id 的序列(CREATE SEQUENCE seq_user_id START WITH 1 INCRMENT BY 1)。

由于用户 ID 不会自动分配,因此它不再是参数列表的一部分。

Your not that far away from a working solution:

PROCEDURE insertExample   (
  name_in IN sample.name%TYPE,     
  age_in  IN sample.age%TYPE   )   
IS    
BEGIN      
  INSERT INTO sample (user_id, name, age)     
  VALUES (seq_user_id.nextval, name_in, age_in);    
END insertExample;

I'm assuming that the table has at least three columns called user_id, name and age. Furthermore, I'm assuming that you have already created a sequence called seq_user_id (CREATE SEQUENCE seq_user_id START WITH 1 INCREMENT BY 1).

As the user Id is not automatically assigned, it is no longer part of the parameter list.

调妓 2024-11-20 01:40:21

我会将您的过程表述为 FUNCTION,并将新的 ID 返回给调用者:

FUNCTION insert_user
(
    name_in IN users.name%TYPE,     
    age_in  IN users.age%TYPE   
)
    RETURN users.id%TYPE 
IS
    v_id users.id%TYPE;
BEGIN      
    INSERT INTO users (
        user_id, 
        name, 
        age
    ) VALUES (
        seq_user_id.nextval, 
        name_in, 
        age_in
    )
    RETURNING
        user_id
    INTO
        v_id
    ;    
    RETURN v_id;
END insert_user;

这通常是更有用的插入模式,特别是当您可能希望随后在其他表中插入子记录时。

I would phrase your procedure as a FUNCTION instead, and return the new ID to the caller:

FUNCTION insert_user
(
    name_in IN users.name%TYPE,     
    age_in  IN users.age%TYPE   
)
    RETURN users.id%TYPE 
IS
    v_id users.id%TYPE;
BEGIN      
    INSERT INTO users (
        user_id, 
        name, 
        age
    ) VALUES (
        seq_user_id.nextval, 
        name_in, 
        age_in
    )
    RETURNING
        user_id
    INTO
        v_id
    ;    
    RETURN v_id;
END insert_user;

This is generally a more useful pattern for inserts, especially where you may want to subsequently insert child records in other tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文