SQL/PLUS learning notes ECHO and SPOOL usage

Source: Internet
Author: User

Last time we introduced:SQL/PLUS Study NotesTo edit the current line of commands in the buffer, this article describes the SQL/PLUS learning notesECHOAndSPOOLNext, let's take a look at this part.

ECHO parameter settings:

SQL> show echo -- display the echo status

Echo OFF -- at this time, echo is OFF.

SQL> set echo on -- set it to on

SQL> show echo

Echo ON -- Enabled

When the script is run, each SQL statement or PL/SQL block in the script is displayed on the terminal. Run the test script as follows:

 
 
  1. SQL> @ test -- the following content is displayed on the terminal.
  2. SQL> select * from t1 where rownum <2;
  3.  
  4. ID NAME
  5. ------------------------------
  6. 1 Testing
  7.  
  8. SQL>
  9. SQL> truncate table t1;
  10.  
  11. Table truncated.
  12.  
  13. SQL>
  14. SQL> begin
  15. 2
  16. 3 for I in 1 .. 1000 loop
  17. 4
  18. 5 insert into t1 values (I, 'testing ');
  19. 6 end loop;
  20. 7 commit;
  21. 8
  22. 9 end;
  23. 10/
  24.  
  25. PL/SQL procedure successfully completed.
  26.  
  27. SQL>
  28. SQL> select count (*) from t1;
  29.  
  30. COUNT (*)
  31. ----------
  32. 1000

Use SPOOL to save the query result set

 
 
  1. SQL> spool outputfile -- The outputfile. lst file is generated in the current path by default.
  2. SQL> select * from t1 where rownum <3;
  3.  
  4. ID NAME
  5. ------------------------------
  6. 1 Testing
  7. 2 Testing
  8.  
  9. SQL> spool off -- terminate. All the content is written to the file outputfile.
  10.  
  11. [Oracle @ localhost ~] $ Cat outputfile. lst -- view the file content as follows:
  12. SQL> select * from t1 where rownum <3;
  13.  
  14. ID NAME
  15. ------------------------------
  16. 1 Testing
  17. 2 Testing
  18.  
  19. SQL> spool off

This function can help generate some dynamic batch processing scripts, such as deleting tables that meet certain conditions in the user's emcd:

 
 
  1. SQL> show user
  2. USER is "EMCD"
  3. SQL> spool droptable. SQL
  4. SQL> select 'drop table' | objec_name from user_objects where object_type = 'table ';
  5. Select 'drop table' | objec_name from user_objects where object_type = 'table'
  6. *
  7. ERROR at line 1:
  8. ORA-00904: "OBJEC_NAME": invalid identifier
  9.  
  10. SQL> l
  11. 1 * select 'drop table' | objec_name from user_objects where object_type = 'table'
  12. SQL> c/objec _/object _
  13. 1 * select 'drop table' | object_name from user_objects where object_type = 'table'
  14. SQL>/
  15.  
  16. 'Droptable' | OBJECT_NAME
  17. --------------------------------------------------------------------------------
  18. Drop tableT1
  19. Drop tableBIN $ qokoVJ6g4HHgQAB/AQAzMg = $0
  20. Drop tableTOAD_PLAN_TABLE
  21.  
  22. SQL> l
  23. 1 * select 'drop table' | object_name from user_objects where object_type = 'table'
  24. SQL> c/'drop table'
  25. 1 * select 'drop table' | object_name from user_objects where object_type = 'table'
  26. SQL>/
  27.  
  28. 'Droptable' | OBJECT_NAME
  29. --------------------------------------------------------------------------------
  30. Drop table T1
  31. Drop table BIN $ qokoVJ6g4HHgQAB/AQAzMg = $0
  32. Drop table TOAD_PLAN_TABLE
  33.  
  34. SQL> spool off -- end input
  35. SQL>!
  36.  
  37. [Oracle @ localhost ~] $ Cat droptable. SQL -- view the output content, as shown below:
  38.  
  39. SQL> select 'drop table' | objec_name from user_objects where object_type = 'table ';
  40. Select 'drop table' | objec_name from user_objects where object_type = 'table'
  41. *
  42. ERROR at line 1:
  43. ORA-00904: "OBJEC_NAME": invalid identifier
  44.  
  45.  
  46. SQL> l
  47. 1 * select 'drop table' | objec_name from user_objects where object_type = 'table'
  48. SQL> c/objec _/object _
  49. 1 * select 'drop table' | object_name from user_objects where object_type = 'table'
  50. SQL>/
  51.  
  52. 'Droptable' | OBJECT_NAME
  53. --------------------------------------------------------------------------------
  54. Drop tableT1
  55. Drop tableBIN $ qokoVJ6g4HHgQAB/AQAzMg = $0
  56. Drop tableTOAD_PLAN_TABLE
  57.  
  58. SQL> l
  59. 1 * select 'drop table' | object_name from user_objects where object_type = 'table'
  60. SQL> c/'drop table'
  61. 1 * select 'drop table' | object_name from user_objects where object_type = 'table'
  62. SQL>/
  63.  
  64. 'Droptable' | OBJECT_NAME
  65. --------------------------------------------------------------------------------
  66. Drop table T1
  67. Drop table BIN $ qokoVJ6g4HHgQAB/AQAzMg = $0
  68. Drop table TOAD_PLAN_TABLE
  69.  
  70. SQL> spool off

In this way, the SQL statement for dynamically deleting some tables is generated.

Here is the introduction of ECHO and SPOOL in SQL/PLUS study notes. I hope this introduction will be helpful to you!

Related Article

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.