PL / SQL:"方法的成员,其中"

时间:2017-02-17 20:39:17

标签: plsql memberof

我怎么能这样做:

从三个不同的列中选择值到嵌套表或其他类型的集合......

DECLARE
TYPE blockers_set IS TABLE OF (
employee_ID NUMBER(8), 
BLOCKING_GROUP NUMBER(4),
BLOCKING_TYPE NUMBER2(2));

select employee_ID, BLOCKING_GROUP, BLOCKING_TYPE 
bulk collect into blockers
from blockers;

...并参考专栏:

if employee_ID not member of blockers where blocking_group = 1
and blocking_type = 2
then <<business logic>>

blockers-collection中的一个employee_ID可以有多个行。

必须使用pl / sql完成。

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式执行此操作:

CREATE TABLE blockers(
                        employee_id NUMBER(8),
                        blocking_group NUMBER(4),
                        blocking_type number(2));


===============================

select * from blockers;

===============================

Create or replace type OBJ_BLOCKERS as OBJECT(
                                                employee_id NUMBER(8),
                                                blocking_group NUMBER(4),
                                                blocking_type number(2)
                                            );

=================================================

Create or replace type varble is table of OBJ_BLOCKERS;

===================================================

DECLARE
  --Creating one element for comparision. In your case you need to populate your employee_id to be compared here.    
  tmp OBJ_BLOCKERS:=OBJ_BLOCKERS(263427,1,2);
  var  varble;
  myid INT;

begin

  --This how you Select values from three different columns into nested table(Object)
  Select OBJ_BLOCKERS(employee_id,
                      blocking_group,
                      blocking_type) 
  Bulk Collect into var
  from blockers;

  -- Displaying Employee_Id from the collection
  For i in 1..var.count
  loop
    dbms_output.put_line(var(i).employee_id);
  end loop;

  --

  SELECT  1
        INTO    myid
        FROM    TABLE(var) q
        where  OBJ_BLOCKERS(q.employee_id,q.blocking_group,q.blocking_type) = tmp --Here you are comparing the employee_id which should be a member of collection
        AND q.blocking_group = 1;

  IF (myid = 1) THEN
       dbms_output.put_line('OK, exists.');
  END IF;       

 Exception
 When No_DATa_found then
        dbms_output.put_line('No Data Found');  

End;                                                    

执行:

SQL> /
263427
534366
454562
OK, exists.

PL/SQL procedure successfully completed.

请注意:

  

如果employee_ID不是阻止者的成员,其中blocking_group = 1和   blocking_type = 2

使用Member Funtion无法做到这一点。你需要声明一个MAP方法,这个方法相当笨拙,如果对象有很多属性(就像你的情况一样)会很烦人。