Я пишу функцию PostGreSQL и моя конструкция выглядит следующим образом:Повторное использование объявленной переменной в функции Postgres
CREATE OR REPLACE FUNCTION function_name (argument_list) RETURNS INTEGER []
AS $$
DECLARE
--along with other declarations
_tablename text;
BEGIN
-- dynamically construct the intermediate _tablename which gets
-- populated
-- Now I want to use this _tablename in other queries like :
-- use it in the select from _tablename loop
-- construct array by selecting a column from this table
-- and return that array
END
Как мне это сделать? Я хочу повторно использовать объявленное имя переменной в моих дальнейших запросах функции.
Моя полная функция Postgres выглядит следующим образом:
DROP FUNCTION get_value_histogram(BIGINT,BIGINT,BIGINT,INTEGER);
CREATE OR REPLACE FUNCTION get_value_histogram(customer_id BIGINT,
start_time BIGINT, end_time BIGINT, bucket_size INTEGER)
RETURNS INTEGER[] AS
$$
DECLARE
_tablename text;
_curr_timestamp BIGINT;
_var1 text;
_min_value INTEGER;
_max_value INTEGER;
_return_array INTEGER[];
BEGIN
-- create an intermediate table with the aggregation of the
-- required values. These values then will be passed to the
-- Histogram function.
_var1 := EXTRACT (EPOCH FROM now());
_var1 := replace(_var1, '.','_');
_tablename := 'thing_data_' || _var1;
EXECUTE 'CREATE TABLE ' || _tablename || ' (t_stamp BIGINT, sum_of_values INTEGER)';
--insert all the values in this intermediate table
EXECUTE ' INSERT INTO ' || _tablename || ' (select t_stamp , sum(data) from thing_data td, collector_tb ct where td.thingname =
ct.collector_name and td.t_stamp BETWEEN ' || quote_literal(start_time) || ' AND ' || quote_literal(end_time) || ' and
ct.type like ' || quote_literal('%outlet%') ||' AND customer_id = ' || customer_id || ' GROUP BY t_stamp)' ;
EXECUTE 'select width_bucket(sum_of_values,500, 1000 , 100), count(*) as cnt from ' || _tablename || ' GROUP BY 1 ORDER BY 1' ;
_return_array := array (select cnt from (select width_bucket(sum_of_values,500, 1000 , 100), count(*) as cnt from _tablename GROUP BY 1 ORDER BY 1));
EXECUTE 'DROP TABLE ' || _tablename;
RETURN _return_array;
END $$ LANGUAGE plpgsql;
Когда я запускаю это, я получаю ошибку говоря отношение «_tablename
» не существует
Да, я знаю, что запрос неправильно и понять, что _tablename не признается в своем запросе. Я ищу решение. Благодаря! – neerajdorle