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

发布于 2024-08-14 05:08:30 字数 335 浏览 9 评论 0原文

我希望能够根据用户输入将可变数量的行插入表中?例如。

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 技术交流群。

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

发布评论

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

评论(5

甜心 2024-08-21 05:08:30

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

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

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

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

2) 控制进程的脚本,此处称为 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 中,您可以像这样运行它:

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 提供给最终用户

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:

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

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.

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

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!

左耳近心 2024-08-21 05:08:30

我认为你正在用螺丝刀敲钉子。

使用 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.

梦萦几度 2024-08-21 05:08:30

最有可能的是,您不能,至少在不编写某种客户端应用程序(不是 SQL*Plus 脚本)的情况下不能。 PL/SQL 在服务器上运行,不接受用户输入。 SQL*Plus 在客户端上运行,但它没有循环结构。你不能混合这两者,所以你不能有一个重复发生的 PL/SQL 循环提示用户输入并根据传入的值执行 INSERT。

最常见的是,人们通过编写一个小前面来解决这个问题-end 使用您最喜欢的脚本语言编写的脚本,用于收集输入,然后发出适当的 INSERT 语句。

如果您真的非常想在 SQL*Plus 中完成任务,这可能是可能的,但相当痛苦。您基本上必须构建一个无限循环,并抛出一个错误来逃脱。例如,

定义一个脚本 a.sql (我碰巧将我的脚本存储在 c:\temp 中),

whenever sqlerror exit;
accept x_val number prompt "Enter a value for x or -1 to stop  ";
INSERT INTO x( col1 ) values( :x_val );
BEGIN
    IF( &x_val = -1 )
    THEN
      commit;
      RAISE_APPLICATION_ERROR( -20001, 'Done' );
    END IF;
END;
/

@c:\temp\a.sql

然后在 SQL*Plus 中,

SQL> variable x_val number;
SQL> crate table x( col1 number );
SQL> truncate table x;

Table truncated.

SQL> @c:\temp\a.sql
Enter a value for x or -1 to stop  3

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(          3 = -1 )

PL/SQL procedure successfully completed.

Enter a value for x or -1 to stop  4

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(          4 = -1 )

PL/SQL procedure successfully completed.

Enter a value for x or -1 to stop  5

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(          5 = -1 )

PL/SQL procedure successfully completed.

Enter a value for x or -1 to stop  -1

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(         -1 = -1 )
BEGIN
*
ERROR at line 1:
ORA-20001: Done
ORA-06512: at line 4

当然非常丑陋,但它对于“有效”的某些定义“有效” ”

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 example

Define a script a.sql (I happen to store mine in c:\temp)

whenever sqlerror exit;
accept x_val number prompt "Enter a value for x or -1 to stop  ";
INSERT INTO x( col1 ) values( :x_val );
BEGIN
    IF( &x_val = -1 )
    THEN
      commit;
      RAISE_APPLICATION_ERROR( -20001, 'Done' );
    END IF;
END;
/

@c:\temp\a.sql

And then in SQL*Plus

SQL> variable x_val number;
SQL> crate table x( col1 number );
SQL> truncate table x;

Table truncated.

SQL> @c:\temp\a.sql
Enter a value for x or -1 to stop  3

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(          3 = -1 )

PL/SQL procedure successfully completed.

Enter a value for x or -1 to stop  4

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(          4 = -1 )

PL/SQL procedure successfully completed.

Enter a value for x or -1 to stop  5

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(          5 = -1 )

PL/SQL procedure successfully completed.

Enter a value for x or -1 to stop  -1

1 row created.

old   2:        IF( &x_val = -1 )
new   2:        IF(         -1 = -1 )
BEGIN
*
ERROR at line 1:
ORA-20001: Done
ORA-06512: at line 4

Horribly ugly, of course, but it "works" for some definition of "works"

不气馁 2024-08-21 05:08:30

也许您可以让用户输入逗号分隔的值列表?

请输入值:1,2,3,4,5

然后处理 plsql 块中的字符串。

Maybe you can have your user enter a comma seperated list of values?

Please enter values: 1,2,3,4,5

Then process the string in your plsql block.

慕烟庭风 2024-08-21 05:08:30

我推荐 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文