如何在PL / SQL

时间:2017-05-31 08:08:33

标签: oracle plsql

我有以下代码:

CREATE OR REPLACE VIEW pohled ("r_minimalni_doba_vysetreni", "r_id_pacienta", "r_id_doktora", "r_minimalni_doba_doktora") AS 
    SELECT MIN(v.DOBA_VYSETRENI), v.ID_PACIENTA, v.ID_DOKTORA, d.MIN_DOBA FROM vysetreni v, doktor d WHERE v.ID_DOKTORA = d.ID_DOKTORA GROUP BY v.ID_DOKTORA, v.ID_PACIENTA, d.MIN_DOBA;

DECLARE
     CURSOR  kurzor(vstup_id_pacienta IN NUMBER, vstup_id_doktora IN NUMBER) IS
        SELECT * FROM pohled WHERE "r_id_pacienta" = vstup_id_pacienta AND "r_id_doktora" = vstup_id_doktora;
     tmp pohled%ROWTYPE;
BEGIN
    OPEN kurzor(1, 1);
    LOOP
        FETCH kurzor INTO tmp;
        EXIT WHEN kurzor%NOTFOUND;
        dbms_output.put_line('How to print some data from tmp variable???');
    END LOOP;
    CLOSE kurzor;
END;

1)创建视图 2)创建游标 3)通过游标在循环中获取视图..

所以,所有步骤都没问题,但它有效,但我不知道如何从变量tmp - tmp pohled%ROWTYPE;打印一些数据。

当我运行代码时:dbms_output.put_line(tmp.r_minimalni_doba_vysetreni);我收到错误oracle - PLS-00302: component must be declared.

我不知道为什么?

感谢您的帮助。

编辑: 代码存在同样的问题:

DECLARE
     CURSOR  kurzor(vstup_id_pacienta IN NUMBER, vstup_id_doktora IN NUMBER) IS
        SELECT * FROM pohled WHERE "r_id_pacienta" = vstup_id_pacienta AND "r_id_doktora" = vstup_id_doktora; 
BEGIN 
    for tmp in kurzor(1, 1) 
    LOOP
        dbms_output.put_line(tmp.r_minimalni_doba_vysetreni);
    END LOOP; 
END;

1 个答案:

答案 0 :(得分:0)

默认情况下,Oracle数据库区分大小写;但是,默认情况下,它们还会将所有内容转换为大写字母,以便从用户那里抽象出区分大小写。

除非用双引号括起标识符,否则数据库将使用您使用过的任何案例。

最好的事情是从不(除非你有一个压倒一切的商业原因)在标识符周围使用双引号并让Oracle管理案例:

CREATE OR REPLACE VIEW pohled (
  r_minimalni_doba_vysetreni,    -- Remove the double quotes here
  r_id_pacienta, 
  r_id_doktora,
  r_minimalni_doba_doktora
) AS 
  SELECT MIN(v.DOBA_VYSETRENI),
         v.ID_PACIENTA,
         v.ID_DOKTORA,
         d.MIN_DOBA
  FROM   vysetreni v
         INNER JOIN doktor d     -- Use an ANSI join rather than legacy Oracle join
         ON ( v.ID_DOKTORA = d.ID_DOKTORA )
  GROUP BY v.ID_DOKTORA, v.ID_PACIENTA, d.MIN_DOBA;

DECLARE
   CURSOR  kurzor(vstup_id_pacienta IN NUMBER, vstup_id_doktora IN NUMBER) IS
      SELECT *
      FROM   pohled
      WHERE  r_id_pacienta = vstup_id_pacienta -- Remove the double quotes here
      AND    r_id_doktora  = vstup_id_doktora;
   tmp pohled%ROWTYPE;
BEGIN
  OPEN kurzor(1, 1);
  LOOP
    FETCH kurzor INTO tmp;
    EXIT WHEN kurzor%NOTFOUND;
    dbms_output.put_line( tmp.r_minimalni_doba_vysetreni );
  END LOOP;
  CLOSE kurzor;
END;

但是,您可以使用原始代码并用双引号括起标识符:

dbms_output.put_line(tmp."r_minimalni_doba_vysetreni");