简体   繁体   English

如何在 SAP HANA 的动态 sql 中使用表变量?

[英]How to use table variable in dynamic sql in SAP HANA?

Inside a procedure, I use a table variable to store the some result from table A.在一个过程中,我使用一个表变量来存储表 A 中的一些结果。

I have to use this table variable in dynamic sql.我必须在动态 sql 中使用这个表变量。

Eg)例如)

Call Procedure A()

Begin

VAR_TAB = select customer_id from table_A;

EXEC ' select * from'||: var_tab;

End;

The above code will throw error.上面的代码会抛出错误。 Anyone has a solution for this?有人对此有解决方案吗?

Depends on your use case:取决于您的用例:

You can use the table variable like a temporary table and can use it w/o dynamic sql:您可以像使用临时表一样使用表变量,并且可以在不使用动态 sql 的情况下使用它:

CREATE PROCEDURE c AS 
BEGIN
    var_tab = SELECT customer_id FROM table_A ;
    select * from :var_tab;
END;

If the result contains the actual table name, you have to use dynamic SQL and can access the table variable value as array :如果结果包含实际表名,则必须使用动态 SQL 并且可以将表变量值作为数组访问:

CREATE PROCEDURE b AS 
BEGIN
    var_tab = SELECT customer_id FROM table_A ;
    EXECUTE IMMEDIATE ' select * from '||:var_tab.customer_id[1];
END;

or use a scalar variable :或使用标量变量

CREATE PROCEDURE a AS 
BEGIN
    DECLARE var_tab nvarchar(20);
    SELECT customer_id INTO var_tab FROM table_A ;
    EXECUTE IMMEDIATE ' select * from '||var_tab;
END;

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

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