drop function check_open(stock_bar, float);

create function check_open(tsrow stock_bar, ARG_VAL float) returning stock_bar;

-- Use the argument in some way
if tsrow.high > ARG_VAL then		
	let tsrow.high = -1;
end if

return row(tsrow.timestamp, tsrow.high, tsrow.low, tsrow.final, tsrow.vol)::stock_bar;

end function;

--------------------------------------------------------------------------------

drop procedure spl_test();

create procedure spl_test()

define id integer;
define name lvarchar;
define var stock_bar;
define ts timeseries(stock_bar);

begin work;

foreach select stock_id, stock_name, stock_data into id, name, ts from daily_stocks
	
	foreach execute function transpose(
	    ts::timeseries(stock_bar),
	    '2011-01-03 00:00:00.00000'::datetime year to fraction(5), 
	    '2011-04-10 00:00:00.00000'::datetime year to fraction(5))
	     into var

	      execute function check_open(var, 600) into var;

	      update daily_stocks set stock_data = PutElemNoDups(stock_data, var);
	end foreach
end foreach

commit work;

end procedure;
