select S_10994_1_SYS_MODELTYPE.nextval,a.typeid,'SYS','Mail_Forward','郵件轉發模板','Mail Forward Model' from
bse_organization a where not exists (select orgtypeid from SYS_MODELTYPE b where b.orgtypeid=a.typeid and modelcode ='Mail_Forward')
--
select S_10994_1_SYS_MODELTYPE.nextval,typeid,'SYS','Mail_Forward','郵件轉發模板','Mail Forward Model' from
bse_organization where typeid not in (
select orgtypeid from SYS_MODELTYPE where modelcode='Mail_Forward'
)
請注意not in 邏輯上不完全等同于not exists,如果你誤用了not in,小心你的程序存在致命的BUG:
請看下面的例子:
create table t1 (c1 number,c2 number);
create table t2 (c1 number,c2 number);
insert into t1 values (1,2);
insert into t1 values (1,3);
insert into t2 values (1,2);
insert into t2 values (1,null);
select * from t1 where c2 not in (select c2 from t2);
no rows found
select * from t1 where not exists (select 1 from t2 where t1.c2=t2.c2);
c1 c2
1 3
正如所看到的,not in 出現了不期望的結果集,存在邏輯錯誤。如果看一下上述兩個select語句的執行計劃,也會不同。后者使用了hash_aj。
因此,請盡量不要使用not in(它會調用子查詢),而盡量使用not exists(它會調用關聯子查詢)。如果子查詢中返回的任意一條記錄含有空值,則查詢將不返回任何記錄,正如上面例子所示。
除非子查詢字段有非空限制,這時可以使用not in ,并且也可以通過提示讓它使用hasg_aj或merge_aj連接。
大盤預測
國富論
posted on 2007-10-11 16:51
華夢行 閱讀(183)
評論(0) 編輯 收藏 所屬分類:
Oracle