I have the following function (returns a set of custom types)
CREATE OR REPLACE FUNCTION myfunction(in_myvar integer)
RETURNS SETOF my_type AS
$BODY$
DECLARE
v_data my_type%rowtype;
v_another_var text;
BEGIN
FOR v_data IN (
-- I want to be able to do a SELECT INTO v_another_var in each iteration
-- of the loop, ideally so I can use this value throughout the select
-- statement e.g.:
-- SELECT INTO v_another_var regex_replace(col3, '[^a-zA-Z0-9 ]', '');
SELECT DISTINCT
col1
col2,
-- v_another_var AS some_column
-- etc.
-- perhaps use this v_another_var again here
-- as part of something else.
FROM mytable
WHERE v_another_var = 'test' + some_function() = 'something'
-- want to use the variable again here.
) LOOP
RETURN NEXT v_data;
END LOOP;
RETURN;
$BODY$
Is it possible to assign a variable and use it in each select statement as part of a loop in this way? Even if this is possible, would the SELECT INTO statement work where I have it (would col3 be accessible?)
Is there a way in postgres to achieve this kind of variable usage?