-1

我有一个存储过程,我需要从一个表中获取所有 id,并在其他表中插入带有这些 id 的新行,但我不太了解函数光标

PROCEDURE INSERTMDCGENERAL AS

 idCat NUMERIC;
 CURSOR cur IS
    SELECT ID_CAT_FILTROS_TALENTO into idCat FROM MDC_CAT_FILTROS_TALENTO;

  BEGIN 

    FOR v_reg IN cur LOOP
      INSERT INTO MDC_FILTROS_TALENTO(ID_FILTRO,ID_CAT_FILTROS_TALENTO)
               VALUES(SEC_MDC_FILTROS_TALENTO.NextVal,idCat);    
    END LOOP;

COMMIT;

END INSERTMDCGENERAL;
4

2 回答 2

2

做比以下更复杂的事情很少有任何意义:

PROCEDURE INSERTMDCGENERAL AS
BEGIN 

      INSERT INTO MDC_FILTROS_TALENTO 
           (ID_FILTRO,ID_CAT_FILTROS_TALENTO)
      SELECT SEC_MDC_FILTROS_TALENTO.NextVal
             , ID_CAT_FILTROS_TALENTO 
      FROM MDC_CAT_FILTROS_TALENTO;

      COMMIT;

END INSERTMDCGENERAL;

这在大多数情况下应该有效。只有当您处理数百万行时,您才可能需要对此进行修饰。即使那样,您也不应该使用游标循环和逐行处理:那效率要低得多。

于 2013-10-17T20:12:57.690 回答
1

This might be what you are expecting...

PROCEDURE INSERTMDCGENERAL AS
 idCat NUMERIC;
 CURSOR cur IS  SELECT ID_CAT_FILTROS_TALENTO FROM MDC_CAT_FILTROS_TALENTO;

  BEGIN 
  open cur;
  loop
    fetch cur into idCat;
    exit when cur%notfound;
      INSERT INTO MDC_FILTROS_TALENTO(ID_FILTRO,ID_CAT_FILTROS_TALENTO)
               VALUES(SEC_MDC_FILTROS_TALENTO.NextVal,idCat);    
  END LOOP;
  close cur;
 COMMIT;

 END INSERTMDCGENERAL;
于 2014-03-11T09:21:21.143 回答