Results 1 to 4 of 4

Thread: Need help

  1. #1
    Join Date
    Apr 2004
    Location
    USA
    Posts
    28

    Unanswered: Need help

    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?
    ___________
    GorkhaliOCA
    USA

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    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. #3
    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 (messageidmessage)
                    
    VALUES (ivMessage);
        
    END LOOP;
        
    COMMIT;
    END
    There are 10 types of people in the world, those that know Binary and those that don't.

  4. #4
    Join Date
    Apr 2004
    Location
    USA
    Posts
    28
    Thanks guys!
    ___________
    GorkhaliOCA
    USA

Posting Permissions

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