插入一个递增值

时间:2015-12-09 16:42:24

标签: sql oracle primary-key increment

我能想到的最好的解释方法是:我试图给每个机器人一个独特的身份。然后,当我运行程序时,我只需要给机器人一个名字和id。状态是为我生成的。

但是,我遇到了一个完全障碍,因为我找不到正确的方法来做到这一点。我已经尝试过对行进行计数并将其放入tempid中。这不起作用。每次运行时都会生成一个新行。所以,我应该总是得到一个新的身份证。我可能做错了。

我收到此错误:

  

错误(23,44):PL / SQL:ORA-00984:此处不允许列

以下是程序:

CREATE OR replace PROCEDURE Checkbot
(nameinput IN VARCHAR2)
IS
  partfound NUMBER(4);
  foundall BOOLEAN;
  tempid NUMBER;
BEGIN

--Where I am having issues:

  SELECT Count(*)
  INTO tempid
  FROM robotinventory;
  INSERT INTO robotinventory VALUES (tempid, nameinput, NULL);

  foundall := TRUE;
  FOR i IN 1..8 LOOP
    partfound := 0;
    SELECT Max(prtserial)
    INTO partfound
    FROM partinventory
    WHERE parttypeid = i;

    IF partfound > 0 THEN
      DELETE FROM partinventory
      WHERE prtserial = partfound;
      INSERT INTO robotprt VALUES (tempid, idinput, i);
    ELSE
      foundall := FALSE;
    END IF;
  END LOOP;


    IF foundall THEN
      UPDATE robotinventory
      SET status = 'ready for assembly'
      WHERE robotid = tempid;
    ELSE
      UPDATE robotinventory
      SET status = 'waiting on parts'
      WHERE robotid = tempid;
    END IF;

  END;/

机器人库存表:

CREATE TABLE RobotInventory
(RobotID Number(4) PRIMARY KEY,
RobotName VARCHAR2(24),
Status VARCHAR2(64));

我的代码的早期版本,取出了我在我的错误,我的目标是用自动递增的数字替换idInput:

create or replace procedure checkbot
(idInput in number, nameInput in varchar2)
is
  partFound number(4);
  foundAll boolean;
begin
  insert into robotInventory values (idInput, nameInput, null);
  foundAll := true;
  for i in 1..8 loop
    partFound := 0;
    select max(prtSerial)
    into partFound
    from partInventory
    where ParttypeID = i;

    if partFound > 0 then
      delete from partInventory
      where prtSerial = partFound;
      insert into robotPrt values (partFound, idInput, i);
    else
      foundAll := false;
    end if;
  end loop;


    if foundAll then
      update robotInventory
      set status = 'ready for assembly'
      where robotID = idInput;
    else
      update robotInventory
      set status = 'waiting on parts'
      where robotID = idInput;
    end if;

  END;
  /

1 个答案:

答案 0 :(得分:1)

创建一个序列(下面称为seq)并在INSERT SQL中使用它来填充robot_id。

INSERT INTO robotinventory VALUES (seq.nextval, nameinput, NULL); 

查看有关如何更详细地创建序列的Oracle文档。这是一个可以根据您的需求进行更改的示例。

create sequence seq
start with 1
increment by 1
maxvalue 9999; 

HTH