DECLARE
V_sql_drop_table VARCHAR2 (): = ' DROP TABLE my_test2 ';
V_sql_create_table VARCHAR2: = ' CREATE TABLE my_test2 (not_null VARCHAR2 () not NULL, only_int INTEGER) ';
BEGIN
EXECUTE IMMEDIATE v_sql_create_table; --Create a test table
EXCEPTION
--Throws an exception if the table already exists
When OTHERS Then
EXECUTE IMMEDIATE v_sql_drop_table; --Delete First
EXECUTE IMMEDIATE v_sql_create_table; --Re-create
END;
/
--2. Test with the test table we just created
DECLARE
V_count INTEGER; --Number of rows recorded in the table
V_int_val My_test. Only_int%type; --Use the%type keyword to refer to a table for a field type declaration variable
BEGIN
V_int_val: = 123456;
--Insert a correct data
INSERT into My_test2 VALUES (' test_success ', v_int_val);
--The number of query bars is 1, and we found that the insertion was successful.
SELECT COUNT (*) into V_count from My_test;
Dbms_output.put_line (' my_test table ' | | V_count | | ' Records ');
--Insert an incorrect data because the second field is int, and inserting character data will definitely go wrong
/* INSERT into My_test VALUES (' Test_fail ', ' ABC '); */
--Final Commit changes
COMMIT;
EXCEPTION
--Exception handling
When OTHERS Then
ROLLBACK; --rollback on exception, so that the correct data inserted for the first time is not saved to the database
Dbms_output.put_line (Exception occurred in ' [PL/SQL transaction], error code: ORA ' | | sqlcode);
--We verify that the data in the table is 0.
SELECT COUNT (*) into V_count from My_test;
Dbms_output.put_line (' After rollback, the my_test table has ' | | V_count | | ' Records ');
END; --End of executable statement
/--This symbol indicates the execution of this PL/SQL code
Oracle executes multiple PL/SQL blocks