有一个表TB1,如下:
clm1 clm2
A 90
A 80
B 80
B 80
A 70
C 60
A 55
B 90
B 80
C 80
C 90
create table tb1(clm1 varchar2(5),clm2 number(5))insert into tb1 values('A',90);
insert into tb1 values('A',80);
insert into tb1 values('B',80);
insert into tb1 values('B',80);
insert into tb1 values('A',70);
insert into tb1 values('C',60);
insert into tb1 values('A',55);
insert into tb1 values('B',90);
insert into tb1 values('B',80);
insert into tb1 values('C',80);
insert into tb1 values('C',90);希望根据输入的clm1的值查询记录
declare
v_clm1 tb1.clm1%type;
v_clm2 tb1.clm2%type;
cursor tb1_cur
is
select clm1,clm2
from tb1
where clm1=v_clm1;
begin
v_CLM1:='&clm1';
dbms_output.put_line('clm1 clm2');
for tb1_record in tb1_cur loop
dbms_output.put_line(tb1_record.clm1||' '||tb1_record.clm2);
end loop;
end;
现在的问题是如希望根据多个(数量不定)的CLM1的值进行查询,该如何处理?
如输入A,可以查询符合A的记录,输入A B,则可以查询相应的记录。
多谢!!给各位添麻烦了!
clm1 clm2
A 90
A 80
B 80
B 80
A 70
C 60
A 55
B 90
B 80
C 80
C 90
create table tb1(clm1 varchar2(5),clm2 number(5))insert into tb1 values('A',90);
insert into tb1 values('A',80);
insert into tb1 values('B',80);
insert into tb1 values('B',80);
insert into tb1 values('A',70);
insert into tb1 values('C',60);
insert into tb1 values('A',55);
insert into tb1 values('B',90);
insert into tb1 values('B',80);
insert into tb1 values('C',80);
insert into tb1 values('C',90);希望根据输入的clm1的值查询记录
declare
v_clm1 tb1.clm1%type;
v_clm2 tb1.clm2%type;
cursor tb1_cur
is
select clm1,clm2
from tb1
where clm1=v_clm1;
begin
v_CLM1:='&clm1';
dbms_output.put_line('clm1 clm2');
for tb1_record in tb1_cur loop
dbms_output.put_line(tb1_record.clm1||' '||tb1_record.clm2);
end loop;
end;
现在的问题是如希望根据多个(数量不定)的CLM1的值进行查询,该如何处理?
如输入A,可以查询符合A的记录,输入A B,则可以查询相应的记录。
多谢!!给各位添麻烦了!
---------- ----------
A 90
A 80
B 80
B 80
A 70
C 60
A 55
B 90
B 80
C 80
C 90已选择11行。OPER@tl> create or replace procedure xx1_p(in_var varchar2)
2 as
3 v_sql varchar2(1000);
4 n number:=0;
5 i number:=1;
6 j number:=0;
7 v_sign varchar2(1000);
8 v_a tb1.clm1%type;
9 v_b tb1.clm2%type;
10 cur sys_refcursor;
11 begin
12 v_sql:='select * from tb1 where clm1 in(';
13 loop
14 n:=instr(in_var,',',1,i);
15 v_sign:=substr(in_var,j+1,n-j-1);
16 exit when n=0;
17 i:=i+1;
18 j:=n;
19 v_sql:=v_sql||''''||v_sign||''',';
20 end loop;
21 v_sql:=substr(v_sql,1,instr(v_sql,',',-1)-1)||')';
22 dbms_output.put_line(v_sql);
23 dbms_output.put_line('clm1,clm2');
24 open cur for v_sql;
25 loop
26 fetch cur into v_a,v_b;
27 exit when cur%notfound;
28 dbms_output.put_line(v_a||','||v_b);
29 end loop;
30 close cur;
31 end;
32 /过程已创建。OPER@tl> exec xx1_p('A,C,')
select * from tb1 where clm1 in('A','C')
clm1,clm2
A,90
A,80
A,70
C,60
A,55
C,80
C,90PL/SQL 过程已成功完成。OPER@tl>