我正在尝试从plsql程序中调用teradata中定义的宏,通过在线搜索各种博客,我能够设计下面的进程来调用并从所有宏中获取值,这些宏只包含一个选择查询,但是如果我使用相同的技术从宏中获取可能包含多个查询的值,然后只获取第一个查询的结果。
宏(单个查询) -
replace macro a_macro1 (arg1 (char(9)), arg2 (char(8)), arg3 (char(8))) as
(select ret1 (char(5)), ret2 (char(3))
from table1 where arg1 = :arg1 and arg2 = :arg2 and arg3 = :arg3;);
宏(多个查询) -
replace macro a_macro2 (arg1 (char(9)), arg2 (char(5)), arg3 (char(8)), arg4
(char(8))) as (
select ret1, ret2, ret3, ret4, ret5, ret6, ret7
from table1 where table1.arg1 = :arg1 and table1.arg2 = :arg2
and table1.arg3 = :arg3 and table1.arg4 = :arg4;
select ret1, ret2 from table2 where ret2 = 'm' and
table2.arg1 = :arg1
and table2.arg3 = :arg3 and table2.arg4 = :arg4;
select ret1, ret2 from table3 where ret2 in ('a','z')
and table3.arg1 = :arg1
and table3.arg2 = :arg2 and table3.arg3 = :arg3 and table3.arg4 =
:arg4;
select ret1, ret2, ret3, ret4, ret5, ret6, ret7, ret8
from table4 where arg1 = :arg1 and arg2 = :arg2 and arg3 = :arg3
and arg4 = :arg4;);
使用PLSQL方法 -
c := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@TERA_DBLINK;
DBMS_HS_PASSTHROUGH.PARSE@TERA_DBLINK(c, 'EXEC a_macro1(?,?,?)');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,1,'val1');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,2,'val2');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,3,'val3');
num_rows := DBMS_HS_PASSTHROUGH.EXECUTE_NON_QUERY@TERA_DBLINK(c);
DBMS_OUTPUT.PUT_LINE(num_rows || ' Rows fetched');
LOOP
num_rows := DBMS_HS_PASSTHROUGH.FETCH_ROW@TERA_DBLINK(c);
EXIT WHEN num_rows = 0;
i := 1;
loop
BEGIN
exit when i = 99;
DBMS_HS_PASSTHROUGH.GET_VALUE@TERA_DBLINK(c, i, val1);
DBMS_OUTPUT.PUT_LINE(val1);
i := i+1;
EXCEPTION
WHEN OTHERS THEN
BEGIN
i:=99;
END;
END;
end loop;
END LOOP;
DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@TERA_DBLINK(c);
此方法适用于从第一个宏获取值。如何更改它以使其适用于第二个宏?
另外,有没有办法获取列名?
GET_VALUE
仅提取值,而不是列名。
编辑1 - 这是我调用第二个宏的代码 -
set serveroutput on;
DECLARE
num_rows INTEGER;
c INTEGER;
i INTEGER;
val1 VARCHAR2(100);
BEGIN
c := DBMS_HS_PASSTHROUGH.OPEN_CURSOR@TERA_DBLINK;
DBMS_OUTPUT.PUT_LINE('Cursor opened');
DBMS_HS_PASSTHROUGH.PARSE@TERA_DBLINK(c, 'EXEC a_macro2 (?,?,?,?)');
DBMS_OUTPUT.PUT_LINE('command set for parse');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,1,'123456789');
DBMS_OUTPUT.PUT_LINE('first variable bound');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,2,'00001');
DBMS_OUTPUT.PUT_LINE('second variable bound');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,3,'20161215');
DBMS_OUTPUT.PUT_LINE('third variable bound');
DBMS_HS_PASSTHROUGH.BIND_VARIABLE@TERA_DBLINK(c,4,'15462250');
DBMS_OUTPUT.PUT_LINE('fourth variable bound');
num_rows := DBMS_HS_PASSTHROUGH.EXECUTE_NON_QUERY@TERA_DBLINK(c);
DBMS_OUTPUT.PUT_LINE(num_rows || ' Rows fetched');
LOOP
num_rows := DBMS_HS_PASSTHROUGH.FETCH_ROW@TERA_DBLINK(c);
EXIT WHEN num_rows = 0;
i := 1;
loop
BEGIN
exit when i = 99;
DBMS_HS_PASSTHROUGH.GET_VALUE@TERA_DBLINK(c, i, val1);
DBMS_OUTPUT.PUT_LINE(val1);
i := i+1;
EXCEPTION
WHEN OTHERS THEN
BEGIN
i:=99;
END;
END;
end loop;
END LOOP;
DBMS_HS_PASSTHROUGH.CLOSE_CURSOR@TERA_DBLINK(c);
END;
/
输出 -
Cursor opened
command set for parse
first variable bound
second variable bound
third variable bound
fourth variable bound
1 Rows fetched
ABCDEF02AB8
ABCDEF02ABC
1234
T1F
1F
A
Y
仅返回7个值,这些值来自第二个宏的第一个查询。
答案 0 :(得分:0)
要使代码适用于下一个宏,只需在此步骤进行更改:
DBMS_HS_PASSTHROUGH.PARSE@TERA_DBLINK(c, 'EXEC a_macro2(?,?,?,?,?)');
不要忘记添加bind varaibles作为传递的参数数量。