Postgresql : ERROR: in creating temp table with list object values
Postgresql : ERROR: in creating temp table with list object values
我正在编写一个 Postgresql 函数(使用 pgAdmin GUI),它将从我的 Spring 引导应用程序调用,使用 java 列表对象作为参数
提到了我的实际需求here and with the suggestions to create a temp table with list values to optimize query, following is the Postgres function which I have tried by referring from here:
CREATE FUNCTION public."getInventory"("vals1Arg" character varying[], "vals2Arg" character varying[])
RETURNS "INVENTORY"
LANGUAGE 'sql'
AS
$BODY$
// I assume the below two lines create two temp tables and populate the table
with my list object values
CREATE TEMP TABLE t1 AS
SELECT * FROM VALUES(vals1Arg)
CREATE TEMP TABLE t2 AS
SELECT * FROM VALUES(vals2Arg)
SELECT * FROM "INVENTORY"
where "COLUMN_1" in (select * from t1)
and "COLUMN_2" in (Select * from t2);
$BODY$;
以下是我调用 postgres 函数的代码片段
@Query(nativeQuery = true, value = "select \"getInventory\" (:vals1Arg,:vals2Arg)")
List<Inventory> getInventoryInfo(List<String> vals1Arg, List<String> vals2Arg);
由于列表会很大,我正在创建一个临时的 table,其中包含列表对象参数中的值,并在我的 select 查询
中使用它
提前致谢!!
有几个问题:
CREATE TABLE ... AS
的语法应该是
CREATE TABLE ... AS
SELECT * FROM (VALUES (...)) AS alias;
而不是
CREATE TABLE ... AS
SELECT * FROM VALUES (...);
您需要这些括号和别名。
查询中的子选择将不起作用,因为它将 varchar
(COLUMN_1
) 与 varchar[]
(临时 table).
要做到这一点,您必须
SELECT * FROM "INVENTORY"
WHERE "COLUMN_1" = ANY (SELECT * FROM t1);
如果您想创建临时 table 而不是直接在 SELECT
中使用数组,您最好
CREATE TEMP TABLE t1 AS
SELECT * FROM unnest(vals1Arg) AS u(c);
CREATE TEMP TABLE t2 AS
SELECT * FROM unnest(scomoIdList) AS u(c);
ANALYZE t1, t2;
RETURN QUERY SELECT * FROM "INVENTORY"
JOIN t1 ON "INVENTORY"."COLUMN_1" = t1.c
JOIN t2 ON "INVENTORY"."COLUMN_2" = t2.c;
这假定列表不包含重复条目。
我正在编写一个 Postgresql 函数(使用 pgAdmin GUI),它将从我的 Spring 引导应用程序调用,使用 java 列表对象作为参数
提到了我的实际需求here and with the suggestions to create a temp table with list values to optimize query, following is the Postgres function which I have tried by referring from here:
CREATE FUNCTION public."getInventory"("vals1Arg" character varying[], "vals2Arg" character varying[])
RETURNS "INVENTORY"
LANGUAGE 'sql'
AS
$BODY$
// I assume the below two lines create two temp tables and populate the table
with my list object values
CREATE TEMP TABLE t1 AS
SELECT * FROM VALUES(vals1Arg)
CREATE TEMP TABLE t2 AS
SELECT * FROM VALUES(vals2Arg)
SELECT * FROM "INVENTORY"
where "COLUMN_1" in (select * from t1)
and "COLUMN_2" in (Select * from t2);
$BODY$;
以下是我调用 postgres 函数的代码片段
@Query(nativeQuery = true, value = "select \"getInventory\" (:vals1Arg,:vals2Arg)")
List<Inventory> getInventoryInfo(List<String> vals1Arg, List<String> vals2Arg);
由于列表会很大,我正在创建一个临时的 table,其中包含列表对象参数中的值,并在我的 select 查询
中使用它提前致谢!!
有几个问题:
CREATE TABLE ... AS
的语法应该是CREATE TABLE ... AS SELECT * FROM (VALUES (...)) AS alias;
而不是
CREATE TABLE ... AS SELECT * FROM VALUES (...);
您需要这些括号和别名。
查询中的子选择将不起作用,因为它将
varchar
(COLUMN_1
) 与varchar[]
(临时 table).要做到这一点,您必须
SELECT * FROM "INVENTORY" WHERE "COLUMN_1" = ANY (SELECT * FROM t1);
如果您想创建临时 table 而不是直接在 SELECT
中使用数组,您最好
CREATE TEMP TABLE t1 AS
SELECT * FROM unnest(vals1Arg) AS u(c);
CREATE TEMP TABLE t2 AS
SELECT * FROM unnest(scomoIdList) AS u(c);
ANALYZE t1, t2;
RETURN QUERY SELECT * FROM "INVENTORY"
JOIN t1 ON "INVENTORY"."COLUMN_1" = t1.c
JOIN t2 ON "INVENTORY"."COLUMN_2" = t2.c;
这假定列表不包含重复条目。