Matteo
Matteo

Reputation: 346

Bash script to export a table into another table

I created a script to export a table from my db into a .csv file

#!/usr/bin/bash

FILE="example.csv"

sqlplus -s abcd/abcd@XE  <<EOF       

SET PAGESIZE 50000
SET COLSEP ","
SET LINESIZE 200
SET FEEDBACK OFF

SPOOL $FILE

SELECT * FROM myTable;

SPOOL OFF
EXIT

and now I'd like to modify this script and export my table into another. How can I change my code?

Upvotes: 0

Views: 914

Answers (1)

Peder Klingenberg
Peder Klingenberg

Reputation: 41225

By "exporting your table into another", do you mean copying data from one table to another? If you don't need indexes or keys or other features on your new table initially, i.e. if it's not for production use, it's quite simple:

#!/usr/bin/bash

TABLE="myOtherTable"
sqlplus -s abcd/abcd@XE  <<EOF       
CREATE TABLE $TABLE as SELECT * FROM myTable;
EXIT

You could also do a create table statement first, specifying columns, keys and storage options as any other table, and then have a separate line that does INSERT INTO $TABLE (SELECT * FROM myTable) to fill it with data copied from myTable.

Upvotes: 1

Related Questions