0
votes

I'm trying to execute the following PostgreSQL code

DECLARE f RECORD;
DECLARE p RECORD;
DECLARE idaus int = 0;
BEGIN   
     FOR f IN EXECUTE 'SELECT DISTINCT name_f FROM piece'
     LOOP 
         EXECUTE 'INSERT INTO format (name) VALUES('''|| f.name_f ::text||''')';
     END LOOP;

     EXECUTE 'ALTER TABLE piece ADD COLUMN format_id integer';

     FOR p IN EXECUTE 'SELECT DISTINCT name_f FROM piece'
     LOOP
         idaus := (SELECT format.id FROM format WHERE name = p.name_f );
         EXECUTE 'UPDATE piece SET format_id = '||idaus||' WHERE name_f = '''||p.name_f ::text||'''';
     END LOOP;

     EXECUTE 'ALTER TABLE piece ADD CONSTRAINT FK_PieceFormat FOREIGN KEY (format_id) REFERENCES format ON DELETE CASCADE;';

     EXECUTE 'ALTER TABLE piece DROP COLUMN name_f RESTRICT';
 END;

during a migration

 var script = File.ReadAllText(FunctionFullPathName);
 Execute.Sql(script );

but I have the following error:

42601: syntax error near or at "RECORD"

I read this https://github.com/fluentmigrator/fluentmigrator/wiki/Fluent-Interface but I didn't find anything. Does anyone know what I am doing wrong?

1

1 Answers

0
votes

(Posted on behalf of the OP).

I found this solution; maybe it's not the best, but at least it works. I defined a function with that code inside and, after having created it, I call it as follow:

Execute.Sql("SELECT do_migration_004()");