0

am using Spool in SQL Developer to get the Output into a CSV file. where as i have heard of use of DDL command can b done to get the create query of a table.

my query is as below

    spool "path/output.csv" 
    @"path/query.sql";
spool of;

and sql is as below

set sqlformat loader
select * from table;

can u pls suggest how can i use the DDL command to spool the create query of the table in a .csv file.

2 Answers 2

1

DBMS_METADATA is the answer in general for Oracle. But, if you're using SQL Developer, like you tagged in your question, then you can simply use the DDL command.

What is DDL? It's a client command you can run in SQLcl and SQL Developer that constructs the dbms_metadata.get_ddl() code for you.

So let's:

  • set our DDL options, how do we want it to look
  • not include the commands in the spooled file
  • not include the '4 rows selected' in our spooled file
  • specify where we want the spooled file
  • start the spool, set the filename
  • ask for the ddl
  • ask for the table rows in CSV
  • turn the spool off

The Code:

clear screen
set ddl storage, segment_attributes off
set echo off
set feedback off
cd c:\users\jdsmith\desktop
spool regions.sql
ddl regions
select /*csv*/ * from regions;
spool off

enter image description here

0
1

How about

set sqlformat loader
spool "path/output.csv" 
select * from table;
spool off;

Based on comments you posted, it looks like you want to spool CREATE TABLE statement. If so, use DBMS_METADATA package, e.g.

SQL> set long 10000
SQL> set pagesize 0
SQL>
SQL> select dbms_metadata.get_ddl('TABLE', 'DEPT', 'SCOTT') create_table from dual;

  CREATE TABLE "SCOTT"."DEPT"
   (    "DEPTNO" NUMBER(2,0),
        "DNAME" VARCHAR2(14),
        "LOC" VARCHAR2(13),
         CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USER_DATA"  ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USER_DATA"


SQL>
8
  • No @Littlefoot, this gives me all the records in the csv file. What i want is the CREATE query of that table in csv as an Output
    – Salva
    Commented Jan 20, 2022 at 6:55
  • I'm sorry, I don't quite understand. What exactly do you want to get as a result in OUTPUT.CSV file?
    – Littlefoot
    Commented Jan 20, 2022 at 6:58
  • Want the create query of that table, eg: like in Microsoft SSMS there is an option to pull the CREATE query as SCRIPT TABLE as >> CREATE TO >> File
    – Salva
    Commented Jan 20, 2022 at 7:01
  • Aha; use DBMS_METADATA, then. I edited the answer and posted an example; you'd include those commands into the .SQL file as well.
    – Littlefoot
    Commented Jan 20, 2022 at 7:05
  • am adding a link of a YouTube video for reference just to understand . youtu.be/8-aUXY2nSVs shift to timestamp 5:52. Thanks
    – Salva
    Commented Jan 20, 2022 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.