title: ORA-01791 Pl-Sql error
body: <p>hi guy i have a query that give me the followin error:</p>
  
  <pre><code>ORA-01791: not a SELECTed expression   
  </code></pre>
  
  <p>this is the select expresison , please can you tell me why ? </p>
  
  <pre><code>declare
  
   freqLettura varchar2(64);
   billingcy  varchar2(64);
  
  begin
  freqLettura := null;
  billingcy := null;
  
  for rec in ( select distinct(fn_get_facilityid(z.uidfacility) ) as a, 1 as b 
              from facilityhistory z, 
              locality l , 
              plant p , 
              ztmp_sam_tb_sdv zsdv , 
              ztmp_sam_tb_plantcode zplant , 
              sam_tb_ca_pdr sam,  
              meterhistory mh, 
              meter m , 
              meterclass mc 
              where 
                  Z.UIDLOCALITY = L.UIDLOCALITY and  
                  p.UIDPLANT = L.UIDPLANT  and 
                  z.uidaccount = zsdv.uidaccount and 
                  p.plantcode = zplant.plantcode and   
                  sam.uidfacility = z.uidfacility and
                  z.stoptime is null and
                  sam.status = 'U' and
                  mh.uidfacility = z.uidfacility and 
                  mh.uidmeter = m.uidmeter and 
                  m.uidmeterclass = mc.uidmeterclass and 
                  (billingcy is null or p.UIDBILLINGCYCLE = billingcy )
              AND
              (
                  (
                  (freqLettura = 'G') AND   ( mh.corrmeterid is not null and mh.stoptime is null and mc.maxflowmeter &gt;= SAM_FN_GET_PARAMETER_FLOAT('MAXFLOWMET_DETT_GIORN'))
                  )
                  OR
                  (
                  nvl(freqLettura,'nullo') &lt;&gt; 'G' AND (freqLettura is null or sam.readfrequency = freqLettura)
                  )
              ) and ROWNUM = 1 order by sam.stoptime, sam.uidsamtbpdr desc ) loop
  begin
      insert into ztmp_sam_tb_elab_pdr (facilityid, uidbatchrequest) VALUES  (rec.a, rec.b);
  exception
       when dup_val_on_index then
          null;
    end;
  end loop;    
  </code></pre>
  
  <p>end;</p>