使用NOT EXISTS 对LOV进行控制

需求:当选择一个任务的成员或者客户时候,要使LOV只显示在数据库表中未插入的数据。

        

解决办法: 应用对LOV的数据源进行控制。采用NOT EXISTS

 

代码如下:

SELECT all_data.project_id project_id, all_data.task_id task_id, all_data.customer_id customer_id, all_data.task_customer_name task_customer_name, all_data.task_customer_number task_customer_number, all_data.project_relationship_code project_relationship_code, all_data.record_version_number record_version_number, all_data.bill_to_customer_id bill_to_customer_id, all_data.bill_customer_name bill_customer_name, all_data.bill_customer_number bill_customer_number, all_data.bill_to_address_id bill_to_address_id, all_data.bill_address1 bill_address1, all_data.bill_address2 bill_address2, all_data.bill_address3 bill_address3, all_data.bill_address4 bill_address4, all_data.ship_to_customer_id ship_to_customer_id, all_data.ship_customer_name ship_customer_name, all_data.ship_customer_number ship_customer_number, all_data.ship_to_address_id ship_to_address_id, all_data.ship_address1 ship_address1, all_data.ship_address2 ship_address2, all_data.ship_address3 ship_address3, all_data.ship_address4 ship_address4 FROM (SELECT ppc.project_id project_id, pt.task_id task_id, ppc.customer_id customer_id, hp.party_name task_customer_name , hp.party_number task_customer_number, ppc.project_relationship_code project_relationship_code, ppc.record_version_number record_version_number, ppc.bill_to_customer_id bill_to_customer_id, hp.party_name bill_customer_name, hp.party_number bill_customer_number, ppc.bill_to_address_id bill_to_address_id, bill_loc.address1 bill_address1, bill_loc.address2 bill_address2, bill_loc.address3 bill_address3, bill_loc.address4 || DECODE(bill_loc.address4, NULL, NULL, ', ') || bill_loc.city || ' , ' || NVL(bill_loc.state, bill_loc.province) || ' ' || bill_loc.postal_code || ' , ' || bill_loc.county bill_address4, ppc.ship_to_customer_id ship_to_customer_id, hp.party_name ship_customer_name, hp.party_number ship_customer_number, ppc.ship_to_address_id ship_to_address_id, ship_loc.address1 ship_address1, ship_loc.address2 ship_address2, ship_loc.address3 ship_address3, ship_loc.address4 || DECODE(ship_loc.address4, NULL, NULL, ', ') || ship_loc.city || ' , ' || NVL(ship_loc.state, ship_loc.province) || ' ' || ship_loc.postal_code || ' , ' || ship_loc.county ship_address4 FROM pa_project_customers ppc, hz_parties hp, hz_cust_accounts hca, hz_cust_acct_sites_all bill_site, hz_cust_acct_sites_all ship_site, hz_party_sites bill_party_site, hz_party_sites ship_party_site, hz_locations bill_loc, hz_locations ship_loc, pa_tasks pt WHERE hp.party_id = hca.party_id AND hca.cust_account_id = ppc.customer_id AND ppc.bill_to_customer_id = bill_site.cust_account_id AND ppc.bill_to_address_id = bill_site.cust_acct_site_id(+) AND ppc.ship_to_customer_id = ship_site.cust_account_id AND ppc.ship_to_address_id = ship_site.cust_acct_site_id(+) AND hp.party_id = bill_party_site.party_id AND hp.party_id = ship_party_site.party_id AND bill_site.party_site_id = bill_party_site.party_site_id(+) AND ship_site.party_site_id = ship_party_site.party_site_id(+) AND bill_party_site.location_id = bill_loc.location_id(+) AND ship_party_site.location_id = ship_loc.location_id(+) AND pt.project_id = ppc.project_id) all_data WHERE NOT EXISTS (SELECT tc.project_id, tc.task_id, tc.customer_id FROM cux_pa_task_customers tc WHERE all_data.project_id = tc.project_id AND all_data.task_id = tc.task_id AND all_data.customer_id = tc.customer_id) AND all_data.project_id=:PARAMETER.G_PROJECT_ID AND all_data.task_id =:PARAMETER.G_TASK_ID

 

 oracle中的exists和not exists和in用法:
有两个简单例子,以说明 “exists”和“in”的效率问题

  1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;

  T1数据量小而T2数据量非常大时,T1<<T2 时,1) 的查询效率高。

  2) select * from T1 where T1.a in (select T2.a from T2) ;

  T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。

  exists 用法:

  请注意 1)句中的有颜色字体的部分 ,理解其含义;

  其中 “select 1 from T2 where T1.a=T2.a” 相当于一个关联表查询,相当于

  “select 1 from T1,T2 where T1.a=T2.a”

  但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。

  “exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。

  因此“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where 条件成立。

  in 的用法:

  继续引用上面的例子

  “2) select * from T1 where T1.a in (select T2.a from T2) ”

  这里的“in”后面括号里的语句搜索出来的字段的内容一定要相对应,一般来说,T1和T2这两个表的a字段表达的意义应该是一样的,否则这样查没什么意义。

  打个比方:T1,T2表都有一个字段,表示工单号,但是T1表示工单号的字段名叫“ticketid”,T2则为“id”,但是其表达的意义是一样的,而且数据格式也是一样的。这时,用 2)的写法就可以这样:

  “select * from T1 where T1.ticketid in (select T2.id from T2) ”

  Select name from employee where name not in (select name from student);

  Select name from employee where not exists (select name from student);

  第一句SQL语句的执行效率不如第二句。

  通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因

你可能感兴趣的:(oracle,sql,数据库,null,HP,任务)