先创建表:
CREATE TABLE TB_USER
(
ID INTEGER PRIMARY KEY,
USER_NAME VARCHAR2(20) NOT NULL,
USER_AGE INTEGER NOT NULL
);
CREATE SEQUENCE SEQ_USER
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;
CREATE OR REPLACE TRIGGER TR_USER BEFORE INSERT ON TB_USER FOR EACH ROW
BEGIN
SELECT SEQ_USER.NEXTVAL INTO :NEW.ID FROM DUAL;
END;
然后用for循环来插入数据:
DECLARE
V_AGE TB_USER.USER_AGE%TYPE;
V_NAME TB_USER.USER_NAME%TYPE;
BEGIN
FOR I IN 1..100 LOOP
DBMS_OUTPUT.put_line(I);
SELECT DBMS_RANDOM.value(1, 100) INTO V_AGE FROM DUAL;
V_NAME := 'FOR_' || V_AGE;
INSERT INTO TB_USER(USER_NAME, USER_AGE) VALUES (V_NAME, V_AGE);
END LOOP;
COMMIT;
END;
倒序插入数据(从100到1):
DECLARE
V_AGE TB_USER.USER_AGE%TYPE;
V_NAME TB_USER.USER_NAME%TYPE;
BEGIN
FOR I IN REVERSE 1..100 LOOP
DBMS_OUTPUT.put_line(I);
SELECT DBMS_RANDOM.value(1, 100) INTO V_AGE FROM DUAL;
V_NAME := 'FOR_REVERSE_' || V_AGE;
INSERT INTO TB_USER(USER_NAME, USER_AGE) VALUES (V_NAME, V_AGE);
END LOOP;
COMMIT;
END;