如何使用pl/sql循环接受用户输入?

how to loop accepting user input with pl/sql?(如何使用pl/sql循环接受用户输入?)
本文介绍了如何使用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.

我不确定如何临时存储行,也不确定如何多次要求用户插入数据.pl/sql 有数组吗?

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?

谢谢

推荐答案

正如其他人所说,单独的 PL/SQL 不适合此任务,您需要在顶部有一个 UI 来与最终用户进行交互.但是,如果您确实需要在 SQL Plus 中执行此操作,则可以使用我在 这个问题.

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.

您需要创建 2 个 SQL Plus 脚本:

You need to create 2 SQL Plus scripts:

1) 执行单个插入的脚本,这里称为 script_insert.sql:

1) A script to perform a single insert, here called script_insert.sql:

insert into t1 values ('&1.');
@main

2) 一个控制进程的脚本,这里称为 main.sql:

2) A script to control the process, here called main.sql:

accept selection prompt "Please enter value, enter 'done' when no more values: "

set term off verify off

column script new_value v_script

select case '&selection.'
       when 'done' then ''
       else '@script_insert &selection.'
       end as script
from dual;

set term on

@&v_script.

现在在 SQL Plus 中你可以像这样运行它:

Now in SQL Plus you can run it like this:

SQL> select * from t1;

no rows selected

SQL> @main
Please enter value, enter 'done' when no more values: 1
Please enter value, enter 'done' when no more values: 2
Please enter value, enter 'done' when no more values: 3
Please enter value, enter 'done' when no more values: done
SQL> select * from t1;

        N1
----------
         1
         2
         3

让我重申一下,这表明它可以做到,我不会声称它是实现需求的好方法 - 除非它只是一个供 DBA 或开发人员使用的临时工具.我永远不会给最终用户 SQL Plus 作为 UI!

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!

这篇关于如何使用pl/sql循环接受用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)