SQL 来自命令行的 sqlplus 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1639704/
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-01 04:11:39  来源:igfitidea点击:

sqlplus statement from command line

sqloraclesqlplus

提问by andersonbd1

Is it possible to do something like this?

有可能做这样的事情吗?

$ sqlplus -s user/pass "select 1 from dual"or
$ echo "select 1 from dual" | sqlplus -s user/pass

$ sqlplus -s user/pass "select 1 from dual"或者
$ echo "select 1 from dual" | sqlplus -s user/pass

I know I can put select 1 from dualin a file and do this:
$ sqlplus -s user/pass @myFile.sql

我知道我可以放入select 1 from dual一个文件并执行以下操作:
$ sqlplus -s user/pass @myFile.sql

but I'm wondering if it's actually necessary to create a file just to satisfy sqlplus

但我想知道是否真的有必要创建一个文件来满足 sqlplus

回答by David Mann

Just be aware that on Unix/Linux your username/password can be seen by anyone that can run "ps -ef" command if you place it directly on the command line . Could be a big security issue (or turn into a big security issue).

请注意,在 Unix/Linux 上,如果您将用户名/密码直接放在命令行上,则任何可以运行“ps -ef”命令的人都可以看到您的用户名/密码。可能是一个很大的安全问题(或变成一个很大的安全问题)。

I usually recommend creating a file or using here document so you can protect the username/password from being viewed with "ps -ef" command in Unix/Linux. If the username/password is contained in a script file or sql file you can protect using appropriate user/group read permissions. Then you can keep the user/pass inside the file like this in a shell script:

我通常建议创建一个文件或使用此处的文档,以便在 Unix/Linux 中使用“ps -ef”命令防止用户名/密码被查看。如果用户名/密码包含在脚本文件或 sql 文件中,您可以使用适当的用户/组读取权限进行保护。然后,您可以在 shell 脚本中将用户/密码保留在文件中,如下所示:

sqlplus -s /nolog <<EOF
connect user/pass
select blah;
quit
EOF

回答by Grant Lammi

I'm able to execute your exact query by just making sure there is a semicolon at the end of my select statement. (Output is actual, connection params removed.)

我可以通过确保在我的 select 语句末尾有一个分号来执行您的确切查询。(输出是实际的,连接参数已删除。)

echo "select 1 from dual;" | sqlplus -s username/password@host:1521/service 

Output:

输出:

         1
----------
         1

Note that is should matter but this is running on Mac OS X Snow Leopard and Oracle 11g.

请注意,这应该很重要,但这是在 Mac OS X Snow Leopard 和 Oracle 11g 上运行的。

回答by rofrol

My version

我的版本

$ sqlplus -s username/password@host:port/service <<< "select 1 from dual;"


         1
----------
         1

EDIT:

编辑:

For multiline you can use this

对于多行,您可以使用它

$ echo -e "select 1 from dual; \n select 2 from dual;" | sqlplus -s username/password@host:port/service


         1
----------
         1


         2
----------
         2

回答by DCookie

I assume this is *nix?

我假设这是*nix?

Use "here document":

使用“此处文档”:

sqlplus -s user/pass <<+EOF
select 1 from dual;
+EOF

EDIT: I should have tried your second example. It works, too (even in Windows, sans ticks):

编辑:我应该试过你的第二个例子。它也有效(即使在 Windows 中,没有滴答声):

$ echo 'select 1 from dual;'|sqlplus -s user/pw

         1
----------
         1


$