PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)


問題描述

PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)

I have to create a PL/SQL block to insert 10 to 100 multiples of 10 in a table called TEN_MULTIPLES that I have to create... (SCHEMA ‑> TEN_MULTIPLES(numbervalue)). I will have to insert inside the table only 10,20,30,...,100 but exluding 50 and 90. So far I have done this... is it correct?

DECLARE  
  CREATE TABLE ten_multiples
     (numbervalue NUMBER (3));
BEGIN
  FOR i IN 9..101 LOOP
      IF (i = 50 OR i = 90) THEN
      ELSIF (i%10 = 0) THEN
         INSERT INTO ten_multiples
          VALUE (i);
      END IF;
  END LOOP;
END;

When I use 10..100 are 10 and 100 included and evaluated as 'i' in the loop?

I need also to find the MAXIMUM number from that table using a cursor, so in this case 100, store it in a variable 'num' declared in the DECLARE part and print it out...

DECLAR
   CURSOR my_cursor IS
    SELECT MAX(v_number) FROM ten_multiples;
   num NUMBER;
BEGIN
  OPEN my_cursor;
    FETCH my_cursor INTO (num);
    DBMS_OUTPUT.PUT_LINE(‘Maximum number is ‘ | num);
  CLOSE my_cursor;
END;

Is this right?

I really thank you in advance :)


參考解法

方法 1:

Why is so much PL/SQL coursework consists of exercises in how not to use PL/SQL?

insert into ten_multiples
with data as ( select level*10 as mult
               from dual
               connect by level <=10)
select * from data
where mult not in (50,90)
/

方法 2:

First part:

  • You cannot execute the CREATE TABLE statement directly in a PL/SQL context. You must use the DBMS_DDL package or dynamic SQL via the EXECUTE IMMEDIATE command, if you must execute within PL/SQL context.
  • Yes number literals in the for loop are included.

Second part:

  • Use DECLARE not DECLAR
  • Your SELECT member must be a column of the table, not v_number
  • Your single quote character is incorrect, use ', not ‘. 
  • Use double pipe for concatenation, not single.

Finally:

  • Actually run these commands through SQL*Plus and listen to the tool. 
  • Trying is your friend.

(by user2179694APCMichael O'Neill)

參考文件

  1. PL/SQL block and LOOP exercise (CC BY‑SA 3.0/4.0)

#plsql #SQL #cursor #block #oracle






相關問題

RAISE_APPLICATION_ERROR 不返回消息 (RAISE_APPLICATION_ERROR doesn't return the message)

PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)

如何從列中僅提取編號的行 (How to extract only numbered rows from a column)

如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)

使用 DBMS_OUTPUT.put_line 顯示錯誤消息 (Display error message using DBMS_OUTPUT.put_line)

如何編寫將Oracle數據庫表數據導出到excel文件的程序 (How to write a Procedure which exports Oracle database table data into excel file)

如何重載對像類型中的方法 (How to overload a method in an object type)

有沒有辦法以編程方式從 Oracle 包中提取表引用? (Is there a way to programmatically extract table references from an Oracle package?)

批量插入 Oracle 數據庫:哪個更好:FOR 游標循環還是簡單的選擇? (Bulk Insert into Oracle database: Which is better: FOR Cursor loop or a simple Select?)

如何使用參數“foo 表”執行 SP? (How to execute SP with arguments 'table of foo'?)

PL/SQL 我做錯了什麼? (PL/SQL What am I doing wrong?)

PL/SQL 顯示帶有條件的表中的數據 (PL/SQL Display Data From a Table with a Condition)







留言討論