oracle plsql游标错误

时间:2016-09-26 18:31:05

标签: plsql cursor

我有一张叫做req_tb的桌子

CREATE TABLE req_tb(TableName   VARCHAR2(43),
                    ColumnName  VARCHAR2(98),
                    Edit_ind    CHAR)
/

我正在插入值

insert into req_tb VALUES('Employees','employee_id      ','Y');
insert into req_tb VALUES('Employees','first_name       ','Y');
insert into req_tb VALUES('Employees','last_name        ','N');
insert into req_tb VALUES('Employees','email            ','N');
insert into req_tb VALUES('Employees','job_id           ','N');
insert into req_tb VALUES('Employees','salary           ','Y');
insert into req_tb VALUES('Employees','commission_pct   ','Y');
insert into req_tb VALUES('Employees','hire_date        ','N');
insert into req_tb VALUES('Employees','department_id    ','Y');

现在我的表格如下

SQL> SELECT * FROM REQ_TB;

TABLENAME                                   COLUMNNAME      EDIT_IND
------------------------------------------- --------------- ----------
Employees                                   employee_id     Y
Employees                                   first_name      Y
Employees                                   last_name       N
Employees                                   email           N
Employees                                   job_id          N
Employees                                   salary          Y
Employees                                   commission_pct  Y
Employees                                   hire_date       N
Employees                                   department_id   Y

考虑列edit_ind在表req_tb中动态变化,我想只打印作为'Y'的列。请检查我的程序

CREATE OR REPLACE PROCEDURE dyn_sql_sp
AS
 cols     VARCHAR2(2000);
 v_cols   VARCHAR2(2000);
 cls      VARCHAR2(2000);
 v_employee_id number;
 emp       employees%rowtype;
 cnt       number;
cursor tab_c
is
 select columnname from req_tb
 where EDIT_IND='Y';
cursor col_c
is
 select employee_id from employees;
BEGIN
for i in tab_C
loop
cols:=cols||'emp.'||i.columnname||'||';
end loop;
cols:=rtrim(cols,'||');
for i in col_c
loop
SELECT * into emp from employees
where employee_id=i.employee_id;
dbms_output.put_line(cols);
end loop;
 Exception
 When Others Then
 dbms_output.put_line(sqlerrm||sqlcode);
 end;
/

我希望输出为

100 Steven                    24000                           90
101 Neena                     17000                           90
102 Lex                        17000                           90
103 Alexander                  9000                           60
104 Bruce                      6000                           60
105 David                      4800                           60
106 Valli                      4800                         60
107 Diana                      4200                           60

输出我是

SQL> exec dyn_sql_sp;
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id
emp.employee_id         ||emp.first_name                ||emp.salary                    ||emp.commission_pct   ||emp.department_id

错误是什么

1 个答案:

答案 0 :(得分:0)

问题在于:

for i in col_c
loop
SELECT * into emp from employees
where employee_id=i.employee_id;
dbms_output.put_line(cols);
end loop;

您正在从*表中选择所有employes(所以每列)并且您永远不会使用它。您想要的是仅选择cols变量中的列。要做到这一点,你应该使用动态语句,如下所示:

EXECUTE IMMEDIATE 'SELECT ' || cols || ' FROM employees WHERE emplyee_id = :1'
INTO emp
USING i.employee_id

但是还有另外一个问题。动态语句中的游标emp不是employees%rowtype类型(列数不同),因此动态语句将失败。

幸运的是,似乎DBMS_SQL包可以帮到你。请看这篇文章:https://community.oracle.com/thread/702648,你应该找到解决这个问题的方法。