简体   繁体   English

IBM DB2 将查询结果存储在一个变量中

[英]IBM DB2 store the query results in a variable

I am trying to write a function and I need to store some table in a variable.我正在尝试编写 function 并且我需要将一些表存储在变量中。 I don't have the type 'Table' I tried to write something like this:我没有“表”类型我试图写这样的东西:

declare t table(a integer, b integer);
set t = (select A, B from MyTable);

It does not work.It says there is a syntax error and that there is no table type.它不起作用。它说存在语法错误并且没有表类型。 What is the solution?解决办法是什么? Is there any other way to store the query results in a variable?有没有其他方法可以将查询结果存储在变量中?

Use declared temporary table :使用声明的临时表

DECLARE GLOBAL TEMPORARY TABLE session.table_name (a integer, b integer) AS 
(select A, B from MyTable) WITH DATA

The declared temporary table description does not appear in the system catalog.声明的临时表描述未出现在系统目录中。 It is not persistent and cannot be shared with other sessions.它不是持久的,不能与其他会话共享。 Each session that defines a declared global temporary table of the same name has its own unique description of the temporary table.每个定义了同名声明的全局临时表的 session 都有自己对临时表的唯一描述。 When the session terminates, the rows of the table are deleted, and the description of the temporary table is dropped.当 session 终止时,表的行被删除,临时表的描述被删除。

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

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