-1

我有一个表 EMPLOYEES,我需要做一个循环,在循环的每一步我使用 dbms_output.put_line(column_name)

我怎样才能做到这一点?

4

3 回答 3

3

刚刚找到这个解决方案

begin
   for rec in (SELECT column_name 
               FROM all_tab_columns 
               WHERE owner = 'HR'
               AND table_name = 'EMPLOYEES')
   loop
       dbms_output.put_line(rec.column_name);
   end loop;
end;
于 2012-11-23T07:12:45.750 回答
0

尝试使用光标如下:

set serveroutput on;
declare
 var_col1 varchar2(30) ;
 num_col2 number ;  

 CURSOR cur
 is       
  select var_col ,num_col
  from table
  where <conditions>;

begin
   open cur;
   fetch cur into var_col1,num_col2;

   LOOP
    fetch cur into var_col1,num_col2;
       EXIT WHEN cur%NOTFOUND;
       dbms_output.put_line( var_col1||'         '||num_col2);
   END LOOP;

   close cur;  
end;

这是另一个解决方案:

set serveroutput on;
begin
  FOR  cur in ( select var_col ,num_col from table where <conditions>)
  LOOP
    dbms_output.put_line(cur.var_col||' = '||cur.num_col);
  END LOOP;
end;
于 2012-11-23T07:32:06.107 回答
0

当然,您可以在 table_name 之后使用 WHERE 重写此 SELECT 示例。我没有编译这段代码,但我认为它可以。更改您的属性的 ID_Employee 和名称

CREATE OR REPLACE PROCEDURE AAA_TEST
    IS
    BEGIN
      DECLARE 
            v_id NUMBER :=0;
            v_name VARCHAR2(25);

            CURSOR cursorMails IS
                 SELECT Id_Employee,Name FROM EMPLOYEES;  

            BEGIN
                           OPEN cursorMails;

                               LOOP

                                    FETCH cursorMails INTO v_id, v_name;
                                         DBMS_OUTPUT.PUT_LINE('Number of Employee: ' || v_id || ' Name Employee: ' || v_name);
                               EXIT WHEN cursorMails%NOTFOUND;
                               END LOOP;
                           CLOSE cursorMails;
            END;
    END;
    /
于 2012-11-23T12:09:12.760 回答