2017-09-25 8 views
0

i ont la fonction suivante, qui génère requête dynamique et à la fin je veux insérer résultat de requête dynamique dans la table, mais l'erreur que je reçois est `postgresql insert requête dynamique résultat en table curseur

ERROR: query has no destination for result data 

HINT: If you want to discard the results of a SELECT, use PERFORM instead. 
CONTEXT: PL/pgSQL function report_get_result(integer) line 46 at SQL statement 
SQL statement "SELECT report_get_result(20150131)" 
PL/pgSQL function inline_code_block line 2 at PERFORM 
********** Error **********' 

corps de la fonction est:

CREATE OR REPLACE FUNCTION report_get_result (
datekey integer 
) returns setof logic_result_rcd 
AS 
$body$ 
DECLARE 
    LogicID integer; 
    SheetName text; 
    Row_ID text; 
    Column_ID text; 
    FromTable text; 
    Operation text; 
    Amount text; 
    CriteriaType_1 text; 
    Function_1 text; 
    Criteria_1 text; 
    CriteriaType_2 text; 
    Function_2 text; 
    Criteria_2 text; 
    CriteriaType_3 text; 
    Function_3 text; 
    Criteria_3 text; 
    sql text; 
    INC Integer; 
begin 
DROP TABLE IF EXISTS loans; 
create temp table loans as 
select * from loan.vfact_state_principal where "DateKey" = datekey; 

DECLARE cursor_logic REFCURSOR; 
BEGIN 
OPEN cursor_logic for execute ('SELECT "LogicID" FROM logic_table_rcd'); 
LOOP 
    FETCH cursor_logic INTO INC; 
    if not found then exit; 
    end if; 
    BEGIN 
SELECT LogicID = "LogicID" 
      ,SheetName = "SheetName" 
      ,Row_ID = "Row_ID"::text 
      ,Column_ID = "Column_ID"::text 
      ,FromTable = "FromTable" 
      ,Operation = "Operation" 
      ,Amount = "Amount" 
      ,CriteriaType_1 = CASE WHEN "CriteriaType_1" <> '' THEN ('WHERE 
' || "CriteriaType_1") ELSE '' END 
      ,Function_1 = CASE WHEN "Function_1" is null THEN 'Empty' ELSE 
"Function_1" END 
      ,Criteria_1 = CASE WHEN "Criteria_1" is null THEN 'Empty' ELSE 
"Criteria_1" END 
      ,CriteriaType_2 = CASE WHEN "CriteriaType_2" <> '' THEN ' AND ' 
|| "CriteriaType_2" ELSE '' END 
      ,Function_2 = CASE WHEN "Function_2" is null THEN 'Empty' ELSE 
"Function_2" END 
      ,Criteria_2 = CASE WHEN "Criteria_2" is null THEN 'Empty' ELSE 
"Criteria_2" END 
      ,CriteriaType_3 = CASE WHEN "CriteriaType_3" <> '' THEN ' AND ' 
|| "CriteriaType_3" ELSE '' END 
      ,Function_3 = CASE WHEN "Function_3" is null THEN 'Empty' ELSE 
"Function_3" END 
      ,Criteria_3 = CASE WHEN "Criteria_3" is null THEN 'Empty' ELSE 
"Criteria_3" END 
    FROM public.logic_table_rcd WHERE "LogicID" = INC; 

sql:= 'INSERT INTO public.logic_result_rcd SELECT ' || INC::text || ', 1, ' 
|| EntityID::text || ', ' || DateKey::text || ', ' || 'RCD' || ', ' || 
SheetName::text || ', ' || Row_ID::text || ', ' || Column_ID::text || ', ' 
|| Operation || '(' || Amount || ')' || ' FROM ' || FromTable 
    || CriteriaType_1 || ' ' || Function_1 || ' ' || Criteria_1 
    || CriteriaType_2 || ' ' || Function_2 || ' ' || Criteria_2 
    || CriteriaType_3 || ' ' || Function_3 || ' ' || Criteria_3; 

RETURN QUERY EXECUTE sql; 
END; 
END LOOP; 
CLOSE cursor_logic; 
END; 
END; 

$body$ 

LANGUAGE plpgsql; 
+0

sélectionner dans ... ou effectuer une sélection (résultat défaussant) –

+0

merci. mais je suis nouveau à postgres et ne sais pas comment écrire sélectionner dans ... ou effectuer une sélection (résultat de rejet). pouvez-vous écrire une partie du code qui est incorrecte ??? – Chernusha

+0

erreur se produit à '' 'SELECT LogicID =" LogicID ", SheetName =" SheetNa ..... OERE "LogicID" = INC; '' '- vous devriez" mettre "résultat quelque part ou le jeter. Supposons que vous vouliez attribuer ce sql comme valeur de texte à sql variable, mais je ne peux pas le dire -parce que votre poste manque de commentaires –

Répondre

0

valeur assign avec VAR_NAME := ''; construction, voici un exemple:

t=# create function f(_i int) returns table (a int) as $$ 
declare sql text; 
begin 
sql := format('select %s',_i); 
return query execute sql; 
end; 
$$ 
language plpgsql 
; 
CREATE FUNCTION 
t=# select * From f(3); 
a 
--- 
3 
(1 row) 
+0

merci. J'ai assigné avec succès des valeurs à variables.now, le problème est "ERREUR: impossible d'ouvrir la requête INSERT en tant que curseur". – Chernusha

+0

poster le code, erreur et question en tant que nouveau poste s'il vous plaît –

+0

https://stackoverflow.com/questions/46423638/postgresql-cannot-open-insert-query-as-cursor – Chernusha