---    ALTER TABLE food600.sk__fin

DO
$$
DECLARE r RECORD;
BEGIN
IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='food600' and table_name='sk__fin' and column_name='u_datetime') THEN
	ALTER TABLE food600.sk__fin  ADD COLUMN u_datetime  timestamp without time zone NOT NULL  DEFAULT current_timestamp;
    FOR r IN
        SELECT idriadok FROM food600.sk__fin
    LOOP
	    BEGIN
			UPDATE food600.sk__fin set u_datetime = to_timestamp(substring(sk__uza.uzaverka,1,6)||sk__fin.u_cas,'YYYYMMDD HH24:MI') from food600.sk__uza where sk__fin.c_uzaverka=sk__uza.idriadok and sk__fin.idriadok=r.idriadok;  
	    EXCEPTION WHEN others THEN
																		  
	    END;
    END LOOP;
END IF;
IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='food600' and table_name='sk__fin_a' and column_name='u_datetime') THEN
	ALTER TABLE food600.sk__fin_a  ADD COLUMN u_datetime  timestamp without time zone NOT NULL  DEFAULT current_timestamp;
    FOR r IN
        SELECT idriadok FROM food600.sk__fin_a
    LOOP
	    BEGIN
			UPDATE food600.sk__fin_a set u_datetime = to_timestamp(substring(sk__uza.uzaverka,1,6)||sk__fin.u_cas,'YYYYMMDD HH24:MI') from food600.sk__uza where sk__fin.c_uzaverka=sk__uza.idriadok and sk__fin.idriadok=r.idriadok;  
	    EXCEPTION WHEN others THEN
																		  
	    END;
    END LOOP;
END IF;

END

$$

