读取SAP Hana中的表类型输出
如何在SAP Hana SQL脚本中读取过程的表外类型参数的内容 示例程序:读取SAP Hana中的表类型输出,sap,procedure,hana,Sap,Procedure,Hana,如何在SAP Hana SQL脚本中读取过程的表外类型参数的内容 示例程序: create procedure "MYSCHEMA".ReturnTypeTest(out OUTPUT_TABLE "MYSCHEMA"."RESOUT") as begin create local temporary table #temp ("COL1" bigint, "COL2" bigint, "COL3" bigint); insert into #temp values(1, 2,
create procedure "MYSCHEMA".ReturnTypeTest(out OUTPUT_TABLE "MYSCHEMA"."RESOUT")
as
begin
create local temporary table #temp ("COL1" bigint, "COL2" bigint, "COL3" bigint);
insert into #temp values(1, 2, 3);
insert into #temp values(4, 5, 6);
insert into #temp values(7, 8, 9);
OUTPUT_TABLE = select * from #temp;
drop table #temp;
end;
create type "MYSCHEMA"."RESOUT" as table ("COL1" bigint, "COL2" bigint, "COL3" bigint);
表格类型(输出参数):
create procedure "MYSCHEMA".ReturnTypeTest(out OUTPUT_TABLE "MYSCHEMA"."RESOUT")
as
begin
create local temporary table #temp ("COL1" bigint, "COL2" bigint, "COL3" bigint);
insert into #temp values(1, 2, 3);
insert into #temp values(4, 5, 6);
insert into #temp values(7, 8, 9);
OUTPUT_TABLE = select * from #temp;
drop table #temp;
end;
create type "MYSCHEMA"."RESOUT" as table ("COL1" bigint, "COL2" bigint, "COL3" bigint);
当我按如下方式调用该过程时,它会在SAP Hana Studio的结果窗格中显示整个内容,但如何以编程方式获取它
call "MYSCHEMA"."RETURNTYPETEST"(?);
过程的输出变量只能分配给SQLScript上下文中的变量。
默认结果集是一个例外,它绑定到过程中执行的最后一个SELECT命令。
如果您的目的是生成可以选择的内容,则可能需要使用表类型的用户定义函数(TUDF) 对示例代码的两条注释:
DECLARE uaccounts TABLE (USERID bigint, USERNAME NVARCHAR(256), CREATED date);
DECLARE expdate date := current_date;
/* In this example the procedure 'get_expired_useraccounts_by date' has got
the IN parameter expiry_date (date) and
the OUT parameter expired_accounts (table structure).
By assigning the variable uaccounts to the OUT parameter, the result set
automatically gets bound to uaccounts.*/
call get_expired_useraccounts_by_date (:expdate, :uaccounts);
/* from here you can use :uaccounts like a table variable*/
SELECT count(*) FROM :uaccounts;
当然,所有这些都是参考文档和开发人员指南的一部分。您可以在系统视图中查询元数据 请检查以下SQLScript选择
select table_type_schema, table_type_name, *
from PROCEDURE_PARAMETERS
where
schema_name = UPPER('MYSCHEMA') and
procedure_name = UPPER('ReturnTypeTest') and
parameter_name = UPPER('OUTPUT_TABLE')
我希望它有帮助,你能检查一下下面的SQLScript吗
declare lt_list "MYSCHEMA"."RESOUT";
call "MYSCHEMA"."RETURNTYPETEST"(lt_list);
select * from :lt_list;
这应该使用最后一条SELECT语句显示输出参数表。在理解了Lars'Q&A的上下文后,答案是:在调用者过程代码中定义一个表变量 声明临时表(n int); 声明临时我的表格类型 然后将被调用方的输出参数分配给它
感谢您的回复,但由于SAP Hana中的函数是只读的,因此无法在我们的情况下使用。除了TUDF,我们还有其他选项吗?好的,那么您需要一个过程来更改数据并返回可以从纯SQL读取的结果集?不,我看不出有什么办法。对于您的示例,您可以简单地运行该过程并从temp中选择修改后的数据。表-数据的寿命与会话的寿命一样长,因此您可能会滥用它作为移交数据的一种方式。但要意识到,这是一个相当糟糕的设计,您可能想要执行的大多数数据操作都可以在不写入/更新表中数据的情况下完成。您在这里选择的方法是性能最差的方法。不,我不是在寻找从纯SQL读取输出参数的方法。我需要在另一个过程的主体中调用这个过程,并处理它返回的内容。但我不知道如何处理表类型的输出。如果它是一个标量输出参数,我可以简单地为它声明一个变量,但我无法确定表类型输出参数使用的是什么数据结构。谢谢,它返回过程参数,但我如何查询/读取输出参数(表类型)的内容?这个答案没有解决OP的问题。