我有这个过程将表保存到CSV文件但不完美:
sqlplus / as sysdba
set heading off
SET TERMOUT OFF
spool MY_TABLE.csv
select column_id||','||column_name||','||data_type||','||nullable||','||data_length from user1.MY_TABLE;
spool off;
exit
问题是无法关闭打印到终端(我有SET TERMOUT OFF但由于某种原因不能抑制输出)。
我想从终端运行带有查询的sqlplus(而不是打开sqlplus并从那里运行)。
怎么做?
答案 0 :(得分:2)
linux create_csv.sh
的示例1在监视器屏幕上显示表数据。
#!/bin/sh
sqlplus -s / as sysdba <<EOF
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ','
spool test2.csv
select code,name,code_rail from alexs.all_station;
spool off
exit;
EOF
Linux createv2_csv.sh
的示例2不显示任何内容。
#!/bin/sh
$ORACLE_HOME/bin/sqlplus -s / as sysdba @csv2.sql
csv2.sql删除了输出文件test2.csv
中字段之间的空格和制表符set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set echo off
set linesize 1000
set pagesize 0
set wrap off
spool test2.csv
select code||','||name||','||code_rail from alexs.all_station;
spool off
exit;
Windows test_csv.bat
sqlplus -s user/password@net_alias < csv.sql
csv.sql
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ','
spool test.csv
select code,name,code_rail from alexs.all_station;
spool off
exit;
Windows powershell脚本create_csv.ps1
# login DBA user
$username_dba = "system"
# PASSWORD DBA user !!!!
$password_dba = "manager"
# Connect without alias
$tnsalias_db = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = test.com)(PORT = 1521)))(CONNECT_DATA = (SID = test)))"
$sqlQuery =
@"
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ','
spool test.csv
select code,name,code_rail from alexs.all_station;
spool off
exit
"@
$sqlOutput = $sqlQuery | sqlplus -silent $username_dba/$password_dba@$tnsalias_db
示例输出test.csv
966203,ГУРСКОЕ , 96
966307,УКТУР , 96
966400,КЕНАЙ , 96
966504,ВЫСОКОГОРНАЯ , 96
966608,КЕНАДА , 96
966805,ТУЛУЧИ , 96
966909,АКУР , 96
967009,ТУМНИН , 96
967102,ХУТУ , 96
967206,УСТЬ-ОРОЧИ , 96
967305,МОНГОХТО , 96
967403,ЛАНДЫШИ , 96
967526,ТОКИ , 96
967600,ВАНИНО , 96
967704,ВАНИНО-ПЕРЕВАЛКА , 96
967808,ВАНИНО-ЭКСПОРТ , 96
968001,ВИНЕВИТИНО , 96
示例输出test2.csv
971907,ВЯЗЕМСКАЯ,96
972007,АВАН,96
972100,РОЗЕНГАРТОВКА,96
972204,БИКИН,96
972308,ЗВЕНЬЕВОЙ,96
972401,БУРЛИТ-ВОЛОЧАЕВСКИЙ,96
972505,ЛУЧЕГОРСК,96
972609,ЛАСТОЧКА,96
972702,ГУБЕРОВО,96
972721,ЭБЕРГАРД,96
972806,ДАЛЬНЕРЕЧЕНСК I,96
973207,ДАЛЬНЕРЕЧЕНСК II,96
973300,ЛАЗО,96
973404,ГРУШЕВОЕ,96
973508,ПРОХАСКО,96
973601,ФИЛАРЕТОВКА,96
973705,РУЖИНО,96
973809,ЛЕСОЗАВОДСК I,96
973813,1634 КМ,96
973902,ШМАКОВКА,96
974002,СУНГАЧ,96
974106,СВИЯГИНО,96
974229,ДРОЗДОВ,96