ようこそ (Jyokoso) MY BLOG

いらっしゃいませ (hajimemashite) สำหรับผู้เข้าใหม่นะค่ะ ^^V

25 กุมภาพันธ์ 2554

How to Create Auto Increment Columns in Oracle

After pointing out how not to create auto increment columns in Oracle, I suppose I should point out how to create auto increment columns in oracle.
Many will gripe about this not being a standard feature in Oracle, but when it’s as easy as two more commands after your CREATE TABLE command I can’t see any good reason to use fancy SQL on every insert.
First let’s create a simple table to play with.
SQL> CREATE TABLE test
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));

Table created.
Now we’ll assume we want ID to be an auto increment field. First we need a sequence to grab values from.
SQL> CREATE SEQUENCE test_sequence
START WITH 1
INCREMENT BY 1;

Sequence created.
Now we can use that sequence in an BEFORE INSERT trigger on the table.
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
/

Trigger created.
This trigger will automatically grab the next value from the sequence we just created and substitute it into the ID column before the insert is completed.
Now we’ll do some inserts:
SQL> INSERT INTO test (name) VALUES ('Jon');
1 row created.
SQL> INSERT INTO test (name) VALUES (’Bork’);
1 row created.
SQL> INSERT INTO test (name) VALUES (’Matt’);
1 row created.
SQL> SELECT * FROM test;
ID NAME
———- ——————————
1 Jon
2 Bork
3 Matt


ที่มา :http://situsnya.wordpress.com/2008/09/02/how-to-create-auto-increment-columns-in-oracle/

14 กุมภาพันธ์ 2554

ตรวจทุก record ว่าเป็นไปตามเงื่อนไขไหม

BEGIN
    GO_BLOCK('BG_USAGE_PAYNAME');
    FIRST_RECORD;
FOR i IN 0..NVL(:bg_usage_payname.nb_cnt_rec,0) LOOP
--message(:bg_usage_payname.nb_cnt_rec);pause;
        IF     :BG_USAGE_PAYNAME.PAY_TYPE_CODE IS NOT NULL AND :BG_USAGE_PAYNAME.PAY_TYPE_CODE != STBG.GET_PARAMETER('PAY_TYPE_PAC','BG')  THEN
                UTIL.SHOW_MESSAGE('STOP','BG','B0148',STFN.PAYMENT_TYPE_NAME(STBG.GET_PARAMETER('PAY_TYPE_PAC','BG')),GET_ITEM_PROPERTY('BG_USAGE_MASTER.INTERNAL_CHARGE_YN',PROMPT_TEXT));
                :BG_USAGE_MASTER.INTERNAL_CHARGE_YN := 'N';
                RAISE FORM_TRIGGER_FAILURE;
        ELSE
                IF NVL(:BG_USAGE_MASTER.INTERNAL_CHARGE_YN,'N') = 'Y' THEN
                    Set_LOV_Property('M_PAY_TYPE_CODE_LOV',GROUP_NAME,'PAY_TYPE_CODE2_REC');
                ELSE
                    Set_LOV_Property('M_PAY_TYPE_CODE_LOV',GROUP_NAME,'PAY_TYPE_CODE_REC');
                END IF;
        END IF;
    NEXT_RECORD;
END LOOP;   
    GO_ITEM('BG_USAGE_MASTER.INTERNAL_CHARGE_YN');
END;