简体   繁体   English

命令行SQL无效凭据

[英]Command line SQL invalid credentials

How can I make the USERNAME, PASSWORD and DATABASENAME INPUT reappear when I input invalid values or when the SQL did not connect? 输入无效值或未连接SQL时,如何重新显示USERNAME,PASSWORD和DATABASENAME INPUT?

样品

After the above image, the COMMAND LINE will just close. 上图之后,命令行将立即关闭。

This is the batch(.bat) file. 这是批处理(.bat)文件。

@echo off

sqlplus /nolog @C:\Users\myuser\Desktop\conn.sql

ECHO About to exit.

timeout t/ 30

:pause

And this is the conn.sql file 这是conn.sql文件

conn &&username@&&DBNAME


SPOOL C:\Sample.log
SELECT 1 FROM DUAL /
SPOOL OFF

EXIT

According to Oracle 11g – SQL Plus Command Line Options and Starting SQL Plus it is possible to specify user name, password and database already on command line of sqlplus.exe . 根据Oracle 11g – SQL Plus命令行选项 启动SQL Plus ,可以在sqlplus.exe命令行上指定用户名,密码和数据库。

I don't have sqlplus.exe to test it out, but something like below should work with appropriate adapting content of file conn.sql . 我没有sqlplus.exe可以对其进行测试,但是类似以下内容的东西应该可以与conn.sql文件的适当适应内容一起工作

@echo off
setlocal
set "SqlUserName="
set "SqlPassword="
set "SqlDatabase="
:Credentials
set /P "SqlUserName=Enter username: "
set /P "SqlPassword=Enter password: "
set /P "SqlDatabase=Enter database: "
sqlplus.exe -L "%SqlUserName%/%SqlPassword%@%SqlDatabase%" /nolog @C:\Users\myuser\Desktop\conn.sql
if errorlevel 1 goto Credentials
echo About to exit.
timeout /t 30
endlocal

You can enhance this batch code further for example with code to check if batch user has really entered user name, password and database. 您可以进一步增强此批处理代码,例如,使用代码来检查批处理用户是否确实输入了用户名,密码和数据库。

The values of the 3 environment variables can be re-used again on other batch lines inserted above command endlocal . 3个环境变量的值可以在命令endlocal上方插入的其他批处理行中再次使用。

Edit: Added a simple loop in case of sqlplus.exe exits with any code greater or equal 1. The user of the batch code has the possibility to just hit key RETURN or ENTER to use the user name, password or database name entered already before. 编辑:添加了一个简单循环,以防sqlplus.exe退出且任何大于或等于1的代码。批处理代码的用户可以按回车键或ENTER键使用之前输入的用户名,密码或数据库名。 The final solution must be most likely better than what is posted here as entering the credentials once again should be only done on error accessing the database and not on other errors. 最终解决方案必须比此处发布的解决方案更好,因为仅在访问数据库时出错,而不是在其他错误时才再次输入凭据。 So the batch files has to distinguish between the various exit codes returned by SQL*PLUS. 因此,批处理文件必须区分SQL * PLUS返回的各种退出代码。

See also the answers on Stack Overflow questions Sql*plus always return exit code 0? 另请参见有关堆栈溢出问题的答案Sql * plus始终返回退出代码0? and How do I capture a SQLPlus exit code within a shell script? 以及如何在Shell脚本中捕获SQLPlus退出代码?

The only difference is that in a Unix shell script if [ $? != 0 ] 唯一的区别是在Unix shell脚本中, if [ $? != 0 ] if [ $? != 0 ] is equivalent to if errorlevel 1 in a Windows batch file (if exit code is not negative which is usually true). if [ $? != 0 ]等效于Windows批处理文件中的错误if errorlevel 1 (如果退出代码不是负数,通常为true)。 See the Microsoft support articles Testing for a Specific Error Level in Batch Files and Correct Testing Precedence of Batch File ERRORLEVELs about evaluating exit codes with if errorlevel . 请参阅Microsoft支持文章“ 在批处理文件中测试特定错误级别”和“ 关于批处理文件ERRORLEVEL的正确测试优先级”,以评估带有if errorlevel退出代码。

Batch File 批处理文件

@echo off
setlocal
set "SqlUserName="
set "SqlPassword="
set "SqlDatabase="
:Credentials
set /P "SqlUserName=Enter username: "
set /P "SqlPassword=Enter password: "
set /P "SqlDatabase=Enter database: "
@(
echo whenever sqlerror exit failure
echo connect %SqlUserName%/%SqlPassword%@%SqlDatabase%
echo select * from dual;
echo exit 
) | sqlplus.exe -s /nolog 
if errorlevel 1 goto Credentials
if errorlevel 0 sqlplus /nolog  @C:\Users\myuser\Desktop\conn.sql 
echo About to exit.
timeout /t 30
endlocal

Conn.sql 连接数据库

SPOOL C:\Sample.log
SELECT 1 FROM DUAL /
SPOOL OFF

EXIT

Thanks to Mofi and Alex: How do I prompt input for database on command line? 感谢Mofi和Alex: 如何在命令行上提示输入数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM