Oracle executes multiple PL/SQL blocks

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.