我在上面的问题上写了一个代码,但是根据问题的要求,我没有填写标准。请给我建议,以便我可以完成

发布于 2025-02-11 16:52:06 字数 723 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2025-02-18 16:52:06

您的代码有几个弱点:

  • 为什么您使用pragma automous_transaction?我没有看到任何理由

  • 您知道表dept1有50行吗?也许或多或少。我认为该表是空的,这就是为什么什么都没有插入,循环永远不会执行的原因。

  • 您完全错过了第26-50行的第二个要求

  • 您一对一地处理行。在真实的实时应用程序中,这可能会导致性能问题。当然,不是50行,而是关于原则。

Oracle提供了一个创建函数,可以是一个简单的解决方案:

create or replace function t1 return varchar2 as
begin

   insert into dept1(columnid,balance)
   SELECT 
       LEVEL AS counter, LEVEL * 10 AS BALANCE
   FROM dual
   CONNECT BY LEVEL <= 25
   UNION ALL
   SELECT 
      25 + LEVEL AS counter, (25 + LEVEL) + LEVEL * 10 AS BALANCE
   FROM dual
   CONNECT BY LEVEL <= 25;

end;

或者,如果您愿意,请运行两个插入语句:

insert into dept1(columnid,balance)
SELECT 
    LEVEL AS counter, LEVEL * 10 AS BALANCE
FROM dual
CONNECT BY LEVEL <= 25;

insert into dept1(columnid,balance)
SELECT 
    25 + LEVEL AS counter, (25 + LEVEL) + LEVEL * 10 AS BALANCE
FROM dual
CONNECT BY LEVEL <= 25;

或使用case表达式

insert into dept1(columnid,balance)
SELECT 
    LEVEL AS counter, 
    CASE 
        WHEN LEVEL <= 25 THEN 
            LEVEL * 10 
        ELSE
            LEVEL + (LEVEL-25) * 10
    END AS BALANCE
FROM dual
CONNECT BY LEVEL <= 50;

Your code has several weak points:

  • Why do you use pragma autonomous_transaction? I don't see any reason for it

  • Do 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:

create or replace function t1 return varchar2 as
begin

   insert into dept1(columnid,balance)
   SELECT 
       LEVEL AS counter, LEVEL * 10 AS BALANCE
   FROM dual
   CONNECT BY LEVEL <= 25
   UNION ALL
   SELECT 
      25 + LEVEL AS counter, (25 + LEVEL) + LEVEL * 10 AS BALANCE
   FROM dual
   CONNECT BY LEVEL <= 25;

end;

Or, if you prefer, run two insert statements:

insert into dept1(columnid,balance)
SELECT 
    LEVEL AS counter, LEVEL * 10 AS BALANCE
FROM dual
CONNECT BY LEVEL <= 25;

insert into dept1(columnid,balance)
SELECT 
    25 + LEVEL AS counter, (25 + LEVEL) + LEVEL * 10 AS BALANCE
FROM dual
CONNECT BY LEVEL <= 25;

or use CASE expression

insert into dept1(columnid,balance)
SELECT 
    LEVEL AS counter, 
    CASE 
        WHEN LEVEL <= 25 THEN 
            LEVEL * 10 
        ELSE
            LEVEL + (LEVEL-25) * 10
    END AS BALANCE
FROM dual
CONNECT BY LEVEL <= 50;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文