oracle 从批处理文件执行 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14294068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:47:05  来源:igfitidea点击:

Execute SQL from batch file

oraclebatch-filesqlplus

提问by Manaysah

I'm new to batch files scripting.

我是批处理文件脚本的新手。

All I want is creating a batch file that calling SQL file and store results in text file .

我想要的只是创建一个批处理文件,该文件调用 SQL 文件并将结果存储在文本文件中。

Can anyone help me, your help is highly appreciated,

任何人都可以帮助我,非常感谢您的帮助,

This is the first time I need to create such files.

这是我第一次需要创建这样的文件。

回答by DazzaL

using a batch file:

使用批处理文件:

save and run this:

保存并运行:

@echo off
sqlplus -s -l user/pass@yourdb @yoursql.sql>your_log.log

p.s. be sure to have the last line of your sql script as exit;or the batch file will hang.

ps 一定要有你的 sql 脚本的最后一行,exit;否则批处理文件将挂起。

回答by Andomar

sqlcmd -S sqlservername -i yoursqlfile.sql -U username -P password -o outputfile.txt

回答by Fr3dY

I created a more advanced launcher, try it out (you can find the latest version at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html)

我创建了一个更高级的启动器,试试看(你可以在http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle找到最新版本- sql-linux-sh-scripts-available-here.html)

Here's the code anyway:

无论如何,这是代码:

launcher.cmd

启动器.cmd

@ECHO OFF

rem Script Launcher by Fr3dY v1.4
rem ##############################
rem Version History:
rem 1.4 - Misc. fixes
rem 1.3 - Merged with 'server launcher', now accepts both SQL and SHELL SCRIPTS
rem 1.2 - Interactive prompt to show the file on screen
rem 1.1 - No need to add 'quit;' or 'exit;' in the .sql file anymore
rem       Fixed sqlplus waiting for username/password if the first attempt was unsuccessful
rem       Log file is generated automatically, including date and time in name
rem       Misc. fixes
rem 1.0 - Initial Version

:MAIN
::Path of PLINK
set PLINK="C:\Program Files (x86)\PuTTY\plink.exe"
::List with TNS NAMES
set dbservers=launcher-databases.txt
::List with LINUX SERVERS
set linuxservers=launcher-servers.txt

set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set dt=%dt: =0%

echo Choose launcher mode:
echo 1) Database scripts (.sql files)
echo 2) Shell scripts (.sh files)
set /p launchermode="Insert value: "
echo.

if %launchermode%==1 (
  set extension=sql
  set servers=%dbservers%
  set mode=DB
) else (
    if %launchermode%==2 (
      set extension=sh
      set servers=%linuxservers%
      set mode=OS
    ) else (echo "Incorrect value, exiting..." & goto :END)
)    

if exist %servers% (
  goto :LISTFILES
) else echo FILE %servers% NOT FOUND, ABORTING & goto :END


:LISTFILES
echo Listing *.%extension% files...
echo.
dir /b *.%extension%
echo.

set /p file=Name of the file to be launched (without extension)? 

if exist %file%.%extension% (
  goto :CONFIRMSHOW
) else echo FILE %file%.%extension% NOT FOUND, ABORTING & goto :END


:CONFIRMSHOW
echo.
set /p confirm=Show the script %file%.%extension% on screen now? 
if %confirm%==y (
  goto :SHOWSCRIPT
  ) else goto :CONFIRMEXEC
echo.

:SHOWSCRIPT
echo.
echo Content of %file%.%extension%
echo ######################
type %file%.%extension%
echo.
echo ######################
echo.

:CONFIRMEXEC
set /p confirm=Are you sure you want to execute this script? 
if %confirm%==y (
  set /p user=%mode% username?  
  goto :HInput
  ) else echo ABORTED & goto :END
echo.
echo Output saved to %file%_%dt%.log
echo.
Goto :END

:HInput
::Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI
::Version 3.0     
SetLocal DisableDelayedExpansion
echo.
Echo Enter password:
Set "Line="
Rem Save 0x08 character in BS variable
For /F %%# In (
'"Prompt;$H&For %%# in (1) Do Rem"'
) Do Set "BS=%%#"

:HILoop
Set "Key="
For /F "delims=" %%# In (
'Xcopy /L /W "%~f0" "%~f0" 2^>Nul'
) Do If Not Defined Key Set "Key=%%#"
Set "Key=%Key:~-1%"
SetLocal EnableDelayedExpansion
If Not Defined Key echo. & Goto :HIEnd
If %BS%==^%Key% (Set /P "=%BS% %BS%" <Nul
Set "Key="
If Defined Line Set "Line=!Line:~0,-1!"
) Else Set /P "=*" <Nul
If Not Defined Line (EndLocal &Set "Line=%Key%"
) Else For /F delims^=^ eol^= %%# In (
"!Line!") Do EndLocal &Set "Line=%%#%Key%"
Goto :HILoop

:HIEnd
if %launchermode%==1 (
  goto :EXECDB
) else goto :EXECLINUX

:EXECDB
FOR /f %%A IN (%servers%) DO CALL ECHO DATABASE: %%A & ECHO DATABASE: %%A >> %file%_%dt%.log & sqlplus -S -L %user%/!Line!@%%A < %file%.%extension% >> %file%_%dt%.log
goto :END

:EXECLINUX
FOR /f %%A IN (%servers%) DO CALL ECHO SERVER: %%A & ECHO SERVER: %%A >> %file%_%dt%.log & echo y | %PLINK% %user%@%%A -pw !Line! "exit" & %PLINK% %user%@%%A -pw !Line! -batch -m %file%.%extension% >> %file%_%dt%.log & echo. >> %file%_%dt%.log
goto :END

:END
pause