简体   繁体   English

PL / SQL动态循环值

[英]PL/SQL Dynamic Loop Value

My goal is to keep a table which contains bind values and arguments, which will later be used by dbms_sql. 我的目标是保留一个包含绑定值和参数的表,稍后将由dbms_sql使用。 The below pl/sql example is basic, it's purpose is to illustrate the issue I am having with recalling values from prior loop objects. 下面的pl / sql示例是基本的,它的目的是说明我从调用先前循环对象的值时遇到的问题。

The table account_table holds acccount information account_table包含帐户信息

CREATE TABLE account_table (account number, name varchar2(100)));

INSERT INTO mytest 
    (account, name) 
VALUES 
    (1 ,'Test');
COMMIT;

The table MYTEST holds bind information MYTEST保存绑定信息

CREATE TABLE mytest (bind_value varchar2(100));

INSERT INTO mytest (bind_value) VALUES ('i.account');
COMMIT;



DECLARE
  v_sql VARCHAR2(4000) := NULL;
  v_ret VARCHAR2(4000) := NULL;
BEGIN
  FOR I IN (
    SELECT account
    FROM account_table
    WHERE ROWNUM = 1
  ) LOOP

    FOR REC IN (
      SELECT *
      FROM mytest
    ) LOOP

      v_sql := 'SELECT ' || rec.bind_value || ' FROM dual';
      EXECUTE IMMEDIATE v_sql INTO v_ret;

      dbms_output.put_line ('Account: ' || v_ret);
    END LOOP;

  END LOOP;
END;
/

I cannot store the name i.account and later use the value that object. 我无法存储名称i.account以后使用该对象的值。 My idea was to use NDS; 我的想法是使用NDS; but, while the value of v_sql looks ok (it will read "Select i.account from dual"), an exception of invalid identifier would be raised on i.account . 但是,虽然v_sql的值看起来没问题(它将显示为“从双重选择i.account”),但会在i.account上引发无效标识符的异常。 Is there a way to get the value of the object? 有没有办法获得对象的价值? We are using Oracle 11g2. 我们使用的是Oracle 11g2。 Thanks! 谢谢!

Dynamic SQL will not have the context of your PL/SQL block. 动态SQL不具有PL / SQL块的上下文。 You cannot use identifiers that are defined in the PL/SQL block in your dynamic SQL statement. 您不能在动态SQL语句中使用PL / SQL块中定义的标识符。 So you cannot reference i.account and reference the loop that you have defined outside of the dynamic SQL statement. 因此,您无法引用i.account并引用您在动态SQL语句之外定义的循环。 You could, of course, dynamically generate the entire PL/SQL block but dynamic PL/SQL is generally a pretty terrible approach-- it is very hard to get that sort of thing right. 当然,您可以动态生成整个PL / SQL块,但动态PL / SQL通常是一种非常糟糕的方法 - 很难做到这一点。

If you are trying to use the value of i.account , however, you can do something like 但是,如果您尝试使用i.account的值,则可以执行类似的操作

v_sql := 'SELECT :1 FROM dual';
EXECUTE IMMEDIATE v_sql
   INTO v_ret
  USING i.account;

That doesn't appear to help you, though, if you want to get the string i.account from a table. 但是,如果您想从表中获取字符串i.account ,这似乎对您没有帮助。

Are you always trying to access the same table with the same where clause, but just looking for a different column each time? 您是否总是尝试使用相同的where子句访问同一个表,但每次只查找不同的列? If so, you could do this: 如果是这样,你可以这样做:

p_what_I_want := 'ACCOUNT';
--
SELECT decode(p_what_I_want
               ,'ACCOUNT', i.account
               , 'OTHER_THING_1', i.other_thing_1
               , 'OTHER_THING_2', i.other_thing_2
               , 'OTHER_THING_1', i.default_thing) out_thing
INTO l_thing
FROM table;

The above statement is not dynamic but returns a different column on demand... 上述声明不是动态的,但会根据需要返回不同的列...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM