0

我怎么能这样做:

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

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 来完成。

4

1 回答 1

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 不是blockers 的成员,其中blocking_group = 1 和blocking_type = 2

这是不可能的Member Funtion。您需要声明一个MAP方法,如果对象具有很多属性(就像您的情况一样),该方法相当笨重并且会变得相当烦人。

于 2017-02-18T10:48:06.630 回答