具有动态返回值的DB2 SQL函数 [英] DB2 SQL Function with dynamic return value

查看:270
本文介绍了具有动态返回值的DB2 SQL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下工作DB2 SQL函数

I have the following working DB2 SQL function

CREATE OR REPLACE FUNCTION selector (param VARCHAR(3))
RETURNS TABLE (id INT, CMD VARCHAR(1024), ATTR CHAR(10))
LANGUAGE SQL 
DETERMINISTIC
NO EXTERNAL ACTION 
RETURN
  SELECT id, cmd, attr
      FROM test.commandtbl c
      WHERE c.attr=param;

调用如下:

select * from table (selector('c'))!      

问题是我希望返回表的大小和类型是动态的。
我想要使用具有很多返回字段的函数,而在测试时,我不想总是检查返回表,它仍然匹配。

The problem is that I want the return table to be dynamic in size and type. I want to use the function with a lot of return fields and and while testing I don't want to always check the return table it everything still matches.

例如:

Test1有5个返回列:INT,INT,INT,CHAR(10) ,VARCHAR(100)

Test1 is with 5 return columns: INT, INT, INT, CHAR(10), VARCHAR(100)

Test2有20个返回列:10 VARCHAR(100)和10 INT

Test2 is with 20 return columns: 10 VARCHAR(100) and 10 INT

等等。

有没有办法这样做?

推荐答案

考虑SQL是一种静态类型的语言,因为它在运行时几乎没有能力发现其变量(例如列)和对象(例如结果集)数据类型;您必须在语句编译时声明类型。换句话说,你想要实现的是不可能的。

You can consider SQL a statically typed language in that it has little ability to discover its variable (e.g. column) and object (e.g. result set) data types at run time; you have to declare types at the statement compilation time. In other words, what you want to achieve is not possible.

有一个通用表函数,它允许您定义基于Java的UDF返回一些结果集:

There is a concept of a generic table function which allows you to define a Java-based UDF that returns some result set:

CREATE FUNCTION selector (param VARCHAR(3))
RETURNS GENERIC TABLE
EXTERNAL NAME...

但是,您仍然需要声明接收端的结果集结构:

However, you still need to declare the result set structure on the receiving end:

SELECT t.* FROM TABLE (selector('c')) AS t (foo INT, bar INT, baz VARCHAR(10)...)

这篇关于具有动态返回值的DB2 SQL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆