如何用pl/sql循环接受用户输入?
我希望能够根据用户输入将可变数量的行插入表中?例如。
Please enter value, enter "done" when no more values: value 1
Please enter value, enter "done" when no more values: value 2
Please enter value, enter "done" when no more values: done
2 Rows inserted successfully.
我不确定如何临时存储行,也不知道如何多次要求用户插入数据。 pl/sql 有数组吗?
谢谢
I want to be able to insert a variable number of rows into a table based on user input? eg.
Please enter value, enter "done" when no more values: value 1
Please enter value, enter "done" when no more values: value 2
Please enter value, enter "done" when no more values: done
2 Rows inserted successfully.
I'm not sure how to store the rows temporarily and I'm not sure how to ask the user multiple times to insert data. Does pl/sql have arrays?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如其他人所说,单独的 PL/SQL 并不适合这项任务,您需要一个顶部的 UI 来与最终用户交互。但是,如果您确实需要在 SQL Plus 中执行此操作,则可以使用我在 这个问题。
您需要创建 2 个 SQL Plus 脚本:
1) 执行单个插入的脚本,此处称为 script_insert.sql:
2) 控制进程的脚本,此处称为 main.sql:
现在在 SQL Plus 中,您可以像这样运行它:
让我重申一下,这表明它是可以完成的,我不会声称它是实现需求的好方法 - 除非它只是 DBA 或开发人员使用的临时工具。我永远不会将 SQL Plus 作为 UI 提供给最终用户!
As others have said, PL/SQL alone is not suitable for this task, you need a UI on top to interact with the end user. However, if you have a real need to do this in SQL Plus, it is possible using the technique I described in this SO question.
You need to create 2 SQL Plus scripts:
1) A script to perform a single insert, here called script_insert.sql:
2) A script to control the process, here called main.sql:
Now in SQL Plus you can run it like this:
Let me reiterate that this demonstrates it can be done, I would not claim it to be a good way to implement the requirement - unless it is just an ad hoc tool to be used by a DBA or developer. I would never give an end user SQL Plus as a UI!
我认为你正在用螺丝刀敲钉子。
使用 Python 脚本、PHP 页面、Perl 脚本、Java 程序或任何其他可以访问 Oracle 的环境,您将获得更大的灵活性。
I think you're pounding a nail with a screwdriver.
You'd get far more flexibility using a Python script, a PHP page, a Perl script, a Java program, or any other environment that can access Oracle.
最有可能的是,您不能,至少在不编写某种客户端应用程序(不是
SQL*Plus
脚本)的情况下不能。PL/SQL
在服务器上运行,不接受用户输入。SQL*Plus
在客户端上运行,但它没有循环结构。你不能混合这两者,所以你不能有一个重复发生的 PL/SQL 循环提示用户输入并根据传入的值执行 INSERT。最常见的是,人们通过编写一个小前面来解决这个问题-end 使用您最喜欢的脚本语言编写的脚本,用于收集输入,然后发出适当的 INSERT 语句。
如果您真的非常想在 SQL*Plus 中完成任务,这可能是可能的,但相当痛苦。您基本上必须构建一个无限循环,并抛出一个错误来逃脱。例如,
定义一个脚本 a.sql (我碰巧将我的脚本存储在 c:\temp 中),
然后在
SQL*Plus
中,当然非常丑陋,但它对于“有效”的某些定义“有效” ”
Most likely, you can't, at least not without writing some sort of client application (not a
SQL*Plus
script).PL/SQL
runs on the server and has nothing to accept user input.SQL*Plus
runs on the client but it has no looping constructs. You can't mix the two, so you can't have a PL/SQL loop that happens repeatedly prompts the user for input and does an INSERT based on the values passed in.Most commonly, people get around this by writing a small front-end script in your favorite scripting language that gathers the input and then issues appropriate INSERT statements.
If you really, really want to accomplish the task in
SQL*Plus
, it is probably possible, but quite painful. You would basically have to construct an infinite loop that you threw an error to escape from. For exampleDefine a script a.sql (I happen to store mine in c:\temp)
And then in
SQL*Plus
Horribly ugly, of course, but it "works" for some definition of "works"
也许您可以让用户输入逗号分隔的值列表?
然后处理 plsql 块中的字符串。
Maybe you can have your user enter a comma seperated list of values?
Then process the string in your plsql block.
我推荐 Oracle Application Express。它是免费的,并且非常容易快速构建简单的应用程序。
当然,对于您极其简单的需求来说,这可能有点过分了,但很可能有人会回来说“我喜欢您的 SQL*Plus 小脚本 - 您可以为其添加更多功能吗”,然后在您意识到之前您就已经知道了正在用棚屋地基建造一座摩天大楼。
I'd recommend Oracle Application Express. It's free, and dead easy to build simple apps very quickly.
Sure, it may be overkill for your extremely simple requirement here, but chances are someone will come back and say "I loved your little SQL*Plus script - could you just add a few more features to it" and before you know it you're building a skyscraper with a shack foundation.