第一次写oracle存储过程,大家帮忙看看语法有什么问题。
表a:字段a_id 、a_company
表b:字段b_id 、b_values
字段都是varchar2类型,oracle 9i
目的:从表a中取出a_company的值,做一个判断,然后根据判断结果在表b中插入相应的数据。
CREATE OR REPLACE Procedure aaa as
x_company varchar2(12);
x_id varchar2(6);
cursor csor_aaa is select t.a_id,t.a_company from 表a t where t.a_company like '001%';
begin
open csor_aaa;
fetch csor_aaa into x_id,x_company;
while csor_company%found loop
if substr(x_company,7,2)=10 then
insert into 表b y (b_id,b_values) values ('&x_id','000001');
elseif substr(x_company,7,2)>10 and substr(x_company,7,2)<30 then
insert into 表b y (b_id,b_values) values ('&x_id','000002');
elseif substr(x_company,7,2)=30 then
insert into 表b (b_id,b_values) values ('&x_id','000003');
end if
fetch csor_aaa into x_id,x_company;
end loop;
close csor_aaa;
commit;
end aaa;
问题点数:50、回复次数:17Top
1 楼baidurenjwt(浮躁)回复于 2006-12-01 01:25:27 得分 0
substr(x_company,7,2)='30' or substr(x_company,7,2)='40'
可以不可以写成 substr(x_company,7,2) in ('30','40')Top
2 楼jdsnhan(柳荫凉)回复于 2006-12-01 08:29:39 得分 10
可以那么写。楼主用的oracle哪个版本。如果9以上,可以直接用case来实现。Top
3 楼baidurenjwt(浮躁)回复于 2006-12-01 09:08:34 得分 0
是9i case可以提高性能吗?Top
4 楼icedut(冰-装修准备中)回复于 2006-12-01 09:22:55 得分 40
用case一个语句就可以写好了,也不用游标了Top
5 楼baidurenjwt(浮躁)回复于 2006-12-01 09:25:15 得分 0
用case 怎么一个语句就好啊?帮忙提示一下啊?Top
6 楼icedut(冰-装修准备中)回复于 2006-12-01 09:34:10 得分 0
insert into 表b y (b_id,b_values)
select t.a_id,
case when substr(t.a_company,7,2) =10 then '000001'
when substr(t.a_company,7,2)>10 and substr(t.a_company,7,2)<30 then '000002'
when substr(t.a_company,7,2)=30 then '000003' end
from 表a t where t.a_company like '001%';
Top
7 楼icedut(冰-装修准备中)回复于 2006-12-01 09:38:35 得分 0
like '001%';
也可以用变量的
我看到你前面的帖子好像是想用变量的Top
8 楼baidurenjwt(浮躁)回复于 2006-12-01 09:47:33 得分 0
回复 :冰
可能是我没说明白,上边的那个是简单写的,实际上在每个 elseif 中有好多判断(and 和 or 形式的),根据判断然后不一定要在 表b中插入几条记录。
上边的那个case不知道可以满足吗?
谢谢Top
9 楼icedut(冰-装修准备中)回复于 2006-12-01 09:51:07 得分 0
case when then
when then
...
end
也可以判断很多阿
我不太明白你的意思
Top
10 楼icedut(冰-装修准备中)回复于 2006-12-01 09:52:16 得分 0
如果你觉得case复杂
就用游标好了
你也可以两种方法都用一下
正好可以比较一下
那个效率更高就用哪个了
Top
11 楼baidurenjwt(浮躁)回复于 2006-12-01 09:56:42 得分 0
能简单说下,不用case用游标的方法吗?谢谢Top
12 楼icedut(冰-装修准备中)回复于 2006-12-01 10:16:11 得分 0
你现在用得不就是游标么
你说得我都糊涂了
Top
13 楼baidurenjwt(浮躁)回复于 2006-12-01 10:21:22 得分 0
不好意思,我第一次用这个东西。
这是其中一小部分 if 。
CREATE OR REPLACE Procedure User_JueSe as
x_company varchar2(12);
x_user_id char(6);
x_user_zwjb char(2);
x_1 char(2);
x_2 char(2);
x_description varchar2(80);
cursor csor_company is select t.user_id,t.company,t.zwjb,t.description from ry_jbxx t where t.company like '3601%';
begin
open csor_company;
fetch csor_company into x_user_id,x_company,x_user_zwjb,x_description;
while csor_company%found loop
x_1:=substr(x_company,7,2);
x_2:=substr(x_company,9,2);
x_description:=trim(description);
if x_1='00' then
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000008');
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000405');
----------------------------------------------------------------------------
elseif to_number(x_1)>'50' and zwjb='40' then
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000004');
elseif to_number(x_1)>'50' and x_2='00' and zwjb='50' then
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000003');
elseif to_number(x_1)>'50' and x_2='01' and zwjb='50' then
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000050');
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000002');
elseif to_number(x_1)>'50' and x_2='02' and zwjb='50' then
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000050');
insert into yhglb y (yhbid,yhgljsid) values (x_user_id,'000001');
end if;
fetch csor_rybh_hh into x_user_id,x_company,x_user_zwjb,x_description;
end loop;
close csor_company;
commit;
end User_JueSe;Top
14 楼baidurenjwt(浮躁)回复于 2006-12-01 10:22:32 得分 0
有什么好的方式优化一下吗?用上边说的case可以实现吗?Top
15 楼icedut(冰-装修准备中)回复于 2006-12-01 10:41:41 得分 0
插入地记录数都不一样
那你还是用现在的做法阿Top
16 楼baidurenjwt(浮躁)回复于 2006-12-01 15:59:25 得分 0
9i 的存促过程 里面用汉字判断可以吗?
比如:
if x_description='电脑' thenTop
17 楼icedut(冰-装修准备中)回复于 2006-12-01 17:57:29 得分 0
可以
declare aa varchar2(200);
begin
aa:='电脑';
if aa='电脑' then
dbms_output.put_line('电脑' );
end if;
end;Top





