在Oracle的过程中使用%ROWTYPE

时间:2017-07-24 10:44:15

标签: oracle plsql

我正在尝试在我的代码中使用var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); var result; if (computerChoice < 0.34) { computerChoice = "rock"; } else if(computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log("Computer's choice: " + computerChoice); console.log("Your choice: " + userChoice); console.log(computerChoice + " vs " + userChoice); function compare(userChoice, computerChoice){ if (userChoice === computerChoice){ return "The result is a tie!"; } else if (userChoice ==="rock"){ if(computerChoice ==="scissors"){ return "rock wins!"; } else { return "paper wins!"; } } else if (userChoice === "paper") { if(computerChoice === "rock") { return "paper wins!"; } else { return "scissors wins!"; } } else if (userChoice === "scissors") { if(computerChoice === "rock") { return "rock wins!"; } else { return "scissors wins!"; } } } console.log(compare(userChoice, computerChoice)); 并尝试使用游标for循环将值插入其中,如下所示:

%ROWTYPE

当我尝试运行时,我得到以下错误:

CREATE or REPLACE PROCEDURE test_acr (
                  PROJECT_START_DATE IN DATE,USER_ID IN VARCHAR2) 
IS TYPE acr_new IS TABLE OF acr_projected_new%ROWTYPE 
        INDEX BY SIMPLE_INTEGER;  
acr_projected_neww acr_new;

CURSOR WEEKENDING_DATE IS  
      SELECT WEEKEND_DATE   
      FROM weekending_table 
      WHERE WEEKEND_DATE BETWEEN PROJECT_START_DATE AND sysdate;

BEGIN

FOR WEEKEND_DATE_REC in WEEKENDING_DATE LOOP 
INSERT INTO acr_projected_neww(WEEKEND_DATE,USERID,TIMESTAMP,ACR_PROJECTED,artificial_id)
       SELECT WEEKEND_DATE_REC.WEEKEND_DATE,USER_ID,sysdate,
                  (select sum(acr_h.activity_impact) 
       FROM ACR_HISTORY acr_h
       LEFT JOIN Activity act on act.activity_id = acr_h.activity_id
       LEFT JOIN Activity_Date_Duration act_d on act_d.activity_id = act.activity_id),1 from dual;

 END LOOP;
END test_acr;

我的要求是创建虚拟表并使用游标for循环将数据插入其中,如果没有,那么任何其他方法都会被理解。

请帮助我们将不胜感激!

4 个答案:

答案 0 :(得分:1)

在INSERT语句中看起来表名不正确。

答案 1 :(得分:0)

您无需使用两个查询。而是定义光标,使其包含要存储在集合中的记录的所有列。然后使用BULK COLLECT INTO代替插入,如图所示。将您的收藏定义为cursor%ROWTYPE

的表格
CREATE OR REPLACE PROCEDURE test_acr
IS
  CURSOR WEEKENDING_DATE
  IS
    SELECT a.col1,a.col2,b.col1,b.col2 ,c.col1
    from table1 a , table2 b LEFT JOIN table3 c;       --Here include all the data from the required tables.

TYPE acr_new
IS
  TABLE OF WEEKENDING_DATE%ROWTYPE;
  acr_projected_neww acr_new;
BEGIN
    FETCH WEEKENDING_DATE BULK COLLECT INTO acr_projected_neww;
END test_acr;

答案 2 :(得分:0)

如果你需要操纵你的数据(访问每一行 - 然后看下面的脚本,这是对嵌套表(PL / SQL集合)的顺序访问(插入)

CREATE or REPLACE PROCEDURE test_acr (PROJECT_START_DATE IN DATE,USER_ID IN VARCHAR2) 
 IS  
 TYPE acr_new 
 IS TABLE OF acr_projected_new%ROWTYPE; // nested table, notice absence of INDEX by clause
    acr_projected_neww acr_new :=  acr_projected_neww(); // instantiation, constructor call

CURSOR WEEKENDING_DATE IS  
      SELECT WEEKEND_DATE   
      FROM weekending_table 
      WHERE WEEKEND_DATE BETWEEN PROJECT_START_DATE AND sysdate;

BEGIN

FOR WEEKEND_DATE_REC in WEEKENDING_DATE 
   LOOP
    acr_new.extend;  // make room for the next element in collection
    acr_new(acr_new.last) := WEEKEND_DATE_REC; // Adding seq. to the end of collection 
    ... 

 END LOOP;
END test_acr;

但是如果你想要BULK INSERT(不需要访问每一行),请参阅下面的脚本

 CREATE or REPLACE PROCEDURE test_acr (PROJECT_START_DATE IN DATE,USER_ID IN VARCHAR2) 
  IS  
  TYPE acr_new IS TABLE OF acr_projected_new%ROWTYPE; // no INDEX BY clause
  acr_projected_neww acr_new = acr_new(); // Notice constructor call

CURSOR WEEKENDING_DATE IS  
      SELECT WEEKEND_DATE   
      FROM weekending_table 
      WHERE WEEKEND_DATE BETWEEN PROJECT_START_DATE AND sysdate;     

    BEGIN

    FETCH WEEKENDING_DATE BULK COLLECT INTO acr_projected_neww;  
    ...

     END LOOP;
    END test_acr;

答案 3 :(得分:0)

我在我的程序之外使用临时表:

  CREATE GLOBAL TEMPORARY TABLE "MY_TEMP" 
   (    "WEEKEND_DATE" DATE, 
    "USERID" VARCHAR2(255 BYTE), 
    "TIMESTAMP" TIMESTAMP (6), 
    "ACR_PROJECTED" NUMBER, 
    "ARTIFICIAL_ID" NUMBER
   ) ON COMMIT PRESERVE ROWS ;

我刚刚在我的程序

中使用了上面的临时表
create or replace PROCEDURE GET_ACR_TEST(
  PROJECT_START_DATE IN DATE ,
  USER_ID IN VARCHAR2,

)  AS 
CURSOR WEEKENDING_DATE IS
  SELECT WEEKEND_DATE, DURATION 
  FROM weekending_table where WEEKEND_DATE between PROJECT_START_DATE and sysdate;

Begin

FOR WEEKEND_DATE_REC in WEEKENDING_DATE 
    LOOP
    insert into  MY_TEMP (WEEKEND_DATE,USERID,TIMESTAMP,ACR_PROJECTED,artificial_id)
      select WEEKEND_DATE_REC.WEEKEND_DATE,USER_ID,sysdate,
                       (select sum(acr_h.activity_impact) 
                       from ACR_HISTORY acr_h
                       LEFT JOIN Activity act on act.activity_id = acr_h.activity_id
                       LEFT JOIN Activity_Date_Duration act_d on act_d.activity_id = act.activity_id),1
     from dual;         
    End Loop;
END GET_ACR_TEST;

上述方法正在运作。

谢谢大家的意见!