Pl/pgSQL priekšrakstā EXECUTE nav parametra $1

Es nevaru atrisināt šo:

CREATE OR REPLACE FUNCTION dpol_insert(
    dpol_cia integer, dpol_tipol character, dpol_nupol integer,
    dpol_conse integer,dpol_date timestamp)
  RETURNS integer AS
$BODY$
    DECLARE tabla text := 'dpol'||EXTRACT (YEAR FROM $5::timestamp);
BEGIN
    EXECUTE '
    INSERT INTO '|| quote_ident(tabla) ||' 
    (dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$4,$5)
    ';
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

Mēģinot

SELECT dpol_insert(1,'X',123456,1,'09/10/2013')

atgriezt nākamo ziņojumu:

ERROR:  there is no parameter $1
LINE 3: ...tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$...
                                                             ^
QUERY:  
    INSERT INTO dpol2013 
    (dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$4,$5)

CONTEXT:  PL/pgSQL function "dpol_insert" line 4 at EXECUTE statement

*** Kļūda ***

ERROR: there is no parameter $1
SQL state: 42P02
Context: PL/pgSQL function "dpol_insert" line 4 at EXECUTE statement

person stefmex    schedule 10.10.2013    source avots


Atbildes (1)


Šeit jums ir dažas problēmas. Tūlītējā problēma ir:

KĻŪDA: nav parametra $1

Tas notiek tāpēc, ka $1 SQL, kuru nododat EXECUTE, nav tas pats, kas $1 galvenās funkcijas pamattekstā. Numurētie vietturi EXECUTE SQL ir kontekstā ar EXECUTE, nevis funkcijas kontekstā, tāpēc jums ir jāiesniedz daži argumenti EXECUTE šiem vietturiem:

execute '...' using dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date;
--            ^^^^^

Skatiet sadaļu Dinamisko komandu izpilde sadaļā rokasgrāmatā, lai iegūtu sīkāku informāciju.

Nākamā problēma ir tāda, ka jūs neko neatgriežat no savas funkcijas, kas RETURNS integer. Es nezinu, ko jūs plānojat atgriezt, bet, iespējams, jūsu tablea ir SĒRIJA id, kuru vēlaties atgriezt. Ja tā, tad jūs vēlaties kaut ko vairāk līdzīgu:

declare
    tabla text := 'dpol' || extract(year from $5::timestamp);
    id integer;
begin
    execute 'insert into ... values ($1, ...) returning id' into id using dpol_cia, ...;
    --                                        ^^^^^^^^^^^^  ^^^^^^^
    return id;
end

savā funkcijā.

person mu is too short    schedule 10.10.2013
comment
@mu ir pārāk īss: šī ir tā pati kļūda, ko saņemu savā SQL funkcijā. Man nav izdevies to atrisināt, varbūt jums ir kādas norādes? -Paldies! stackoverflow.com/ jautājumi/19918385/ - person jO.; 14.11.2013