1. Registered User
Join Date
Apr 2004
Location
USA
Posts
28

Hello all,
I have created this table called 'messages'

CREATE TABLE MESSAGES(
MESSAGEID NUMBER(10)
CONSTRAINT MESSAGEID_PK PRIMARY KEY,
MESSAGE VARCHAR2(50)
CONSTRAINT MESSAGE_NN NOT NULL);

now I want to Write a PL/SQL block that uses a loop to insert values into the Messages table. Populate the MessageId column with numbers starting from 25 till 40. For each number determine if it is odd or even and then enter a statement into the Message column that is similar to the following: "The number X is ODD/EVEN." where x is the current number I am on.

Can anyone give me an idea?

2. Lost Boy
Join Date
Jan 2004
Location
Croatia, Europe
Posts
4,113
For example, something like that might do the job (select from your table would be OK):
PHP Code:
``` CREATE OR REPLACE PROCEDURE ins_msg AS BEGIN    INSERT INTO messages         VALUES (25, 'The number 25 is odd');    INSERT INTO messages         VALUES (26, 'The number 26 is even');    /* 27, 28, ..., 39 */    INSERT INTO messages         VALUES (40, 'The number 40 is even'); END;  ```
Or, you could use a loop to do the job. Of course, FIRST you should know how to determine if a number is odd or even (I don't expect you to know which function(s) to use, just to say it in a few words), and SECOND, learn how to use some kind of a loop.

So, put a few sentences here to see what's YOUR idea about the problem (what does it mean, "an odd number", what kinds of loops do you know about and which one of them seems nicest to you), and then I'd be happy to help you with a code.

3. Registered User
Join Date
Mar 2004
Location
Berkshire, UK
Posts
186
PHP Code:
``` DECLARE     vMessage    VARCHAR2(50); BEGIN     FOR i IN 25 .. 40 LOOP     /* find the remainder when dividing by 2*/         IF mod(i,2) = 1 THEN         /* if the remainder is 1 then the number we are on must be odd */             vMessage := 'The number '||to_char(i)||' is ODD.';         ELSE             vMessage := 'The number '||to_char(i)||' is EVEN.';         END IF;         INSERT INTO messages (messageid, message)                 VALUES (i, vMessage);     END LOOP;     COMMIT; END;  ```

4. Registered User
Join Date
Apr 2004
Location
USA
Posts
28
Thanks guys!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•