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

      <bdo id='o1iz4'></bdo><ul id='o1iz4'></ul>
    1. <tfoot id='o1iz4'></tfoot>
      1. <legend id='o1iz4'><style id='o1iz4'><dir id='o1iz4'><q id='o1iz4'></q></dir></style></legend>

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

        FOR r in (SELECT ... INTO ...)

        FOR r in (SELECT ... INTO ...)(FOR r in (SELECT ... INTO ...))
      2. <tfoot id='nFePC'></tfoot><legend id='nFePC'><style id='nFePC'><dir id='nFePC'><q id='nFePC'></q></dir></style></legend>

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

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

              • <bdo id='nFePC'></bdo><ul id='nFePC'></ul>
                    <tbody id='nFePC'></tbody>
                1. 本文介绍了FOR r in (SELECT ... INTO ...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  今天,我遇到了一段我认为不应该编译的有趣代码.它在 FOR r IN ... LOOP 中使用了 SELECT ... INTO 子句.这是在 Oracle 11i 上编译的脚本.该脚本是实际 PL/SQL 代码的简化版本,编译在一个包中,在生产环境中运行.

                  Today, I came across a funny piece of code that I think should not compile. It uses an SELECT ... INTO clause within a FOR r IN ... LOOP. Here's a script that compiles on Oracle 11i. The script is a shortened version of actual PL/SQL code compiled in a package, runing in production.

                  create table tq84_foo (
                     i number,
                     t varchar2(10)
                  );
                  
                  insert into tq84_foo values (1, 'abc');
                  insert into tq84_foo values (2, 'def');
                  
                  declare
                  
                    rec tq84_foo%rowtype;
                  
                  begin
                  
                    for r in (
                       select     i,     t 
                         into rec.i, rec.t -- Hmm???
                         from tq84_foo
                    )
                    loop
                  
                      dbms_output.put_line('rec: i= ' || rec.i || ', t=' || rec.t);
                  
                    end loop;
                  
                  end;
                  /
                  
                  
                  drop table tq84_foo purge;
                  

                  运行时的输出是:

                  rec: i= , t=
                  rec: i= , t=
                  

                  相信 1) 我可以安全地删除 select 语句的 INTO 部分和 2) 该构造应该无效或至少表现出未定义的行为.

                  I believe 1) I can safely remove the INTO part of the select statement and 2) that this construct should either be invalid or exhibits at least undefined behaviour.

                  我的两个假设是否正确?

                  Are my two assumptions right?

                  推荐答案

                  您的假设部分正确:

                  1) 是的,您可以安全地删除 SELECT 语句的 INTO 部分.但是您应该将循环中的行更改为这种格式:

                  1) Yes, you can safely remove the INTO part of the SELECT statement. But you should change the line in the loop to this format:

                  dbms_output.put_line('rec: i= ' || r.i || ', t=' || r.t);
                  

                  这样它就会从 r 变量中取出数据

                  That way it will get the data out of the r variable

                  2) 此代码的问题在于,如果查询返回多于一行,SELECT ... INTO 的语法应该会失败.如果它没有失败,那么它可能是一个错误并且会出现意外行为.

                  2) The problem with this code is that the syntax of the SELECT ... INTO should fail if the query return more than one row. If it does not fail so it might be a bug and will have unexpected behaviour.

                  这篇关于FOR r in (SELECT ... INTO ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to redirect the output of DBMS_OUTPUT.PUT_LINE to a file?(如何将 DBMS_OUTPUT.PUT_LINE 的输出重定向到文件?)
                  How do I get column datatype in Oracle with PL-SQL with low privileges?(如何使用低权限的 PL-SQL 在 Oracle 中获取列数据类型?)
                  Get a list of all functions and procedures in an Oracle database(获取 Oracle 数据库中所有函数和过程的列表)
                  Why cannot I create triggers on objects owned by SYS?(为什么我不能在 SYS 拥有的对象上创建触发器?)
                  Returning result even for elements in IN list that don#39;t exist in table(即使对于表中不存在的 IN 列表中的元素也返回结果)
                  Reset Sequence in oracle 11g(oracle 11g 中的重置序列)

                  1. <small id='YGzpw'></small><noframes id='YGzpw'>

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

                              <tbody id='YGzpw'></tbody>
                            <legend id='YGzpw'><style id='YGzpw'><dir id='YGzpw'><q id='YGzpw'></q></dir></style></legend>