Re: ECPG: non-integer constant in group by - Mailing list pgsql-general

From Poul Jensen
Subject Re: ECPG: non-integer constant in group by
Date
Msg-id 450C271B.3050108@gfy.ku.dk
Whole thread Raw
In response to Re: ECPG: non-integer constant in group by  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
Martijn van Oosterhout wrote:
> for (i=0; i<NVARS; i++)
> {
>    sprintf(stmt,"SELECT %s FROM beamdata GROUP BY %s;",vars[i],vars[i]);
>    EXEC SQL PREPARE mystmt FROM :stmt;
>    EXEC SQL EXECUTE mystmt INTO wherever;
>    n_occ[i] = sqlca.sqlerrd[2];
> }
>
Apologies. I already read this in the docs, but also forgot it again.
:-| There is a little more to the solution since I need another array to
save the retrieved data after each query. So in the hope to help others,
here's how I did it:

int *all_vars[NVARS];
int *tmp=NULL;

  for (i=0; i<NVARS; i++)
  {
    sprintf(stmt,"SELECT %s FROM beamdata GROUP BY %s;",vars[i],vars[i]);
    EXEC SQL PREPARE query FROM :stmt;
    EXEC SQL EXECUTE query INTO :tmp;
    n_occ[i] = sqlca.sqlerrd[2];   /* Number of rows processed in query */
    if ((all_vars[i]=(int *)malloc(n_occ[i]*sizeof(int)))==NULL)
    {
      fprintf(stderr,"Memory allocation failure\n");
      exit(-1);
    }
    memcpy(all_vars[i], tmp, n_occ[i]*sizeof(int));
    EXEC SQL DEALLOCATE PREPARE query;
    tmp=NULL;
  }

(Remember to free allocated memory when done)

Thanks so much for the help!


pgsql-general by date:

Previous
From: Peter Bauer
Date:
Subject: Fwd: Multiple entries of same table in pg_class
Next
From: Tom Lane
Date:
Subject: Re: Fwd: Multiple entries of same table in pg_class