这是一个非常基本的问题,但是如何在不声明表返回列的情况下返回动态创建的查询的结果集?
我尝试了很多方法,但都没有成功。
DO $$
DECLARE
query_text text;
BEGIN
-- Build the dynamic SQL query
query_text :=
'
CREATE TEMP TABLE result_table AS
SELECT
obj->>''objectsourceeid'' AS datatag,
MAX(CASE WHEN (obj->>''objectclasstitle'') = ''area'' THEN obj->>''objectclassid'' END) AS area_objectclasstitle,
MAX(CASE WHEN (obj->>''objectclasstitle'') = ''area'' THEN (obj->>''objectinstanceproperty'')::JSONB->>''objectinstanceproperty'' END) AS area_objectinstance
FROM
TEMP_JSON_DATA,
jsonb_array_elements(data::JSONB) AS obj
GROUP BY
datatag;
SELECT * FROM result_table;
DROP TABLE result_table;
';
-- Execute the dynamic SQL query and store the result in the result_record variable
EXECUTE query_text;
END $$;
字符串
这里是一些临时数据元素。我使用JSONB。
CREATE TEMP TABLE TEMP_JSON_DATA (DATA JSONB);-- Insert the JSON data into the temporary table
INSERT INTO TEMP_JSON_DATA (DATA)
VALUES ('[
{
"Lvl": 1,
"objectclassid": "8DEE7GB6-35E1-43DC-8A53-900A30ECCD4A",
"objectclasstitle": "Root",
"objectinstance": "2465f43ff41e1aeefa21b15073224e191fda5907",
"objectinstanceproperty": {
"objectinstanceproperty": ""
},
"objectsourceeid": "P000_P1_TOT"
},
{
"Lvl": 2,
"objectclassid": "8B53E756-481E-4464-B1FE-28A96B10446C",
"objectclasstitle": "area",
"objectinstance": "873365aef4da0a5f2e7764528ddc02453e5a7bda",
"objectinstanceproperty": {
"objectinstanceproperty": "P"
},
"objectsourceeid": "P000_P1_TOT"
}
] '::JSONB);
```
Thanks in advance
型
1条答案
按热度按时间xiozqbni1#
唯一的方法是创建一个函数。如果你不需要一个永久函数,你可以创建一个临时函数,当你断开连接时,它会被删除:
字符串
您可以使用schema名称调用它:
型