我在上面的问题上写了一个代码,但是根据问题的要求,我没有填写标准。请给我建议,以便我可以完成
Q- /*编写一个函数以创建表,该表包含列ID并在1至25之间的列ID中增加10列的列和插入记录 (ID余额
1 10
)
20。50
220。。25250
如果
列ID 26至50之间的余额将为
(ID余额
26 26+10
27 27+
)
。
*/
/我编写该代码,并且该函数也编译了,但数据没有插入表中。 请在下面找到我的代码。/
create or replace function t1
return varchar2 as
pragma autonomous_transaction;
cursor c1 is
select columnid,balance from dept1;
emp_rec c1%rowtype;
counter number(2):=1;
bal number:=10;
begin
open c1;
loop
fetch c1 into emp_rec;
exit when c1%notfound;
insert into dept1(columnid,balance) values(counter,bal);
counter:= counter+1;
bal:=counter*10;
end loop;
close c1;
return 'true';
end;
/
Q- /*Write a function to create table which contain columns ID and BALANCE and insert record in the way if Column ID between 1 to 25 BALANCE will increase by 10
(ID BALANCE
1 10
2 20
.
.
25 250)
If column ID between 26 to 50 the BALANCE will be
( ID BALANCE
26 26+10
27 27+20
.
.
50 ).*/
/I write that code and the function also compiled but data didn't insert into the table.
Please find my code below here./
create or replace function t1
return varchar2 as
pragma autonomous_transaction;
cursor c1 is
select columnid,balance from dept1;
emp_rec c1%rowtype;
counter number(2):=1;
bal number:=10;
begin
open c1;
loop
fetch c1 into emp_rec;
exit when c1%notfound;
insert into dept1(columnid,balance) values(counter,bal);
counter:= counter+1;
bal:=counter*10;
end loop;
close c1;
return 'true';
end;
/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有几个弱点:
为什么您使用
pragma automous_transaction
?我没有看到任何理由您知道表
dept1
有50行吗?也许或多或少。我认为该表是空的,这就是为什么什么都没有插入,循环永远不会执行的原因。您完全错过了第26-50行的第二个要求
Oracle提供了一个创建函数,可以是一个简单的解决方案:
或者,如果您愿意,请运行两个插入语句:
或使用
case
表达式Your code has several weak points:
Why do you use
pragma autonomous_transaction
? I don't see any reason for itDo you know table
dept1
has exactly 50 rows? Maybe there are more or less. I assume this table is empty, that's the reason why nothing is inserted, the loop is never executed.You entirely missed the second requirement for rows 26-50
You process the rows one-by-one. In a real live application, this may cause performance issues. Of course, not for 50 rows but it's about principles.
Oracle provides a row-generator function, a simple solution could be this one:
Or, if you prefer, run two insert statements:
or use
CASE
expression