Oracle
如何從本地電腦(mac)連接到遠端 Oracle DB 並執行腳本並將結果保存到 CSV?
我可以使用此簽名連接到遠端 Oracle DB:
sqlplus 'user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))'
現在想要執行腳本並將結果保存到本地 CSV 文件 - 怎麼做?
我已經有一個正在工作的腳本,但需要一個終端(bash)命令來(遠端)連接到數據庫,然後呼叫一個腳本(all in 1 command)。
猜測這樣的事情(但沒有奏效):
sqlplus 'user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))' @sql_script
謝謝。
從 12.2 版開始的 SQL*Plus 支持以 CSV 格式列印輸出。
您可以在腳本中添加以下內容:
set markup csv on
然後執行您的腳本,稱為
myscript.sql
:sqlplus 'user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))' @myscript.sql
例如:
[oracle@o71 ~]$ cat myscript.sql set markup csv on set feedback off echo off termout off spool my.csv select username, account_status, created from dba_users where username like 'SYS%'; spool off exit [oracle@o71 ~]$ sqlplus -S 'bp/bp@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = o71.balazs.vm)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = SIMPLE)))' @myscript.sql [oracle@o71 ~]$ cat my.csv "USERNAME","ACCOUNT_STATUS","CREATED" "SYS","OPEN","10-JAN-18" "SYSTEM","OPEN","10-JAN-18" "SYS$UMF","EXPIRED & LOCKED","10-JAN-18" "SYSBACKUP","EXPIRED & LOCKED","10-JAN-18" "SYSRAC","EXPIRED & LOCKED","10-JAN-18" "SYSKM","EXPIRED & LOCKED","10-JAN-18" "SYSDG","EXPIRED & LOCKED","10-JAN-18"