Stored procedure returns an array; how to use in SQL?

一曲冷凌霜 提交于 2019-12-12 05:27:41

问题


I am writing an accounting system backed by an h2 database. The tree of accounts is stored in the ACCOUNTS table, with the PARENT_ID column storing the links in the tree.

To get the path to a given node in the tree, I have the following stored procedure:

public static Object[] getAncestorPKs(Long id)

whose job is to produce an array of integers, being the PARENT_ID values between the given node and the root of the tree. I register the procedure in the database under the name ANCESTOR_PKS like this:

CREATE ALIAS IF NOT EXISTS ANCESTOR_PKS FOR "xxx.yyy.zzz.getAncestorPKs"

My problem is I can't seem to find a way to use the thing without triggering a SQLException! I need to use it in the following way:

SELECT ID FROM ACCOUNTS WHERE [...] AND ID IN ANCESTOR_PKS(5) [for example]

I get the following error:

Syntax error in SQL statement "SELECT ID FROM ACCOUNTS WHERE ID IN ANCESTOR_PKS[*](5) "; expected "("

But there is a bracket after ANCESTOR_PKS!!! What on earth is going on here? Is there some way to use IN with the result of a stored procedure? I have searched and searched, but all the examples of the use of IN that I see are with literal arrays, and no-one seems to think of showing how to use an array which is the return value of a stored procedure.

Help???


回答1:


I'm unfamiliar with h2, but this page says you must place () around the array.

SELECT ID FROM ACCOUNTS WHERE ID IN (ANCESTOR_PKS(5))

Maybe you even have to add a subselect

SELECT ID FROM ACCOUNTS WHERE ID IN (SELECT ANCESTOR_PKS(5))


来源:https://stackoverflow.com/questions/11658251/stored-procedure-returns-an-array-how-to-use-in-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!