1
create table cdi(comp_id varchar2(3),pk_key varchar2(2000));
insert into cdi values('abc','empno,ename,job');
insert into cdi values('pqr','empno,job,mgr');
insert into cdi values('cde','empno,mgr,sal');
commit;

create table emp_test(empno integer,ename varchar2(200),job varchar2(200),mrg  integer,sal integer);
insert into emp_test values(1,'Gaurav Soni','DB',12,12000);
insert into emp_test values(2,'Niharika Saraf','Law',13,12000);
insert into emp_test values(2,'Niharika Saraf','Law',13,12000);
insert into emp_test values(3,'Saurabh Soni',null,12,12000);
commit;

cdi表中comp_id是主键

create or replace procedure test(p_comp_id IN cdi.comp_id%TYPE
                            ,p_empno   IN emp_test.empno%TYPE
                            )
IS
TYPE ref_cur is ref cursor;
v_cur ref_cur;
v_pk_key cdi.pk_key%TYPE;


BEGIN
  OPEN v_cur is select pk_key from cdi where comp_id =p_comp_id;
  fetch v_cur into v_pk_key;

 --now this list v_pk_key is primary key for that comp_id
 --so following things need to be done 
 --1.check the emp_test table with this column list (v_pk_key )
 --2. whether for that emp_no the primary key is null eg.
   -- incase of comp_id cde ...empno,mgr,sal  value should be not null
   --if any of the value is null raise an error 
 --3.If there are two rows for that primary also raise an error.
   -- for eg comp_id=abc  two rows are fetched from emp_test 
 close v_cur;
END;

我不确定我应该做什么,首先我想到连接v_pk_key类似empno||ename||job 然后在选择查询中使用它,但无法检查空值,我很困惑该怎么做。

编辑

我尝试过的是将列表 v_pk_key 转换为

NVL(empno,'$')||NVL(ename,'$')||NVL(job,'$') and then

select v_pk_list from emp_test where empno=p_empno; 

然后检查结果中的 $ 如果结果中没有 $ 我会检查不止一行,但我发现这不是一个有效的解决方案

如果有人给我一个jist,我会解决这个问题。

4

1 回答 1

1

I would split out that list of values, which really represents 3 columns ('empno, ename, job'). Use instr function, or create a separate function to split and return a pl/sql table, but either way it would be much more clear what is intended in the code.

See here for a SO link to some examples using instr to split csv fields.

Once you have 3 separate local variables with these values (l_empno, l_ename, l_job), then you can use much easier in your various SQL statements (where l_empno = blah and l_ename not in (blahblah)), etc...

于 2012-03-08T17:22:17.790 回答