为什么我不能在动态 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:DDL 语句.会导致ORA-00904::无效标识符

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

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

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

  • 要理解为什么会发生这种情况,我们需要查看动态 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) || ')';
    

  • 示例 2:

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

  • 示例 3:

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

  • 相关文章