When you build a table with dynamic SQL in a stored procedure, if you insert

Source: Internet
Author: User

Ask your teacher a question, if you want to set up a table and insert data into a stored procedure, what should be done, if the table inserted into the data is not established before inserting data, the stored procedure will be Error notification table does not exist, causing the stored procedure to fail, is there any workaround?  The reason why a table is not built outside the stored procedure is to execute the stored procedure periodically, if there is a drop in the table, it will cause the stored procedure to insert data error, so you want to automatically in the stored procedure to consider the establishment and processing of the table, insert data does not consider the use of dynamic SQL to insert, Because of this can not use plsql things (such as the Remote link optimization tips), ask how to deal with, thank you!  simply put, it is how to implement in a process or package, build a table and insert, and insert cannot be dynamic SQL, For example, if the table does not have a stored procedure, the response error is invalid.
   
 
  1. CREATE OR REPLACE PROCEDURE y_p_test AS
  2. BEGIN
  3. drop_table(‘y_test‘);
  4. EXECUTE IMMEDIATE ‘ create table y_test (id number, name varchar2(20)) ‘;
  5. FOR i IN (SELECT/*+driving_site(b) */ * FROM small_tab_local a, [email protected] b
  6. WHERE a.sub_id=b.sub_id AND a.acc_id=b.acc_id)
  7. LOOP
  8. INSERT INTO y_test VALUES i;
  9. END LOOP;
  10. COMMIT;
  11. END;
a solution has been thought of, tested and effective, The idea is to save a drop table after the creation, and then use a save to insert the data, and then with a saved through the dynamic SQL in a way to call these 2 saved, because the dynamic SQL is executed to judge, so can be successfully executed for your reference
   
 
  1. create or replace procedure drop_table(x varchar2) as
  2. table1 number;
  3. begin
  4. select COUNT(*) INTO table1 from user_tables where table_name=upper(x);
  5. if table1 > 0
  6. then execute immediate ‘drop table ‘||x;
  7. end if;
  8. end;
  9. /
  10. CREATE OR REPLACE PROCEDURE y_p_test0
  11. AS
  12. BEGIN
  13. drop_table(‘y_test‘);
  14. EXECUTE IMMEDIATE ‘create table y_test as select * from fzt_dd where 1=2‘;
  15. END;
  16. /
  17. CREATE OR REPLACE PROCEDURE y_p_test AS
  18. BEGIN
  19. FOR i IN (SELECT * FROM fzt_dd WHERE ROWNUM<10)
  20. LOOP
  21. INSERT INTO y_test VALUES i;
  22. END LOOP;
  23. COMMIT;
  24. END;
  25. /
  26. CREATE OR REPLACE PROCEDURE y_p_test1 AS
  27. BEGIN
  28. EXECUTE IMMEDIATE ‘begin y_p_test0; end;‘;
  29. EXECUTE IMMEDIATE ‘begin y_p_test; end;‘;
  30. END;
  31. /


From for notes (Wiz)

When you build a table with dynamic SQL in a stored procedure, if you insert

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.