PL/SQL 怎么判断一个studentID 是否在student 表中 ??

caocao123 2004-10-24 09:09:56
SQL> select studentID from student;

STUDENTID
----------
1001
1002
1003
1004
1005
1201
1202
1203
1204
1205

10 rows selected.


-- 怎么判断 1003 是否在表中??
好想不能用 in ??

-- 这个函数怎么写》
isStudent(1003) return true
isStudent(2003) return false

...全文
181 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ATGC 2004-10-25
  • 打赏
  • 举报
回复
这样好了
select count(*) from (select studentid from student where studentid=1003);
l2g32003 2004-10-25
  • 打赏
  • 举报
回复
我晕
算了 当我没说
ATGC 2004-10-25
  • 打赏
  • 举报
回复
那这样好了
select count(rowid) from student where studentid='1003';
这个是索引查找
bluelamb 2004-10-25
  • 打赏
  • 举报
回复
function isStudent(id in number)
return number
as
num number;
begin
select count(*) into num from student where studentID=id;
return num;
end isStudent;
l2g32003 2004-10-25
  • 打赏
  • 举报
回复
select count(*) from dual where exists ( select NULL from student where studentID =1003)

别人说的要看
dinya2003 2004-10-25
  • 打赏
  • 举报
回复
caocao123(大英雄曹操)兄 :
一楼的不能实现吗?

回楼主:
个人认为,如果数据比较少的话(比如说暂时处理的一些数据,或接口表等)可以考虑select count(*)或游标 ,如果数据量比较大的话,考虑用游标.
zealot_zk 2004-10-25
  • 打赏
  • 举报
回复
select * from student where exists ( select * from student where studentID =1003)
l2g32003 2004-10-25
  • 打赏
  • 举报
回复
一定要劝着大家使用
oracle 的sql写法其实很多的 大部分好的都可以在asktom上看到
在csdn上遇见的很多问题上面都有 可是大家好像不喜欢e文

例子:

10.1.0~lgone@ONE.LG.OK> create table t as select * from dba_users;

Table created.

10.1.0~lgone@ONE.LG.OK> insert into t select * from t;

23 rows created.

.......

10.1.0~lgone@ONE.LG.OK> insert into t select * from t;

23552 rows created.

10.1.0~lgone@ONE.LG.OK> alter session set sql_trace=true
2 ;

Session altered.

10.1.0~lgone@ONE.LG.OK> select count(*) from t where username='LG';

COUNT(*)
----------
2048

10.1.0~lgone@ONE.LG.OK> select count(*) from dual where exists (select null from t where username='LG');

COUNT(*)
----------
1

10.1.0~lgone@ONE.LG.OK> alter session set sql_trace=false;

Session altered.

10.1.0~lgone@ONE.LG.OK>


下面的是结果

********************************************************************************

select count(*)
from
t where username='LG'


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.08 70 724 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.08 70 724 0 1

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 81

Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=724 pr=70 pw=0 time=84079 us)
2048 TABLE ACCESS FULL T (cr=724 pr=70 pw=0 time=14519 us)

********************************************************************************

select count(*)
from
dual where exists (select null from t where username='LG')


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 4 0 0
Fetch 2 0.00 0.00 0 0 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 0 4 0 1

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 81

Rows Row Source Operation
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=4 pr=0 pw=0 time=367 us)
1 FILTER (cr=4 pr=0 pw=0 time=258 us)
1 FAST DUAL (cr=0 pr=0 pw=0 time=10 us)
1 TABLE ACCESS FULL T (cr=4 pr=0 pw=0 time=214 us)

********************************************************************************

dinya2003 2004-10-25
  • 打赏
  • 举报
回复
牛角
liuyi8903 2004-10-25
  • 打赏
  • 举报
回复
来晚了,凑个热闹吧!
jack_4826 2004-10-25
  • 打赏
  • 举报
回复
在函数中使用游标
显式游标使用sql%notfound来判断
隐式游标使用异常no_data_found来判断
caocao123 2004-10-24
  • 打赏
  • 举报
回复
count 要遍历整个表吧?
caocao123 2004-10-24
  • 打赏
  • 举报
回复
ATGC(这一生受了很多委屈吃了很多苦。。)

真不想骂你啊~~~~~~~~


用count 来判断 ,肯定很SB的啦
count 要遍历怎么表吧?
如果第一个几是, 那下面的遍历就多余了吧
ssDOn 2004-10-24
  • 打赏
  • 举报
回复
建议用这个 select nvl(sum(1),-1)) from .... where ....
即使没有ID值时,在Pl/sql中也不会出现exception.
ATGC 2004-10-24
  • 打赏
  • 举报
回复
返回是0,就是没有啦,也可以select studentid from student where studentid=1003;若返回空值就是没有啦
ATGC 2004-10-24
  • 打赏
  • 举报
回复
这个方法很多咯~比如
SQL> select * from student;

STUDENTID
----------
1002

SQL> select count(*) from student where studentId=1003;

COUNT(*)
----------
0

17,380

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧