Home > database >  Oracle. Write a program block
Oracle. Write a program block

Time:09-16

Find the finish all within 1000 number, namely a number is equal to, it doesn't contain itself the sum of the factor of
Declare
I number;
J number;
The sum number;
The BEGIN
For I IN 1.. 1000
Loop
Sum:=0;
For j IN 1. I - 1
Loop
If the mod (I, j)=0 then sum:=sum + j;
end if;
end loop;
If (I)=sum then dbms_output. Putline (To_char (I));
end if;
end loop;
end;
Just began to learn the hope can help you

CodePudding user response:

Thanks for sharing, the best record in the blog, too,

CodePudding user response:

First of all, thank you for your sharing, but in line with a common learning, the principle of common progress, there are two problems, need to share with you,
First, your code in my environment, execution, there are some content that needs to be modified, below is the content of the modified,
DECLARE
I NUMBER;
J NUMBER;
N_sum NUMBER;
The BEGIN
I:=0;
N_sum:=0;
FOR I IN 1.. 1000 LOOP
N_sum:=0;
FOR j IN 1. I - 1 LOOP
IF
The MOD (I, j)=0
THEN
SELECT
N_sum + j
INTO
N_sum
The FROM
Dual;

END IF;
END LOOP;

IF
I=n_sum
THEN
Dbms_output. Put_line (I);
END IF;
END LOOP;

END;

The second block of code, only in the amount of data is large, or under the condition of the business logic is very complex, just can use, programmers spell is logic, rather than physical strength, the problem, can be obtained by direct query, don't need some code block loop,
Time is a bit hasty, didn't watch carefully, still should have room for optimization, for all to share,
Within the following statement, can query to 10000 the number of
WITH
TAB1 AS (SELECT LEVEL L FROM DUAL CONNECT BY LEVEL & lt;=10000),
TAB2 AS (SELECT T1. L, L1, T2. L L2 FROM TAB1 T1 JOIN TAB1 T2 ON MOD (T2) L, T1. L)=0 AND T2. L & gt; T1. L),
TAB3 AS (SELECT L2, SUM (L1) L1 FROM TAB2 GROUP BY L2)
The SELECT L1 FROM TAB3 WHERE L2=L1 ORDER BY 1;

CodePudding user response:

refer to the second floor AHUA1001 response:
first of all to thank you for your sharing, but in line with a common learning, the principle of common progress, there are two problems, need to share with you,
First, your code in my environment, execution, there are some content that needs to be modified, below is the content of the modified,
DECLARE
I NUMBER;
J NUMBER;
N_sum NUMBER;
The BEGIN
I:=0;
N_sum:=0;
FOR I IN 1.. 1000 LOOP
N_sum:=0;
FOR j IN 1. I - 1 LOOP
IF
The MOD (I, j)=0
THEN
SELECT
N_sum + j
INTO
N_sum
The FROM
Dual;

END IF;
END LOOP;

IF
I=n_sum
THEN
Dbms_output. Put_line (I);
END IF;
END LOOP;

END;

The second block of code, only in the amount of data is large, or under the condition of the business logic is very complex, just can use, programmers spell is logic, rather than physical strength, the problem, can be obtained by direct query, don't need some code block loop,
Time is a bit hasty, didn't watch carefully, still should have room for optimization, for all to share,
Within the following statement, can query to 10000 the number of
WITH
TAB1 AS (SELECT LEVEL L FROM DUAL CONNECT BY LEVEL & lt;=10000),
TAB2 AS (SELECT T1. L, L1, T2. L L2 FROM TAB1 T1 JOIN TAB1 T2 ON MOD (T2) L, T1. L)=0 AND T2. L & gt; T1. L),
TAB3 AS (SELECT L2, SUM (L1) L1 FROM TAB2 GROUP BY L2)
The SELECT L1 FROM TAB3 WHERE L2=L1 ORDER BY 1;


Actually spell now program ape's physical strength, can't 996 cannot come out to mix

CodePudding user response:

Thanks for sharing
Don't suggest use sum this keyword as a variable name, behind putline should be set so that you can run again PUT_LINE

CodePudding user response:

refer to the second floor AHUA1001 response:
first of all to thank you for your sharing, but in line with a common learning, the principle of common progress, there are two problems, need to share with you,
First, your code in my environment, execution, there are some content that needs to be modified, below is the content of the modified,
DECLARE
I NUMBER;
J NUMBER;
N_sum NUMBER;
The BEGIN
I:=0;
N_sum:=0;
FOR I IN 1.. 1000 LOOP
N_sum:=0;
FOR j IN 1. I - 1 LOOP
IF
The MOD (I, j)=0
THEN
SELECT
N_sum + j
INTO
N_sum
The FROM
Dual;

END IF;
END LOOP;

IF
I=n_sum
THEN
Dbms_output. Put_line (I);
END IF;
END LOOP;

END;

The second block of code, only in the amount of data is large, or under the condition of the business logic is very complex, just can use, programmers spell is logic, rather than physical strength, the problem, can be obtained by direct query, don't need some code block loop,
Time is a bit hasty, didn't watch carefully, still should have room for optimization, for all to share,
Within the following statement, can query to 10000 the number of
WITH
TAB1 AS (SELECT LEVEL L FROM DUAL CONNECT BY LEVEL & lt;=10000),
TAB2 AS (SELECT T1. L, L1, T2. L L2 FROM TAB1 T1 JOIN TAB1 T2 ON MOD (T2) L, T1. L)=0 AND T2. L & gt; T1. L),
TAB3 AS (SELECT L2, SUM (L1) L1 FROM TAB2 GROUP BY L2)
The SELECT L1 FROM TAB3 WHERE L2=L1 ORDER BY 1;

Sorry, this is my hand, it is have some keywords are not standard grammar!
  • Related