执行PostgreSql函数

时间:2017-03-30 15:20:52

标签: c# postgresql syntax-error fluent-migrator

这不是我第一次遇到这个问题。 当我必须迁移我的Postgres数据库并且我必须创建一些函数时,我总是遇到FluentMigrator Migration接受的语法问题,它们通常与&#相关34;或'。

一个例子是以下功能:

DROP FUNCTION public.generate_csv_of_typology(IN typology integer)

CREATE OR REPLACE FUNCTION public.generate_csv_of_typology(IN typology integer)
RETURNS void
LANGUAGE 'plpgsql'
VOLATILE 
AS $function$
DECLARE r RECORD;
DECLARE ro RECORD;
DECLARE rf RECORD;
DECLARE nAn bigint;
DECLARE nRoi bigint;
DECLARE nf bigint;
DECLARE iA int = 0;
DECLARE iR int = 0;
DECLARE iFormato int = 0;
DECLARE nAnalisiFormato int = 0;
BEGIN
DROP TABLE IF EXISTS formati;
EXECUTE 'CREATE TABLE formati AS SELECT DISTINCT nome_formato FROM pezzo';
nf := (SELECT COUNT(*) FROM formati);

FOR rf IN EXECUTE 'SELECT * FROM formati'
LOOP
    iFormato := iFormato + 1;
    raise notice '------- n formato: (% - %) -------', iFormato, nf;

    DROP TABLE IF EXISTS analisiFormato;
    EXECUTE 'CREATE TABLE analisiFormato AS SELECT analisi.id, pezzo_id, num_vista, tipologia, esito, liv_errore, analisi.created_at, analisi.updated_at FROM analisi JOIN pezzo ON analisi.pezzo_id = pezzo.id WHERE pezzo.nome_formato = '''||rf.nome_formato::text||''' ';
    nAnalisiFormato := (SELECT COUNT(*) FROM analisiFormato);
    raise notice '------- n analisi del formato %: % -------', rf.nome_formato::text, nAnalisiFormato;

    DROP TABLE IF EXISTS numvista;    
    EXECUTE 'CREATE TABLE numvista AS SELECT DISTINCT num_vista FROM analisiFormato';

    FOR r IN EXECUTE 'SELECT * FROM numvista'
    LOOP   
        DROP TABLE IF EXISTS oiddistinct;  
        EXECUTE 'CREATE TABLE oiddistinct AS select distinct oid from analisiFormato join area on analisiFormato.id = area.analisi_id AND analisiFormato.num_vista = '||r.num_vista::text||' AND analisiFormato.tipologia = '||typology::integer;
        nRoi := (SELECT COUNT(*) FROM oiddistinct);
        FOR ro IN EXECUTE 'SELECT oid FROM oiddistinct'
        LOOP       
            raise notice '------- n roi: (% - %) -------', iR, nRoi;
            EXECUTE 'COPY
            (
                SELECT area.created_at, area.risultato_analisi, area.updated_at FROM area
                    join analisiFormato on area.analisi_id = analisiFormato.id
                    WHERE oid = '''||ro.oid::text||''' ORDER BY area.created_at 
            ) TO ''C:/Program Files/PostgreSQL/9.6/data/Formato'||rf.nome_formato::text||'NumVista'||r.num_vista::text||'-'||ro.oid::text||'.csv'' WITH CSV;';
            iR := iR + 1;
        END LOOP;
    END LOOP;
    PERFORM pg_sleep(3);
END LOOP;
END;

$function$;

从文件中读取此函数并使用命令

执行
Execute.Sql(functionDefinition);

错误是:

error 42601 syntax error at or near 'CREATE'

我试图添加其他'它会去。我想我错过了一些东西,但我没有找到任何相关的信息。有没有人知道在使用FluentMigrator迁移期间是否有关于如何执行npgsql函数的规则?

0 个答案:

没有答案