【发布时间】:2015-02-25 07:29:15
【问题描述】:
我正在尝试做这样的事情:
create function getData(r1 IN TABLE1%ROWTYPE,col1 string, valor OUT string)RETURN string AS
instruccion VARCHAR2(500);
valor VARCHAR(200);
BEGIN
valor := r1.col1;
return valor;
END getData;
将记录和列名作为参数并返回值的过程:
CREATE procedure p1 as
vData VARCHAR2(80);
v1 VARCHAR2(80);
vValue VARCHAR2(80);
inst VARCHAR2(500);
CURSOR vTable2 IS SELECT * FROM TABLE2;
CURSOR vTable1 IS SELECT * FROM TABLE1;
BEGIN
--A cursor for the table with the data
FOR d1 IN vTable1
LOOP
--A cursor for the table that store the name of the columns of the table 1
FOR v1 IN vTable2
LOOP
--get the table1 column name
vData := v1.table2Col1;
--calls the procedure that gives back the value of that record on the column name is stored in VData
inst := 'begin getData(:d1, :vData, :vValue ); end;';
EXECUTE IMMEDIATE inst USING in d1 , vData, OUT vValue;
DBMS_OUTPUT.PUT_LINE(vValue);
END LOOP;
END LOOP;
END p1;
所以总而言之,我只想有一个表,将表 1 的名称存储在表 2 中,因为我希望该过程与名称为表 1 的任何表一起使用,这段代码给了我错误“表达式必须是SQL types”用于动态块的参数,我理解它,但可以找到一种新的方法来做到这一点。我已经尝试了很多东西,但我是 Oracle 的新手,非常感谢您的帮助。
【问题讨论】:
-
你为什么使用
execute immediate?为什么不直接调用函数getData(d1,vData,vValue);? -
这里不需要动态SQL。看我的回答。
-
不清楚你想达到什么目标,但是看看DBMS_SQL。但在很多情况下,您不需要动态 SQL。
-
你能编译你的函数
getData吗? -
嗨,没有函数 getData 说必须声明组件 col1 我假设这是因为 col1 它不是列的名称,而是包含列名称的变量。 @Polppan
标签: oracle plsql record dynamic-programming rowtype