<tfoot id='s684l'></tfoot>

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

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

    2. <legend id='s684l'><style id='s684l'><dir id='s684l'><q id='s684l'></q></dir></style></legend>
          <bdo id='s684l'></bdo><ul id='s684l'></ul>

        为什么我不能在动态 SQL 的 DDL/SCL 语句中使用绑定变量?

        时间:2023-09-19

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

          <bdo id='Whk50'></bdo><ul id='Whk50'></ul>

          <legend id='Whk50'><style id='Whk50'><dir id='Whk50'><q id='Whk50'></q></dir></style></legend>

                  <tbody id='Whk50'></tbody>

                <i id='Whk50'><tr id='Whk50'><dt id='Whk50'><q id='Whk50'><span id='Whk50'><b id='Whk50'><form id='Whk50'><ins id='Whk50'></ins><ul id='Whk50'></ul><sub id='Whk50'></sub></form><legend id='Whk50'></legend><bdo id='Whk50'><pre id='Whk50'><center id='Whk50'></center></pre></bdo></b><th id='Whk50'></th></span></q></dt></tr></i><div id='Whk50'><tfoot id='Whk50'></tfoot><dl id='Whk50'><fieldset id='Whk50'></fieldset></dl></div>
              • <tfoot id='Whk50'></tfoot>
                1. 本文介绍了为什么我不能在动态 SQL 的 DDL/SCL 语句中使用绑定变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用绑定变量在动态 SQL 中执行 SQL 命令:

                  I am trying to execute an SQL command within dynamic SQL with bind variables:

                  -- this procedure is a part of PL/SQL package Test_Pkg
                  PROCEDURE Set_Nls_Calendar(calendar_ IN VARCHAR2)
                  IS
                  BEGIN
                     EXECUTE IMMEDIATE
                        'ALTER SESSION
                        SET NLS_CALENDAR = :cal'
                        USING IN calendar_;
                  END Set_Nls_Calendar;
                  

                  然后在客户端,我试图调用该过程:

                  Then on the client side, I am trying to invoke the procedure:

                  Test_Pkg.Set_Nls_Calendar('Thai Buddha');
                  

                  但这让我知道ORA-02248:ALTER SESSION 的无效选项.

                  我的问题是:为什么我不能在动态 SQL 的 DDL/SCL 语句中使用绑定变量?

                  And my question is: Why cannot I use bind variables in DDL/SCL statements in dynamic SQL?

                  推荐答案

                  DDL 语句中不允许绑定变量.所以下面的语句会导致错误:

                  Bind variables are not allowed in DDL statements. So following statements will cause errors:

                  • 示例 1:DDL 语句.会导致ORA-01027:数据定义操作不允许绑定变量

                  EXECUTE IMMEDIATE
                    'CREATE TABLE dummy_table ( dummy_column NUMBER DEFAULT :def_val )'
                    USING 42;
                  

                2. 示例 2:DDL 语句.会导致ORA-00904::无效标识符

                  EXECUTE IMMEDIATE
                    'CREATE TABLE dummy_table ( :col_name NUMBER )'
                    USING var_col_name;
                  

                3. 示例 3:SCL 语句.会导致 ORA-02248: ALTER SESSION 的无效选项

                  EXECUTE IMMEDIATE
                    'ALTER SESSION SET NLS_CALENDAR = :cal'
                    USING var_calendar_option;
                  

                4. 要理解为什么会发生这种情况,我们需要查看动态 SQL 语句如何已处理.

                  To understand why this happens, we need to look at How Dynamic SQL Statements Are Processed.

                  通常,应用程序会提示用户输入 SQL 语句的文本以及语句中使用的宿主变量的值.然后Oracle 解析SQL 语句.也就是说,Oracle 检查 SQL 语句以确保它遵循语法规则并且 引用有效的数据库对象.解析还包括检查数据库访问权限1,保留所需资源,并找到最佳访问路径.

                  Typically, an application program prompts the user for the text of a SQL statement and the values of host variables used in the statement. Then Oracle parses the SQL statement. That is, Oracle examines the SQL statement to make sure it follows syntax rules and refers to valid database objects. Parsing also involves checking database access rights1, reserving needed resources, and finding the optimal access path.

                  1 回答者加的重点

                  注意解析步骤发生在之前将任何变量绑定到动态语句.如果你检查上面的四个例子,你会发现解析器在不知道绑定变量的值的情况下无法保证这些动态 SQL 语句的语法有效性.

                  Note that parsing step happens before binding any variables to the dynamic statement. If you examine the above four examples, you will realize that there is no way for the parser to guarantee the syntactical validity of these dynamic SQL statements without knowing the values for bind variables.

                  • 示例 #1:解析器无法判断绑定值是否有效.如果程序员写的是 USING '42' 而不是 USING 42 会怎样?
                  • 示例 #2:解析器无法判断 :col_name 是否是有效的列名.如果绑定的列名称是 'identifier_that_well_exceeds_thirty_character_identifier_limit' 会怎样?
                  • 示例 #3:NLS_CALENDAR 的值内置于常量中(对于给定的 Oracle 版本?).解析器无法判断绑定变量是否具有有效值.
                  • Example #1: Parser cannot tell if the bind value will be valid. What if instead of USING 42, programmer wrote USING 'forty-two'?
                  • Example #2: Parser cannot tell if :col_name would be a valid column name. What if the bound column name was 'identifier_that_well_exceeds_thirty_character_identifier_limit'?
                  • Example #3: Values for NLS_CALENDAR are built in constants (for a given Oracle version?). Parser cannot tell if the bound variable will have a valid value.

                  所以答案是你不能在动态 SQL 中绑定表名、列名等模式元素.也不能绑定内置常量.

                  实现动态引用架构元素/常量的唯一方法是在动态 SQL 语句中使用字符串连接.

                  The only way to achieve referencing schema elements/constants dynamically is to use string concatenation in dynamic SQL statements.

                  • 示例 1:

                  EXECUTE IMMEDIATE
                    'CREATE TABLE dummy_table ( dummy_column NUMBER DEFAULT ' || to_char(42) || ')';
                  

                5. 示例 2:

                  EXECUTE IMMEDIATE
                    'CREATE TABLE dummy_table (' || var_col_name || ' NUMBER )';
                  

                6. 示例 3:

                  EXECUTE IMMEDIATE
                    'ALTER SESSION SET NLS_CALENDAR = ''' || var_calendar_option || '''';
                  

                7. 这篇关于为什么我不能在动态 SQL 的 DDL/SCL 语句中使用绑定变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将时间戳数据类型转换为 unix 时间戳 Oracle 下一篇:为什么我似乎不能强制 Oracle 11g 为单个 SQL 查询消耗更多 CPU

                  相关文章

                  • <bdo id='OupH9'></bdo><ul id='OupH9'></ul>

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

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