SQL*Plus如何在一个语句中执行环境设置和查询

发布于 2025-01-22 17:34:50 字数 472 浏览 2 评论 0原文

我执行以下查询,该查询返回一个值,并且可以正常工作。

echo "select myquery ;" | sqlplus user/pass@dbase | | head -4 | tail -1

问题是,当返回的一个值太大时,它会被多行分开,因此我进一步使用的ETL工具会遇到问题。 因此,我尝试使用Linesize选项,但无法将其与查询结合使用。

如果我连接到sqlplus并运行: 设置LINESIZE 3200后跟选择MyQuery;输出很好,但我无法在Echo语句中组合它们。 我以不同的方式尝试了:

echo "SET LINESIZE 3200 / select myquery ;" | sqlplus user/pass@dbase | | head -4 | tail -1

有什么想法吗?

谢谢!

I execute the following query which returns one value and it works fine.

echo "select myquery ;" | sqlplus user/pass@dbase | | head -4 | tail -1

The issue is that when the one value returned is too large it gets split in multiple rows thus the ETL tool that I further use gets problems.
Therefore I have tried to use the linesize option but I am not able to combine it with the query.

If I connect to sqlplus and run:
SET LINESIZE 3200 followed by select myquery ; the output is fine but I am not able to combine them in the echo statement.
I have tried in different ways, for example:

echo "SET LINESIZE 3200 / select myquery ;" | sqlplus user/pass@dbase | | head -4 | tail -1

Any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2025-01-29 17:34:50

HEREDOC(如 @Philippe的答案所示)将更容易阅读和维护,但是如果出于某种原因您真的想将其保持一行,则可以将-e flag添加到<代码> echo 并嵌入新行:

echo -e "SET LINESIZE 3200 \n select myquery ;" | sqlplus user/pass@dbase | head -4 | tail -1

或使用printf :(

printf "set linesize 3200 \n select myquery ;\n" | sqlplus user/pass@dbase | head -4 | tail -1

在两者中,我从原始版本中取出了额外的|

)为了避免需要并向输出进行尾声:

printf "set linesize 3200 \n set pagesize 0 \n set feedback off \n select myquery ;\n" | sqlplus -s user/pass@dbase

或使用各种set选项组合:

printf "set linesize 3200 pagesize 0 feedback off \n select myquery ;\n" | sqlplus -s user/pass@dbase

-s抑制横幅;从您使用的头部/尾巴值中,我想您已经拥有它并且没有显示它。 (我通常也会扔进-L,因此,如果凭据错误,它不会卡住。)其他set命令命令删除列标题和“”选择了1行。”信息。

尽管总体上是更长的单个命令行,但它具有不操纵/操纵您可能会收到的任何错误输出的优势。

或作为Heredoc:

sqlplus -l -s user/pass@dbase <<EOF
set linesize 3200
set pagesize 0
set feedback off
select myquery ;
EOF

A heredoc (as shown in @Philippe's answer) is going to be easier to read and maintain, but if for some reason you really wanted to keep it in one line you could add the -e flag to echo and embed new lines:

echo -e "SET LINESIZE 3200 \n select myquery ;" | sqlplus user/pass@dbase | head -4 | tail -1

or use printf:

printf "set linesize 3200 \n select myquery ;\n" | sqlplus user/pass@dbase | head -4 | tail -1

(In both I've taken out the extra | from your original version)

You can extend this to avoid the need to head and tail the output:

printf "set linesize 3200 \n set pagesize 0 \n set feedback off \n select myquery ;\n" | sqlplus -s user/pass@dbase

or with the various set options combined:

printf "set linesize 3200 pagesize 0 feedback off \n select myquery ;\n" | sqlplus -s user/pass@dbase

The -s suppresses the banner; from the head/tail values you're using I imagine you already have that and just didn't show it. (I'd usually throw in -l as well, so it doesn't get stuck if the credentials are wrong.) The other set commands remove the column headings and "1 row selected." message.

While that's a longer single command line overall, it has the advantage of not manipulating/mangling any error output you might get.

Or as a heredoc:

sqlplus -l -s user/pass@dbase <<EOF
set linesize 3200
set pagesize 0
set feedback off
select myquery ;
EOF
以可爱出名 2025-01-29 17:34:50

您能尝试一下:

sqlplus user/pass@dbase << "EOF" |...| head -4 | tail -1
SET LINESIZE 3200;
select myquery ;
EOF

Can you try this :

sqlplus user/pass@dbase << "EOF" |...| head -4 | tail -1
SET LINESIZE 3200;
select myquery ;
EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文