关于dbms_metadata.get_ddl 的使用

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-25 08:29:55

查询具体的表名大写加单引号.批量不加。

  1. SCOTT@PROD>select dbms_metadata.get_ddl('TABLE','DEPT','SCOTT') CODE from dual;
  2. CREATE TABLE "SCOTT"."DEPT"
  3. ( "DEPTNO" NUMBER(2,0),
  4. "DNAME" VARCHAR2(14),
  5. "LOC" VARCHAR2(13),
  6. CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")
  7. USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  8. STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  9. PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  10. BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  11. TABLESPACE "USERS" ENABLE
  12. ) SEGMENT CREATION IMMEDIATE
  13. PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
  14. NOCOMPRESS LOGGING
  15. STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  16. PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  17. BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  18. TABLESPACE "USERS"
  19. SCOTT@PROD>

如果要查询所有表的结构呢:
下面只选取了一部分:

  1. SCOTT@PROD>select dbms_metadata.get_ddl('TABLE',t.table_name) from user_tables t;
  2. CREATE TABLE "SCOTT"."OB_JEST"
  3. ( "ID" NUMBER(*,0),
  4. "NAME" VARCHAR2(25),
  5. "YEAR" NUMBER(*,0)
  6. ) SEGMENT CREATION DEFERRED
  7. PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
  8. NOCOMPRESS LOGGING
  9. TABLESPACE "USERS"
  10. CREATE TABLE "SCOTT"."ANIMALS"
  11. ( "NAME" VARCHAR2(10),
  12. "SPECIES" VARCHAR2(20),
  13. "DATE_OF_BIRTH" DATE
  14. ) SEGMENT CREATION DEFERRED
  15. PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
  16. NOCOMPRESS LOGGING
  17. TABLESPACE "USERS"
  18. CREATE TABLE "SCOTT"."OB_TEST"
  19. ( "ENAME" VARCHAR2(20)
  20. ) SEGMENT CREATION DEFERRED
  21. PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
  22. NOCOMPRESS LOGGING
  23. TABLESPACE "USERS"

也可以查询sp view sequence等等。做相应的替换就可以了。

  1. select dbms_metadata.get_ddl('view',t.view_name) from user_views t;
  2. SCOTT@PROD>select dbms_metadata.get_ddl('PROCEDURE',t.object_name) from user_objects t where object_type='PROCEDURE';
  3. CREATE OR REPLACE PROCEDURE "SCOTT"."CHANGE_SAL" (v_empno in number,
  4. v_out out number )
  5. as
  6. v_count number;
  7. begin
  8. select count(1) into v_count from emp where empno = v_empno;
  9. if v_count=0 then
  10. v_out:=1;
  11. else
  12. v_out:=0;
  13. end if;
  14. end;
  15. CREATE OR REPLACE PROCEDURE "SCOTT"."PROCEDURE_1" (v_1 in number)
  16. as
  17. type dept_n is table of emp%rowtype index by binary_integer;
  18. v_dept dept_n;
  19. begin
  20. select empno,ename,job bulk collect into v_dept from emp;
  21. for i in v_dept.first..v_dept.last loop
  22. dbms_output.put_line(v_dept(i).empno);
  23. end loop;
  24. end;
  25. SCOTT@PROD>

 

查询具体的表名大写加单引号.批量不加。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!