3

我正在尝试创建一个过程,该过程将复制表的多行(或仅单行)并为每一行插入增加 ID。

我的问题是,在我的过程中,我使用游标选择要复制的行,当我选择该游标中没有 WHERE 条件的所有行时,一切正常。但是当我设置 WHERE 条件以仅选择一行时……什么也没有发生

这是我的程序

CREATE OR REPLACE PROCEDURE DuplicateEmployee (p_EmployeeID IN Employee.id%TYPE)
 AS
  p_New_EmployeeID Employee.id%TYPE;
  CURSOR c_DuplicateEmployee IS
    SELECT *
        FROM Employee
        WHERE Employee.id = p_EmployeeID; -- if this line is deleted all content is duplicated
  row_Employee c_DuplicateEmployee%ROWTYPE;
    BEGIN
      FOR myEmployee IN c_DuplicateEmployee LOOP
          p_New_EmployeeID := employee_seq.NEXTVAL;
          INSERT INTO Employee(id, first_name, last_name, start_date, end_date, salary, city, description)
            VALUES(p_New_EmployeeID, myEmployee.first_name, myEmployee.last_name, myEmployee.start_date, myEmployee.end_date, myEmployee.salary, myEmployee.city, myEmployee.description);
      END LOOP;
    COMMIT;
   END DuplicateEmployee;

我知道在这个例子中,选择要复制的主键的过程是没有意义的,但在我的生产基地中,它将用于选择外键。

下面是创建测试表所需的代码和我用于此过程的 SEQUENCE

CREATE TABLE Employee
  (
    ID         VARCHAR2(4 BYTE) NOT NULL,
    First_Name VARCHAR2(10 BYTE),
    Last_Name  VARCHAR2(10 BYTE),
    Start_Date DATE,
    End_Date DATE,
    Salary      NUMBER(8,2),
    City        VARCHAR2(10 BYTE),
    Description VARCHAR2(15 BYTE)
  );
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
  VALUES
    ('01', 'Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Programmer');
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
  VALUES
    ('02', 'Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver', 'Tester');
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
  VALUES
    ('03', 'James', 'Smith', to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver', 'Tester');
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
  VALUES
    ('04', 'Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver', 'Manager');
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
  VALUES
    ('05', 'Robert', 'Black', to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver', 'Tester');
 INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
  VALUES
    ('06', 'Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78, 'New York', 'Tester');
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
VALUES
    ('07', 'David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78, 'New York', 'Manager');
INSERT
INTO Employee
    (ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
VALUES
    ('08', 'James', 'Cat', to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78, 'Vancouver', 'Tester');

这里是管理主键(ID)的序列

CREATE SEQUENCE "TEST"."EMPLOYEE_SEQ" MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE ;

这里是执行程序的代码

BEGIN
  employeepackage.duplicateemployee(5);
END;

我真的不明白为什么它在复制所有行时不能正常工作?对少于 2 行的游标有限制吗?

任何帮助将非常感激 ;)

4

2 回答 2

7

Why do you need a cursor? You can do this with SQL directly:

INSERT INTO Employee(id, first_name, last_name, 
                     start_date, end_date, 
                     salary, city, description)
SELECT employee_seq.NEXTVAL, e.first_name, e.last_name,
       e.start_date, e.end_date, 
       e.salary, e.city, e.description
FROM Employee e
WHERE e.id = p_EmployeeID;

Anyway, the actual problem is that your ID is a VARCHAR2(4), whereas you think it is a NUMBER. You actually do not have an employee with ID = 5, but you do have one with ID = '05'. So without changing anything, your procedure already works:

BEGIN
  employeepackage.duplicateemployee('05');
END;

Of course, it would make sense to change the data type of ID.

于 2012-05-14T14:43:00.533 回答
0

卢卡斯的解决方案如果我的第一个和最后一个表都很好,不需要调用其他程序来复制多个孩子,但对于我使用的中间表:

PROCEDURE Duplicate_Company (p_IdCity IN City.IdCity%TYPE) AS
p_New_IdCompany Company.IdCompany%TYPE;
CURSOR c_DuplicateCompany IS
SELECT *
    FROM Company c
    WHERE c.IdCity = p_IdCity; 
row_Company c_DuplicateCompany%ROWTYPE;
BEGIN
  FOR c1 IN c_DuplicateCompany LOOP
      p_New_IdCompany := company_seq.NEXTVAL;
      INSERT INTO Company(IdCompany, IdCity, Name, CreationDate)
        VALUES(p_New_IdCompany, c1.IdCity, c1.Name, c1.CreationDate);
      -- Call the procedure to duplicate current employee
      Duplicate_Employee(c1.IdCompany);
  END LOOP;
END Duplicate_Company;

这是一个好方法吗?

于 2012-05-15T12:06:25.550 回答