When I attempt to create a new functions using the above code I get this:
ERROR: syntax error at or newr "BODY&DECLARE
i int := 1;
r text := '';
a anyarray := string_to_array( o, ',' );
m int := array_ndims( a );
begin
loop
if ( a[i] <> '' ) then
if ( r = '' ) then
r := ( select full_code from option_table where option_table.code = a[i] );
else
r := r || ',' || ( select full_code from option_table where option_table.code = a[i] );
end if;
end if;
i := i + 1;
exit when i > m;
end loop;
return r;
end;
$$ language plpgsql;
SELECT stocknumber, inventorystatus, decode_options( options )
FROM stock LEFT OUTER JOIN vehicleoptions ON vehicleoptions.option_cd = stock.options&BODY&" at character 84
Main Topics
Browse All Topics





by: earthman2Posted on 2009-10-20 at 04:13:37ID: 25612847
you could use some fancy SQL functions using string_to_array, unnest and aggregate functions but something like the following (untested) should suffice.
create or replace function decode_options( o text ) returns text as $$
declare
i int := 1;
r text := '';
a anyarray := string_to_array( o, ',' );
m int := array_ndims( a );
begin
loop
if ( a[i] <> '' ) then
if ( r = '' ) then
r := ( select full_code from option_table where option_table.code = a[i] );
else
r := r || ',' || ( select full_code from option_table where option_table.code = a[i] );
end if;
end if;
i := i + 1;
exit when i > m;
end loop;
return r;
end;
$$ language plpgsql;
SELECT stocknumber, inventorystatus, decode_options( options )
FROM stock LEFT OUTER JOIN vehicleoptions ON vehicleoptions.option_cd = stock.options