<legend id='tGlzb'><style id='tGlzb'><dir id='tGlzb'><q id='tGlzb'></q></dir></style></legend>
      <i id='tGlzb'><tr id='tGlzb'><dt id='tGlzb'><q id='tGlzb'><span id='tGlzb'><b id='tGlzb'><form id='tGlzb'><ins id='tGlzb'></ins><ul id='tGlzb'></ul><sub id='tGlzb'></sub></form><legend id='tGlzb'></legend><bdo id='tGlzb'><pre id='tGlzb'><center id='tGlzb'></center></pre></bdo></b><th id='tGlzb'></th></span></q></dt></tr></i><div id='tGlzb'><tfoot id='tGlzb'></tfoot><dl id='tGlzb'><fieldset id='tGlzb'></fieldset></dl></div>

        • <bdo id='tGlzb'></bdo><ul id='tGlzb'></ul>
      1. <tfoot id='tGlzb'></tfoot>

        <small id='tGlzb'></small><noframes id='tGlzb'>

        如何在 PL/SQL 中使用循环多次运行相同的查询?

        时间:2023-11-03
      2. <tfoot id='ZI8Xx'></tfoot>
          <tbody id='ZI8Xx'></tbody>
          <legend id='ZI8Xx'><style id='ZI8Xx'><dir id='ZI8Xx'><q id='ZI8Xx'></q></dir></style></legend>

            <i id='ZI8Xx'><tr id='ZI8Xx'><dt id='ZI8Xx'><q id='ZI8Xx'><span id='ZI8Xx'><b id='ZI8Xx'><form id='ZI8Xx'><ins id='ZI8Xx'></ins><ul id='ZI8Xx'></ul><sub id='ZI8Xx'></sub></form><legend id='ZI8Xx'></legend><bdo id='ZI8Xx'><pre id='ZI8Xx'><center id='ZI8Xx'></center></pre></bdo></b><th id='ZI8Xx'></th></span></q></dt></tr></i><div id='ZI8Xx'><tfoot id='ZI8Xx'></tfoot><dl id='ZI8Xx'><fieldset id='ZI8Xx'></fieldset></dl></div>
                • <bdo id='ZI8Xx'></bdo><ul id='ZI8Xx'></ul>
                • <small id='ZI8Xx'></small><noframes id='ZI8Xx'>

                  本文介绍了如何在 PL/SQL 中使用循环多次运行相同的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用此代码我无法多次运行插入查询;它只要求 ID 和名称一次(如果计数器中的值 > 1 ).

                  Using this code I'm not able to run an insert query multiple times; it asks for ID and name only one time ( if value in counter is > than 1 ).

                  declare
                          counter number := 0 ;
                  begin 
                          counter := &counter ;
                          while counter > 0 loop
                                 insert into customer values ( &id, '&name' ) ;
                                 counter := counter - 1 ;
                         end loop ;
                  end ;   
                  

                  让我用一个例子来解释:-

                  Let me explain with the help of an example :-

                  假设我将值 2 放入 counter.有了这个,它应该两次询问我的 ID 和名称,但它只询问一次,并将我为 ID 和名称输入的值复制到表 test 中两次.

                  Say I put value 2 in counter. With this it should ask me for ID and name twice but it only asks once, and it copies the values I entered for the ID and name into table test twice.

                  我该如何解决这个问题?如果我不能,那么请建议可以解决我的问题的替代代码.

                  How can I fix this? If I can't then please suggest alternative code which will fix my problem.

                  推荐答案

                  替换变量 &counter&id&name 每个都被评估一次,当 PL/SQL 块被编译时 - 不是,因为它正在执行.

                  The substitution variables &counter, &id and &name are each evaluated once, when the PL/SQL block is compiled - not as it is being executed.

                  变量不会也不能在 PL/SQL 块内重新评估或重新提升.该块作为数据库中的单个单元执行 - 一旦它被提交执行,它就独立于客户端,客户端只等待它完成(除非你中断它,客户端也会处理).PL/SQL 不是交互式语言,您不应将客户端功能(例如替换变量)与 SQL 或 PL/SQL 功能混淆.

                  The variables are not, and cannot be, re-evaluated or re-promoted within the PL/SQL block. The block is executed as a single unit within the database - once it has been submitted for execution it is independent of the client, which just waits for it to complete (unless you interrupt it, which the client also handles). PL/SQL is not an interactive language, and you shouldn't confuse client functionality (e.g. substitution variables) with SQL or PL/SQL functionality.

                  只是为了好玩,您可以基于 counter 生成一个脚本,该脚本对 ID 和名称进行适当数量的提示,并将它们转换为可以通过简单插入使用的格式:

                  Just for fun, you could generate a script based on counter which does the appropriate number of prompts for IDs and names, and gets them into a format that could be used by a simple insert:

                  set serveroutput on
                  set feedback off
                  set echo off
                  set verify off
                  set termout off
                  
                  accept counter "How many value pairs do you want to insert?"
                  
                  var ids varchar2(4000);
                  var names varchar2(4000);
                  
                  spool /tmp/prompter.sql
                  
                  begin
                    -- prompt for all the value pairs
                    for i in 1..&counter loop
                      dbms_output.put_line('accept id' ||i|| ' number  "Enter ID ' ||i|| '"');
                      dbms_output.put_line('accept name' ||i|| '  char "Enter name ' ||i|| '"');
                    end loop;
                  
                    -- concatenate the IDs into one variable
                    dbms_output.put('define ids="');
                    for i in 1..&counter loop
                      if i > 1 then
                        dbms_output.put(',');
                      end if;
                      dbms_output.put('&'||'id'||i);
                    end loop;
                    dbms_output.put_line('"');
                  
                    -- concatenate the names into one variable
                    dbms_output.put('define names="');
                    for i in 1..&counter loop
                      if i > 1 then
                        dbms_output.put(',');
                      end if;
                      -- each name wrapped in single quotes
                      dbms_output.put(q'['&]'||'name'||i||q'[']');
                    end loop;
                    dbms_output.put_line('"');
                  end;
                  /
                  spool off
                  
                  @/tmp/prompter
                  
                  insert into customer (id, name)
                  select i.id, n.name
                  from (
                    select rownum as rid, column_value as id 
                    from table(sys.odcinumberlist(&ids))
                  ) i
                  join (
                    select rownum as rid, column_value as name
                    from table(sys.odcivarchar2list(&names))
                  ) n
                  on n.rid = i.rid;
                  
                  select * from customer;
                  

                  这会创建一个名为 prompter.sql 的文件(我已经把它放在/tmp 中;把它放在适合你环境的地方!);'值对的数量'提示回答为 2,临时脚本看起来包含:

                  That creates a file called prompter.sql (I've put it in /tmp; put it somewhere suitable for your environment!); with the 'number of value pairs' prompt answered as 2 that temporary script would look contain:

                  accept id1 number  "Enter ID 1"
                  accept name1  char "Enter name 1"
                  accept id2 number  "Enter ID 2"
                  accept name2  char "Enter name 2"
                  define ids="&id1,&id2"
                  define names="'&name1','&name2'"
                  

                  然后使用 @ 运行该临时脚本,提示用户输入所有这些单独的值.然后在插入使用的选择中使用由组合替换变量构建的表集合.

                  That temporary script is then run with @, prompting the user for all those individual values. And then table collections built from the combined substitution variables are used in a select, which is used by the insert.

                  这篇关于如何在 PL/SQL 中使用循环多次运行相同的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:以 KB 为单位确定 SQL 结果集的大小 下一篇:Oracle - ORA-06502: PL/SQL: 数字或值错误 (DBMS_OUTPUT)

                  相关文章

                    <legend id='5c9vt'><style id='5c9vt'><dir id='5c9vt'><q id='5c9vt'></q></dir></style></legend>
                  1. <tfoot id='5c9vt'></tfoot>

                    <small id='5c9vt'></small><noframes id='5c9vt'>

                      <i id='5c9vt'><tr id='5c9vt'><dt id='5c9vt'><q id='5c9vt'><span id='5c9vt'><b id='5c9vt'><form id='5c9vt'><ins id='5c9vt'></ins><ul id='5c9vt'></ul><sub id='5c9vt'></sub></form><legend id='5c9vt'></legend><bdo id='5c9vt'><pre id='5c9vt'><center id='5c9vt'></center></pre></bdo></b><th id='5c9vt'></th></span></q></dt></tr></i><div id='5c9vt'><tfoot id='5c9vt'></tfoot><dl id='5c9vt'><fieldset id='5c9vt'></fieldset></dl></div>
                        <bdo id='5c9vt'></bdo><ul id='5c9vt'></ul>