重构PL/pgSQL函数以返回各种SELECT查询的输出
SELECT
SELECT
CREATE OR REPLACE FUNCTION data_of(integer) RETURNS text AS$BODY$DECLARE sensors varchar(100); -- holds list of column names type varchar(100); -- holds name of table result text; -- holds SQL query -- declare more variablesBEGIN -- do some crazy stuff result := 'SELECT\r\nDatahora,' || sensors || '\r\n\r\nFROM\r\n' || type || '\r\n\r\nWHERE\r\id=' || $1 ||'\r\n\r\nORDER BY Datahora;'; RETURN result;END;$BODY$LANGUAGE 'plpgsql' VOLATILE;ALTER FUNCTION data_of(integer) OWNER TO postgres;
sensors
type
sensors
: 'column1, column2, column3'
Datahora
(timestamp
double precision
.
type
:'myTable'
Datahora
.
sensors
type
type
pcdmet
sensors
'datahora,dirvento,precipitacao,pressaoatm,radsolacum,tempar,umidrel,velvento'
SELECT
result
SELECT Datahora, column1, column2, column3FROM myTableWHERE id=20ORDER BY Datahora;
text
跃然一笑
肥皂起泡泡
相关分类